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,
27 int dataHeight, int left, int top, int width, int height) : greyData_(greyData),
28 dataWidth_(dataWidth), dataHeight_(dataHeight), left_(left), top_(top), width_(width),
31 if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) {
32 throw IllegalArgumentException("Crop rectangle does not fit within image data.");
36 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);
50 unsigned char* GreyscaleLuminanceSource::getMatrix() {
51 int size = width_ * height_;
52 unsigned char* result = new unsigned char[size];
53 if (left_ == 0 && top_ == 0 && dataWidth_ == width_ && dataHeight_ == height_) {
54 memcpy(result, greyData_, size);
56 for (int row = 0; row < height_; row++) {
57 memcpy(result + row * width_, greyData_ + (top_ + row) * dataWidth_ + left_, width_);
63 Ref<LuminanceSource> GreyscaleLuminanceSource::rotateCounterClockwise() {
64 // Intentionally flip the left, top, width, and height arguments as needed. dataWidth and
65 // dataHeight are always kept unrotated.
66 return Ref<LuminanceSource> (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_,
67 dataHeight_, top_, left_, height_, width_));