C++: added GreyscaleLuminanceSource
[zxing.git] / cpp / core / src / zxing / common / GreyscaleRotatedLuminanceSource.cpp
1 /*
2  *  GreyscaleRotatedLuminanceSource.cpp
3  *  zxing
4  *
5  *  Copyright 2010 ZXing authors All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20
21 #include <zxing/common/GreyscaleRotatedLuminanceSource.h>
22 #include <zxing/common/IllegalArgumentException.h>
23
24 namespace zxing {
25
26 // Note that dataWidth and dataHeight are not reversed, as we need to be able to traverse the
27 // greyData correctly, which does not get rotated.
28 GreyscaleRotatedLuminanceSource::GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight,
29       int left, int top, int width, int height) : greyData_(greyData), dataWidth_(dataWidth), dataHeight_(dataHeight),
30       left_(left), top_(top), width_(width), height_(height) {
31
32   // Intentionally comparing to the opposite dimension since we're rotated.
33   if (left + width > dataHeight || top + height > dataWidth) {
34     throw IllegalArgumentException("Crop rectangle does not fit within image data.");
35   }
36 }
37
38 // The API asks for rows, but we're rotated, so we return columns.
39 unsigned char* GreyscaleRotatedLuminanceSource::getRow(int y, unsigned char* row) {
40   if (y < 0 || y >= getHeight()) {
41     throw IllegalArgumentException("Requested row is outside the image: " + y);
42   }
43   int width = getWidth();
44   // TODO(flyashi): determine if row has enough size.
45   if (row == NULL) {
46     row = new unsigned char[width];
47   }
48   int offset = (left_ * dataWidth_) + (dataWidth_ - (y + top_));
49   for (int x = 0; x < width; x++) {
50     row[x] = greyData_[offset];
51     offset += dataWidth_;
52   }
53   return row;
54 }
55
56 unsigned char* GreyscaleRotatedLuminanceSource::getMatrix() {
57   // FIXME(flyashi): fine for 1D scanning, need to implement for 2D scanning
58   return NULL;
59 }
60
61 } // namespace