GreyscaleLuminanceSource.cpp Example File
appdemos/qtws/QZXing/zxing/zxing/common/GreyscaleLuminanceSource.cpp
#include <zxing/common/GreyscaleLuminanceSource.h>
#include <zxing/common/GreyscaleRotatedLuminanceSource.h>
#include <zxing/common/IllegalArgumentException.h>
using zxing::Ref;
using zxing::ArrayRef;
using zxing::LuminanceSource;
namespace zxing {
GreyscaleLuminanceSource::
GreyscaleLuminanceSource(ArrayRef<byte> greyData,
int dataWidth, int dataHeight,
int left, int top,
int width, int height)
: Super(width, height),
greyData_(greyData),
dataWidth_(dataWidth), dataHeight_(dataHeight),
left_(left), top_(top) {
if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) {
throw IllegalArgumentException("Crop rectangle does not fit within image data.");
}
}
ArrayRef<byte> GreyscaleLuminanceSource::getRow(int y, ArrayRef<byte> row) const {
if (y < 0 || y >= this->getHeight()) {
throw IllegalArgumentException("Requested row is outside the image.");
}
int width = getWidth();
if (!row || row->size() < width) {
ArrayRef<byte> temp (width);
row = temp;
}
int offset = (y + top_) * dataWidth_ + left_;
memcpy(&row[0], &greyData_[offset], width);
return row;
}
ArrayRef<byte> GreyscaleLuminanceSource::getMatrix() const {
if (left_ == 0 && top_ == 0 && dataWidth_ == getWidth() && dataHeight_ == getHeight()) {
return greyData_;
} else {
int size = getWidth() * getHeight();
ArrayRef<byte> result (size);
for (int row = 0; row < getHeight(); row++) {
memcpy(&result[row * getWidth()], &greyData_[(top_ + row) * dataWidth_ + left_], getWidth());
}
return result;
}
}
Ref<LuminanceSource> GreyscaleLuminanceSource::rotateCounterClockwise() const {
Ref<LuminanceSource> result (
new GreyscaleRotatedLuminanceSource(greyData_,
dataWidth_, dataHeight_,
top_, left_, getHeight(), getWidth()));
return result;
}
}