2 * GreyscaleLuminanceSource.cpp
5 * Copyright 2010 ZXing authors All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <zxing/common/GreyscaleLuminanceSource.h>
21 #include <zxing/common/GreyscaleRotatedLuminanceSource.h>
22 #include <zxing/common/IllegalArgumentException.h>
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) {
30 if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) {
31 throw IllegalArgumentException("Crop rectangle does not fit within image data.");
35 unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) {
37 if (y < 0 || y >= this->getHeight()) {
38 throw IllegalArgumentException("Requested row is outside the image: " + y);
40 int width = getWidth();
41 // TODO(flyashi): determine if row has enough size.
43 row = new unsigned char[width_];
45 int offset = (y + top_) * dataWidth_ + left_;
46 memcpy(row, &greyData_[offset], width);
52 unsigned char* GreyscaleLuminanceSource::getMatrix() {
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_));