2 * GreyscaleRotatedLuminanceSource.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.
21 #include <zxing/common/GreyscaleRotatedLuminanceSource.h>
22 #include <zxing/common/IllegalArgumentException.h>
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,
29 int dataWidth, int dataHeight, int left, int top, int width, int height) : greyData_(greyData),
30 dataWidth_(dataWidth), dataHeight_(dataHeight), left_(left), top_(top), width_(width),
33 // Intentionally comparing to the opposite dimension since we're rotated.
34 if (left + width > dataHeight || top + height > dataWidth) {
35 throw IllegalArgumentException("Crop rectangle does not fit within image data.");
39 // The API asks for rows, but we're rotated, so we return columns.
40 unsigned char* GreyscaleRotatedLuminanceSource::getRow(int y, unsigned char* row) {
41 if (y < 0 || y >= getHeight()) {
42 throw IllegalArgumentException("Requested row is outside the image: " + y);
44 int width = getWidth();
45 // TODO(flyashi): determine if row has enough size.
47 row = new unsigned char[width];
49 int offset = (left_ * dataWidth_) + (dataWidth_ - (y + top_));
50 for (int x = 0; x < width; x++) {
51 row[x] = greyData_[offset];
57 unsigned char* GreyscaleRotatedLuminanceSource::getMatrix() {
58 unsigned char* result = new unsigned char[width_ * height_];
59 unsigned char* row = new unsigned char[width_];
60 for (int y = 0; y < height_; y++) {
62 memcpy(result + y * width_, row, width_);