I moved a chunk of the histogram/black point code out of BaseMonochromeBitmapSource...
[zxing.git] / core / src / com / google / zxing / common / CroppedMonochromeBitmapSource.java
1 /*
2  * Copyright 2009 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.common;
18
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22
23 /**
24  * Encapulates a cropped region of another {@link com.google.zxing.MonochromeBitmapSource}.
25  *
26  * @author Sean Owen
27  */
28 public final class CroppedMonochromeBitmapSource implements MonochromeBitmapSource {
29
30   private final MonochromeBitmapSource delegate;
31   private final int left;
32   private final int top;
33   private final int right;
34   private final int bottom;
35
36   /**
37    * Creates an instance that uses only a region of the given image as a source of pixels to decode.
38    *
39    * @param delegate image to decode a region of
40    * @param left x coordinate of leftmost pixels to decode
41    * @param top y coordinate of topmost pixels to decode
42    * @param right one more than the x coordinate of rightmost pixels to decode, i.e. we will decode
43    *  pixels whose x coordinate is in [left,right)
44    * @param bottom likewise, one more than the y coordinate of the bottommost pixels to decode
45    */
46   public CroppedMonochromeBitmapSource(MonochromeBitmapSource delegate,
47                                        int left, int top, int right, int bottom) {
48     this.delegate = delegate;
49     this.left = left;
50     this.top = top;
51     this.right = right;
52     this.bottom = bottom;
53   }
54
55   public boolean isBlack(int x, int y) {
56     return delegate.isBlack(left + x, top + y);
57   }
58
59   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
60     return delegate.getBlackRow(top + y, row, left + startX, getWidth);
61   }
62
63   public BitArray getBlackColumn(int x, BitArray column, int startY, int getHeight) {
64     return delegate.getBlackColumn(left + x, column, top + startY, getHeight);
65   }
66
67   public int getHeight() {
68     return bottom - top;
69   }
70
71   public int getWidth() {
72     return right - left;
73   }
74
75   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument)
76       throws ReaderException {
77     // Hmm, the delegate will probably base this on the whole image though...
78     delegate.estimateBlackPoint(method, argument);
79   }
80
81   public BlackPointEstimationMethod getLastEstimationMethod() {
82     return delegate.getLastEstimationMethod();
83   }
84
85   public MonochromeBitmapSource rotateCounterClockwise() {
86     MonochromeBitmapSource rotated = delegate.rotateCounterClockwise();
87     return new CroppedMonochromeBitmapSource(rotated,
88                                              top,
89                                              delegate.getWidth() - right,
90                                              delegate.getHeight() - bottom,
91                                              left);
92   }
93
94   public boolean isRotateSupported() {
95     return delegate.isRotateSupported();
96   }
97
98   public int getLuminance(int x, int y) {
99     return delegate.getLuminance(x, y);
100   }
101
102   public int[] getLuminanceRow(int y, int[] row) {
103     return delegate.getLuminanceRow(y, row);
104   }
105
106   public int[] getLuminanceColumn(int x, int[] column) {
107     return delegate.getLuminanceColumn(x, column);
108   }
109
110 }