From 849866a07aa95632a41bc7a82820bd2379e0c88a Mon Sep 17 00:00:00 2001 From: "smparkes@smparkes.net" Date: Sat, 6 Apr 2013 19:15:02 +0000 Subject: [PATCH] C++ updates for r2574 and other java reconciliation git-svn-id: https://zxing.googlecode.com/svn/trunk@2622 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../src/zxing/InvertedLuminanceSource.cpp | 44 ++++++++++++++++ cpp/core/src/zxing/InvertedLuminanceSource.h | 38 ++++++++++++++ cpp/core/src/zxing/LuminanceSource.cpp | 13 +++-- cpp/core/src/zxing/LuminanceSource.h | 28 ++++++---- cpp/core/src/zxing/ZXing.h | 12 +++-- cpp/core/src/zxing/common/Array.h | 4 +- .../zxing/common/GreyscaleLuminanceSource.cpp | 52 +++++-------------- .../zxing/common/GreyscaleLuminanceSource.h | 29 ++++------- .../GreyscaleRotatedLuminanceSource.cpp | 30 +++++------ .../common/GreyscaleRotatedLuminanceSource.h | 22 +++----- cpp/magick/src/MagickBitmapSource.cpp | 25 +++------ cpp/magick/src/MagickBitmapSource.h | 16 +++--- 12 files changed, 176 insertions(+), 137 deletions(-) create mode 100644 cpp/core/src/zxing/InvertedLuminanceSource.cpp create mode 100644 cpp/core/src/zxing/InvertedLuminanceSource.h diff --git a/cpp/core/src/zxing/InvertedLuminanceSource.cpp b/cpp/core/src/zxing/InvertedLuminanceSource.cpp new file mode 100644 index 000000000..a56e27909 --- /dev/null +++ b/cpp/core/src/zxing/InvertedLuminanceSource.cpp @@ -0,0 +1,44 @@ +// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- +/* + * Copyright 2013 ZXing authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +using zxing::ArrayRef; +using zxing::InvertedLuminanceSource; + +InvertedLuminanceSource::InvertedLuminanceSource(Ref const& delegate_) + : Super(delegate_->getWidth(), delegate_->getHeight()), delegate(delegate_) {} + +ArrayRef InvertedLuminanceSource::getRow(int y, ArrayRef row) const { + row = delegate->getRow(y, row); + int width = getWidth(); + for (int i = 0; i < width; i++) { + row[i] = (byte) (255 - (row[i] & 0xFF)); + } + return row; +} + +ArrayRef InvertedLuminanceSource::getMatrix() const { + ArrayRef matrix = delegate->getMatrix(); + int length = getWidth() * getHeight(); + ArrayRef invertedMatrix(length); + for (int i = 0; i < length; i++) { + invertedMatrix[i] = (byte) (255 - (matrix[i] & 0xFF)); + } + return invertedMatrix; +} diff --git a/cpp/core/src/zxing/InvertedLuminanceSource.h b/cpp/core/src/zxing/InvertedLuminanceSource.h new file mode 100644 index 000000000..0c0091379 --- /dev/null +++ b/cpp/core/src/zxing/InvertedLuminanceSource.h @@ -0,0 +1,38 @@ +// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- +#ifndef __INVERTEDLUMINANCESOURCE_H__ +#define __INVERTEDLUMINANCESOURCE_H__ +/* + * Copyright 2013 ZXing authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace zxing { + +class InvertedLuminanceSource : public LuminanceSource { +private: + typedef LuminanceSource Super; + const Ref delegate; + +public: + InvertedLuminanceSource(Ref const&); + + ArrayRef getRow(int y, ArrayRef row) const; + ArrayRef getMatrix() const; +}; + +} + +#endif /* INVERTEDLUMINANCESOURCE_H_ */ diff --git a/cpp/core/src/zxing/LuminanceSource.cpp b/cpp/core/src/zxing/LuminanceSource.cpp index c2e0133b4..b139c6927 100644 --- a/cpp/core/src/zxing/LuminanceSource.cpp +++ b/cpp/core/src/zxing/LuminanceSource.cpp @@ -20,16 +20,15 @@ #include #include +#include #include using zxing::Ref; using zxing::LuminanceSource; -LuminanceSource::LuminanceSource() { -} +LuminanceSource::LuminanceSource(int width_, int height_) :width(width_), height(height_) {} -LuminanceSource::~LuminanceSource() { -} +LuminanceSource::~LuminanceSource() {} bool LuminanceSource::isCropSupported() const { return false; @@ -51,7 +50,7 @@ Ref LuminanceSource::rotateCounterClockwise() { throw IllegalArgumentException("This luminance source does not support rotation."); } -LuminanceSource::operator std::string() { +LuminanceSource::operator std::string() const { ArrayRef row; std::ostringstream oss; for (int y = 0; y < getHeight(); y++) { @@ -74,3 +73,7 @@ LuminanceSource::operator std::string() { } return oss.str(); } + +Ref LuminanceSource::invert(Ref const& that) { + return Ref(new InvertedLuminanceSource(that)); +} diff --git a/cpp/core/src/zxing/LuminanceSource.h b/cpp/core/src/zxing/LuminanceSource.h index 6178e9aba..8f657e86c 100644 --- a/cpp/core/src/zxing/LuminanceSource.h +++ b/cpp/core/src/zxing/LuminanceSource.h @@ -25,29 +25,35 @@ #include namespace zxing { - class LuminanceSource; -} -class zxing::LuminanceSource : public Counted { -public: - LuminanceSource(); +class LuminanceSource : public Counted { + private: + const int width; + const int height; + + public: + LuminanceSource(int width, int height); virtual ~LuminanceSource(); - virtual int getWidth() const = 0; - virtual int getHeight() const = 0; + int getWidth() const { return width; } + int getHeight() const { return height; } // Callers take ownership of the returned memory and must call delete [] on it themselves. - virtual ArrayRef getRow(int y, ArrayRef row) = 0; - virtual ArrayRef getMatrix() = 0; + virtual ArrayRef getRow(int y, ArrayRef row) const = 0; + virtual ArrayRef getMatrix() const = 0; virtual bool isCropSupported() const; virtual Ref crop(int left, int top, int width, int height); virtual bool isRotateSupported() const; + + static Ref invert(Ref const&); + virtual Ref rotateCounterClockwise(); - operator std::string (); // should be const but don't want to make sure a - // large breaking change right now + operator std::string () const; }; +} + #endif /* LUMINANCESOURCE_H_ */ diff --git a/cpp/core/src/zxing/ZXing.h b/cpp/core/src/zxing/ZXing.h index a8fbfb925..9c952bc6f 100644 --- a/cpp/core/src/zxing/ZXing.h +++ b/cpp/core/src/zxing/ZXing.h @@ -23,6 +23,12 @@ #define ZXING_DEBUG 0 #endif +namespace zxing { + +typedef char byte; + +} + #if ZXING_DEBUG #include @@ -40,10 +46,8 @@ using std::ostream; #include namespace zxing { - class DebugTimer; -} -class zxing::DebugTimer { +class DebugTimer { public: DebugTimer(char const* string_) : chars(string_) { gettimeofday(&start, 0); @@ -80,6 +84,8 @@ private: struct timeval start; }; +} + #define ZXING_TIME(string) DebugTimer __timer__ (string) #define ZXING_TIME_MARK(string) __timer__.mark(string) diff --git a/cpp/core/src/zxing/common/Array.h b/cpp/core/src/zxing/common/Array.h index 99728c243..8aefdf900 100644 --- a/cpp/core/src/zxing/common/Array.h +++ b/cpp/core/src/zxing/common/Array.h @@ -60,7 +60,7 @@ public: values_ = array; return *this; } - T operator[](int i) const { + T const& operator[](int i) const { return values_[i]; } T& operator[](int i) { @@ -117,7 +117,7 @@ public: array_ = 0; } - T operator[](int i) const { + T const& operator[](int i) const { return (*array_)[i]; } T& operator[](int i) { diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp index b5beb8010..f5806b65b 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp @@ -32,17 +32,17 @@ GreyscaleLuminanceSource(ArrayRef greyData, int dataWidth, int dataHeight, int left, int top, int width, int height) - : greyData_(greyData), - dataWidth_(dataWidth), dataHeight_(dataHeight), - left_(left), top_(top), - width_(width), height_(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 GreyscaleLuminanceSource::getRow(int y, ArrayRef row) { +ArrayRef GreyscaleLuminanceSource::getRow(int y, ArrayRef row) const { if (y < 0 || y >= this->getHeight()) { throw IllegalArgumentException("Requested row is outside the image."); } @@ -56,14 +56,14 @@ ArrayRef GreyscaleLuminanceSource::getRow(int y, ArrayRef row) { return row; } -ArrayRef GreyscaleLuminanceSource::getMatrix() { - int size = width_ * height_; +ArrayRef GreyscaleLuminanceSource::getMatrix() const { + int size = getWidth() * getHeight(); ArrayRef result (size); - if (left_ == 0 && top_ == 0 && dataWidth_ == width_ && dataHeight_ == height_) { + if (left_ == 0 && top_ == 0 && dataWidth_ == getWidth() && dataHeight_ == getHeight()) { memcpy(&result[0], &greyData_[0], size); } else { - for (int row = 0; row < height_; row++) { - memcpy(&result[row * width_], &greyData_[(top_ + row) * dataWidth_ + left_], width_); + for (int row = 0; row < getHeight(); row++) { + memcpy(&result[row * getWidth()], &greyData_[(top_ + row) * dataWidth_ + left_], getWidth()); } } return result; @@ -77,35 +77,7 @@ Ref GreyscaleLuminanceSource::rotateCounterClockwise() { Ref result ( new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_, dataHeight_, - top_, left_, height_, width_) + top_, left_, getHeight(), getWidth()) ); - - /* - // testing code ... remove when we trust everything. - // NB: very slow, so don't forget and leave on. - for(int r = top_; r < top_+height_; ++r) { - for(int c = left_; c < left_+width_; ++c) { - int rot_r = r; - int rot_c = c; - // transpose - int tmp = rot_r; - rot_r = rot_c; - rot_c = tmp; - // flip vert - rot_r = width_-rot_r-1; - if (getRow(r, ArrayRef())[c] != - result->getRow(rot_r, ArrayRef())[rot_c]) { - using namespace std; - cerr << r << "," << c << " " - << rot_r << "," << rot_c << " " - << (0+getRow(r, ArrayRef())[c]) << " " - << (0+result->getRow(rot_r, ArrayRef())[rot_c]) << " " - << endl; - abort(); - } - } - } - */ - return result; } diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h index 4b78b01fe..d548164a6 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h @@ -23,41 +23,32 @@ #include namespace zxing { - class GreyscaleLuminanceSource; -} -class zxing::GreyscaleLuminanceSource : public LuminanceSource { +class GreyscaleLuminanceSource : public LuminanceSource { private: + typedef LuminanceSource Super; ArrayRef greyData_; - int dataWidth_; - int dataHeight_; - int left_; - int top_; - int width_; - int height_; + const int dataWidth_; + const int dataHeight_; + const int left_; + const int top_; public: GreyscaleLuminanceSource(ArrayRef greyData, int dataWidth, int dataHeight, int left, int top, int width, int height); - ArrayRef getRow(int y, ArrayRef row); - ArrayRef getMatrix(); + ArrayRef getRow(int y, ArrayRef row) const; + ArrayRef getMatrix() const; bool isRotateSupported() const { return true; } - int getWidth() const { - return width_; - } - - int getHeight() const { - return height_; - } - Ref rotateCounterClockwise(); }; +} + #endif diff --git a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp index fceca8e89..71a4fc6f3 100644 --- a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp @@ -32,11 +32,11 @@ GreyscaleRotatedLuminanceSource:: GreyscaleRotatedLuminanceSource(ArrayRef greyData, int dataWidth, int dataHeight, int left, int top, - int width, int height) - : greyData_(greyData), - dataWidth_(dataWidth), - left_(left), top_(top), width_(width), - height_(height) { + int width, int height) + : Super(width, height), + greyData_(greyData), + dataWidth_(dataWidth), + left_(left), top_(top) { // Intentionally comparing to the opposite dimension since we're rotated. if (left + width > dataHeight || top + height > dataWidth) { throw IllegalArgumentException("Crop rectangle does not fit within image data."); @@ -45,34 +45,34 @@ GreyscaleRotatedLuminanceSource(ArrayRef greyData, // The API asks for rows, but we're rotated, so we return columns. ArrayRef -GreyscaleRotatedLuminanceSource::getRow(int y, ArrayRef row) { +GreyscaleRotatedLuminanceSource::getRow(int y, ArrayRef row) const { if (y < 0 || y >= getHeight()) { throw IllegalArgumentException("Requested row is outside the image."); } - if (!row || row.size() < width_) { - row = ArrayRef(width_); + if (!row || row.size() < getWidth()) { + row = ArrayRef(getWidth()); } int offset = (left_ * dataWidth_) + (dataWidth_ - 1 - (y + top_)); using namespace std; if (false) { cerr << offset << " = " << top_ << " " << left_ << " " - << height_ << " " << width_ << " " + << getHeight() << " " << getWidth() << " " << y << endl; } - for (int x = 0; x < width_; x++) { + for (int x = 0; x < getWidth(); x++) { row[x] = greyData_[offset]; offset += dataWidth_; } return row; } -ArrayRef GreyscaleRotatedLuminanceSource::getMatrix() { - ArrayRef result (width_ * height_); - for (int y = 0; y < height_; y++) { - char* row = &result[y * width_]; +ArrayRef GreyscaleRotatedLuminanceSource::getMatrix() const { + ArrayRef result (getWidth() * getHeight()); + for (int y = 0; y < getHeight(); y++) { + char* row = &result[y * getWidth()]; int offset = (left_ * dataWidth_) + (dataWidth_ - 1 - (y + top_)); - for (int x = 0; x < width_; x++) { + for (int x = 0; x < getWidth(); x++) { row[x] = greyData_[offset]; offset += dataWidth_; } diff --git a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h index 292159ada..77d6ffbf6 100644 --- a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h +++ b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h @@ -24,37 +24,27 @@ #include namespace zxing { - class GreyscaleRotatedLuminanceSource; -} -class zxing::GreyscaleRotatedLuminanceSource : public LuminanceSource { +class GreyscaleRotatedLuminanceSource : public LuminanceSource { private: + typedef LuminanceSource Super; ArrayRef greyData_; const int dataWidth_; const int left_; const int top_; - const int width_; - const int height_; public: GreyscaleRotatedLuminanceSource(ArrayRef greyData, int dataWidth, int dataHeight, int left, int top, int width, int height); - ArrayRef getRow(int y, ArrayRef row); - ArrayRef getMatrix(); + ArrayRef getRow(int y, ArrayRef row) const; + ArrayRef getMatrix() const; bool isRotateSupported() const { return false; } - - int getWidth() const { - return width_; - } - - int getHeight() const { - return height_; - } - }; +} + #endif diff --git a/cpp/magick/src/MagickBitmapSource.cpp b/cpp/magick/src/MagickBitmapSource.cpp index 0d913e0eb..c76f18311 100644 --- a/cpp/magick/src/MagickBitmapSource.cpp +++ b/cpp/magick/src/MagickBitmapSource.cpp @@ -26,27 +26,18 @@ using zxing::Ref; using zxing::LuminanceSource; using zxing::MagickBitmapSource; -MagickBitmapSource::MagickBitmapSource(Image& image) : image_(image) { - width = image.columns(); - height = image.rows(); -} +MagickBitmapSource::MagickBitmapSource(Image& image) + : Super(image.columns(), image.rows()), image_(image) {} MagickBitmapSource::~MagickBitmapSource() { } -int MagickBitmapSource::getWidth() const { - return width; -} +ArrayRef MagickBitmapSource::getRow(int y, ArrayRef row) const { + const int width = getWidth(); -int MagickBitmapSource::getHeight() const { - return height; -} - -ArrayRef MagickBitmapSource::getRow(int y, ArrayRef row) { const Magick::PixelPacket* pixel_cache = - image_.getConstPixels(0, y, width, 1); + image_.getConstPixels(0, y, width, 1); - int width = getWidth(); if (!row || row->size() < width) { row =ArrayRef(width); } @@ -62,10 +53,10 @@ ArrayRef MagickBitmapSource::getRow(int y, ArrayRef row) { } /** This is a more efficient implementation. */ -ArrayRef MagickBitmapSource::getMatrix() { +ArrayRef MagickBitmapSource::getMatrix() const { + const int width = getWidth(); + const int height = getHeight(); const Magick::PixelPacket* pixel_cache = image_.getConstPixels(0, 0, width, height); - int width = getWidth(); - int height = getHeight(); ArrayRef matrix = ArrayRef(width*height); char* m = &matrix[0]; const Magick::PixelPacket* p = pixel_cache; diff --git a/cpp/magick/src/MagickBitmapSource.h b/cpp/magick/src/MagickBitmapSource.h index dd7fd3a97..a80ea7544 100644 --- a/cpp/magick/src/MagickBitmapSource.h +++ b/cpp/magick/src/MagickBitmapSource.h @@ -21,28 +21,26 @@ #include namespace zxing { - class MagickBitmapSource; -} -class zxing::MagickBitmapSource : public LuminanceSource { +class MagickBitmapSource : public LuminanceSource { private: + typedef LuminanceSource Super; Magick::Image image_; - int width; - int height; public: MagickBitmapSource(Magick::Image& image); ~MagickBitmapSource(); - int getWidth() const; - int getHeight() const; - ArrayRef getRow(int y, ArrayRef row); - ArrayRef getMatrix(); + ArrayRef getRow(int y, ArrayRef row) const; + ArrayRef getMatrix() const; + bool isCropSupported() const; Ref crop(int left, int top, int width, int height); bool isRotateSupported() const; Ref rotateCounterClockwise(); }; +} + #endif /* MAGICKMONOCHROMEBITMAPSOURCE_H_ */