C++: added GreyscaleLuminanceSource
[zxing.git] / cpp / core / src / zxing / common / GreyscaleLuminanceSource.cpp
1 /*
2  *  GreyscaleLuminanceSource.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 #include <zxing/common/GreyscaleLuminanceSource.h>
21 #include <zxing/common/GreyscaleRotatedLuminanceSource.h>
22 #include <zxing/common/IllegalArgumentException.h>
23
24 namespace zxing {
25
26 GreyscaleLuminanceSource::GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top,
27       int width, int height) : greyData_(greyData), dataWidth_(dataWidth), dataHeight_(dataHeight),
28       left_(left), top_(top), width_(width), height_(height)  {
29
30   if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) {
31     throw IllegalArgumentException("Crop rectangle does not fit within image data.");
32   }
33 }
34
35 unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) {
36
37   if (y < 0 || y >= this->getHeight()) {
38     throw IllegalArgumentException("Requested row is outside the image: " + y);
39   }
40   int width = getWidth();
41   // TODO(flyashi): determine if row has enough size.
42   if (row == NULL) {
43     row = new unsigned char[width_];
44   }
45   int offset = (y + top_) * dataWidth_ + left_;
46   memcpy(row, &greyData_[offset], width);
47
48   return row;
49 }
50
51
52 unsigned char* GreyscaleLuminanceSource::getMatrix() {
53   return greyData_;
54 }
55
56 Ref<LuminanceSource> GreyscaleLuminanceSource::rotateCounterClockwise() {
57   // Intentionally flip the left, top, width, and height arguments as needed. dataWidth and
58   // dataHeight are always kept unrotated.
59   return Ref<LuminanceSource> (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_, dataHeight_,
60                                top_, left_, height_, width_));
61 }
62
63
64 } /* namespace */
65