c++ changes for r2542

git-svn-id: https://zxing.googlecode.com/svn/trunk@2614 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2013-04-01 06:01:26 +00:00
parent 1b8b0113c7
commit 1d366b108b
6 changed files with 105 additions and 259 deletions

View file

@ -23,11 +23,6 @@
#include <vector> #include <vector>
#ifdef DEBUG_COUNTING
#include <iostream>
#include <typeinfo>
#endif
#include <zxing/common/Counted.h> #include <zxing/common/Counted.h>
namespace zxing { namespace zxing {
@ -58,23 +53,11 @@ public:
virtual ~Array() { virtual ~Array() {
} }
Array<T>& operator=(const Array<T> &other) { Array<T>& operator=(const Array<T> &other) {
#ifdef DEBUG_COUNTING
cout << "assigning values from Array " << &other << " to this Array " << this << ", ";
#endif
values_ = other.values_; values_ = other.values_;
#ifdef DEBUG_COUNTING
cout << "new size = " << values_.size() << "\n";
#endif
return *this; return *this;
} }
Array<T>& operator=(const std::vector<T> &array) { Array<T>& operator=(const std::vector<T> &array) {
#ifdef DEBUG_COUNTING
cout << "assigning values from Array " << &array << " to this Array " << this << ", ";
#endif
values_ = array; values_ = array;
#ifdef DEBUG_COUNTING
cout << "new size = " << values_.size() << "\n";
#endif
return *this; return *this;
} }
T operator[](int i) const { T operator[](int i) const {
@ -103,53 +86,31 @@ public:
Array<T> *array_; Array<T> *array_;
ArrayRef() : ArrayRef() :
array_(0) { array_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating empty ArrayRef " << this << "\n";
#endif
} }
explicit ArrayRef(int n) : explicit ArrayRef(int n) :
array_(0) { array_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating ArrayRef " << this << "with size " << n << "\n";
#endif
reset(new Array<T> (n)); reset(new Array<T> (n));
} }
ArrayRef(T *ts, int n) : ArrayRef(T *ts, int n) :
array_(0) { array_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating ArrayRef " << this << "with " << n << " elements at " << (void *)ts << "\n";
#endif
reset(new Array<T> (ts, n)); reset(new Array<T> (ts, n));
} }
ArrayRef(Array<T> *a) : ArrayRef(Array<T> *a) :
array_(0) { array_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating ArrayRef " << this << " from pointer:\n";
#endif
reset(a); reset(a);
} }
ArrayRef(const ArrayRef &other) : ArrayRef(const ArrayRef &other) :
Counted(), array_(0) { Counted(), array_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
#endif
reset(other.array_); reset(other.array_);
} }
template<class Y> template<class Y>
ArrayRef(const ArrayRef<Y> &other) : ArrayRef(const ArrayRef<Y> &other) :
array_(0) { array_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
#endif
reset(static_cast<const Array<T> *>(other.array_)); reset(static_cast<const Array<T> *>(other.array_));
} }
~ArrayRef() { ~ArrayRef() {
#ifdef DEBUG_COUNTING
cout << "destroying ArrayRef " << this << " with " << (array_ ? typeid(*array_).name() : "NULL") << " "
<< array_ << "\n";
#endif
if (array_) { if (array_) {
array_->release(); array_->release();
} }
@ -167,10 +128,6 @@ public:
} }
void reset(Array<T> *a) { void reset(Array<T> *a) {
#ifdef DEBUG_COUNTING
cout << "resetting ArrayRef " << this << " from " << (array_ ? typeid(*array_).name() : "NULL") << " "
<< array_ << " to " << (a ? typeid(*a).name() : "NULL") << " " << a << "\n";
#endif
if (a) { if (a) {
a->retain(); a->retain();
} }

View file

@ -18,14 +18,8 @@
* limitations under the License. * limitations under the License.
*/ */
//#define DEBUG_COUNTING
#include <iostream> #include <iostream>
#ifdef DEBUG_COUNTING
#include <typeinfo>
#endif
namespace zxing { namespace zxing {
/* base class for reference-counted objects */ /* base class for reference-counted objects */
@ -35,43 +29,19 @@ private:
public: public:
Counted() : Counted() :
count_(0) { count_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating " << typeid(*this).name() << " " << this <<
" @ " << count_ << "\n";
#endif
} }
virtual ~Counted() { virtual ~Counted() {
} }
Counted *retain() { Counted *retain() {
#ifdef DEBUG_COUNTING
cout << "retaining " << typeid(*this).name() << " " << this <<
" @ " << count_;
#endif
count_++; count_++;
#ifdef DEBUG_COUNTING
cout << "->" << count_ << "\n";
#endif
return this; return this;
} }
void release() { void release() {
#ifdef DEBUG_COUNTING
cout << "releasing " << typeid(*this).name() << " " << this <<
" @ " << count_;
#endif
if (count_ == 0 || count_ == 54321) { if (count_ == 0 || count_ == 54321) {
#ifdef DEBUG_COUNTING
cout << "\nOverreleasing already-deleted object " << this << "!!!\n";
#endif
throw 4711; throw 4711;
} }
count_--; count_--;
#ifdef DEBUG_COUNTING
cout << "->" << count_ << "\n";
#endif
if (count_ == 0) { if (count_ == 0) {
#ifdef DEBUG_COUNTING
cout << "deleting " << typeid(*this).name() << " " << this << "\n";
#endif
count_ = 0xDEADF001; count_ = 0xDEADF001;
delete this; delete this;
} }
@ -91,44 +61,26 @@ public:
T *object_; T *object_;
explicit Ref(T *o = 0) : explicit Ref(T *o = 0) :
object_(0) { object_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating Ref " << this << " from pointer" << o << "\n";
#endif
reset(o); reset(o);
} }
Ref(const Ref &other) : Ref(const Ref &other) :
object_(0) { object_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating Ref " << this << " from Ref " << &other << "\n";
#endif
reset(other.object_); reset(other.object_);
} }
template<class Y> template<class Y>
Ref(const Ref<Y> &other) : Ref(const Ref<Y> &other) :
object_(0) { object_(0) {
#ifdef DEBUG_COUNTING
cout << "instantiating Ref " << this << " from reference\n";
#endif
reset(other.object_); reset(other.object_);
} }
~Ref() { ~Ref() {
#ifdef DEBUG_COUNTING
cout << "destroying Ref " << this << " with " <<
(object_ ? typeid(*object_).name() : "NULL") << " " << object_ << "\n";
#endif
if (object_) { if (object_) {
object_->release(); object_->release();
} }
} }
void reset(T *o) { void reset(T *o) {
#ifdef DEBUG_COUNTING
cout << "resetting Ref " << this << " from " <<
(object_ ? typeid(*object_).name() : "NULL") << " " << object_ <<
" to " << (o ? typeid(*o).name() : "NULL") << " " << o << "\n";
#endif
if (o) { if (o) {
o->retain(); o->retain();
} }

View file

@ -34,45 +34,15 @@ DataMatrixReader::DataMatrixReader() :
Ref<Result> DataMatrixReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) { Ref<Result> DataMatrixReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
(void)hints; (void)hints;
#ifdef DEBUG
cout << "decoding image " << image.object_ << ":\n" << flush;
#endif
Detector detector(image->getBlackMatrix()); Detector detector(image->getBlackMatrix());
#ifdef DEBUG
cout << "(1) created detector " << &detector << "\n" << flush;
#endif
Ref<DetectorResult> detectorResult(detector.detect()); Ref<DetectorResult> detectorResult(detector.detect());
#ifdef DEBUG
cout << "(2) detected, have detectorResult " << detectorResult.object_ << "\n" << flush;
#endif
ArrayRef< Ref<ResultPoint> > points(detectorResult->getPoints()); ArrayRef< Ref<ResultPoint> > points(detectorResult->getPoints());
#ifdef DEBUG
cout << "(3) extracted points " << &points << "\n" << flush;
cout << "found " << points.size() << " points:\n";
for (size_t i = 0; i < points.size(); i++) {
cout << " " << points[i]->getX() << "," << points[i]->getY() << "\n";
}
cout << "bits:\n";
cout << *(detectorResult->getBits()) << "\n";
#endif
Ref<DecoderResult> decoderResult(decoder_.decode(detectorResult->getBits())); Ref<DecoderResult> decoderResult(decoder_.decode(detectorResult->getBits()));
#ifdef DEBUG
cout << "(4) decoded, have decoderResult " << decoderResult.object_ << "\n" << flush;
#endif
Ref<Result> result( Ref<Result> result(
new Result(decoderResult->getText(), decoderResult->getRawBytes(), points, BarcodeFormat::DATA_MATRIX)); new Result(decoderResult->getText(), decoderResult->getRawBytes(), points, BarcodeFormat::DATA_MATRIX));
#ifdef DEBUG
cout << "(5) created result " << result.object_ << ", returning\n" << flush;
#endif
return result; return result;
} }

View file

@ -33,45 +33,12 @@ namespace zxing {
} }
//TODO: see if any of the other files in the qrcode tree need tryHarder //TODO: see if any of the other files in the qrcode tree need tryHarder
Ref<Result> QRCodeReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) { Ref<Result> QRCodeReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
#ifdef DEBUG
cout << "decoding image " << image.object_ << ":\n" << flush;
#endif
Detector detector(image->getBlackMatrix()); Detector detector(image->getBlackMatrix());
#ifdef DEBUG
cout << "(1) created detector " << &detector << "\n" << flush;
#endif
Ref<DetectorResult> detectorResult(detector.detect(hints)); Ref<DetectorResult> detectorResult(detector.detect(hints));
#ifdef DEBUG
cout << "(2) detected, have detectorResult " << detectorResult.object_ << "\n" << flush;
#endif
ArrayRef< Ref<ResultPoint> > points (detectorResult->getPoints()); ArrayRef< Ref<ResultPoint> > points (detectorResult->getPoints());
#ifdef DEBUG
cout << "(3) extracted points " << &points << "\n" << flush;
cout << "found " << points.size() << " points:\n";
for (size_t i = 0; i < points.size(); i++) {
cout << " " << points[i]->getX() << "," << points[i]->getY() << "\n";
}
cout << "bits:\n";
cout << *(detectorResult->getBits()) << "\n";
#endif
Ref<DecoderResult> decoderResult(decoder_.decode(detectorResult->getBits())); Ref<DecoderResult> decoderResult(decoder_.decode(detectorResult->getBits()));
#ifdef DEBUG
cout << "(4) decoded, have decoderResult " << decoderResult.object_ << "\n" << flush;
#endif
Ref<Result> result( Ref<Result> result(
new Result(decoderResult->getText(), decoderResult->getRawBytes(), points, BarcodeFormat::QR_CODE)); new Result(decoderResult->getText(), decoderResult->getRawBytes(), points, BarcodeFormat::QR_CODE));
#ifdef DEBUG
cout << "(5) created result " << result.object_ << ", returning\n" << flush;
#endif
return result; return result;
} }

