Small style stuff
[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,
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),
31     height_(height) {
32
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.");
36   }
37 }
38
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);
43   }
44   int width = getWidth();
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   unsigned char* result = new unsigned char[width_ * height_];
58   // This depends on getRow() honoring its second parameter.
59   for (int y = 0; y < height_; y++) {
60     getRow(y, &result[y * width_]);
61   }
62   return result;
63 }
64
65 } // namespace