View file

@ -196,12 +196,6 @@ Ref<BitMatrix> Version::buildFunctionPattern() {
functionPattern->setRegion(0, dimension - 11, 6, 3); functionPattern->setRegion(0, dimension - 11, 6, 3);
} }
//#ifdef DEBUG
// cout << "version " << versionNumber_ << " built function pattern:\n";
// cout << *functionPattern;
//#endif
return functionPattern; return functionPattern;
} }

View file

@ -44,11 +44,11 @@ using namespace zxing::qrcode;
using namespace zxing::common; using namespace zxing::common;
const char DecodedBitStreamParser::ALPHANUMERIC_CHARS[] = const char DecodedBitStreamParser::ALPHANUMERIC_CHARS[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':' 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':'
}; };
namespace {int GB2312_SUBSET = 1;} namespace {int GB2312_SUBSET = 1;}
@ -139,7 +139,7 @@ void DecodedBitStreamParser::decodeHanziSegment(Ref<BitSource> bits_,
} }
delete [] buffer; delete [] buffer;
} }
void DecodedBitStreamParser::decodeKanjiSegment(Ref<BitSource> bits, std::string &result, int count) { void DecodedBitStreamParser::decodeKanjiSegment(Ref<BitSource> bits, std::string &result, int count) {
// Each character will require 2 bytes. Read the characters as 2-byte pairs // Each character will require 2 bytes. Read the characters as 2-byte pairs
@ -342,9 +342,11 @@ DecodedBitStreamParser::decode(ArrayRef<char> bytes,
Ref<BitSource> bits_ (new BitSource(bytes)); Ref<BitSource> bits_ (new BitSource(bytes));
BitSource& bits (*bits_); BitSource& bits (*bits_);
string result; string result;
result.reserve(50);
ArrayRef< ArrayRef<char> > byteSegments (0);
try {
CharacterSetECI* currentCharacterSetECI = 0; CharacterSetECI* currentCharacterSetECI = 0;
bool fc1InEffect = false; bool fc1InEffect = false;
ArrayRef< ArrayRef<char> > byteSegments (0);
Mode* mode = 0; Mode* mode = 0;
do { do {
// While still another segment to read... // While still another segment to read...
@ -405,6 +407,10 @@ DecodedBitStreamParser::decode(ArrayRef<char> bytes,
} }
} }
} while (mode != &Mode::TERMINATOR); } while (mode != &Mode::TERMINATOR);
} catch (IllegalArgumentException const& iae) {
// from readBits() calls
throw FormatException();
}
return Ref<DecoderResult>(new DecoderResult(bytes, Ref<String>(new String(result)), byteSegments, (string)ecLevel)); return Ref<DecoderResult>(new DecoderResult(bytes, Ref<String>(new String(result)), byteSegments, (string)ecLevel));
} }