From 12387c2f8149c4410aa97798f1c4fd1557e9ca2c Mon Sep 17 00:00:00 2001 From: "smparkes@smparkes.net" Date: Sat, 6 Apr 2013 21:30:17 +0000 Subject: [PATCH] const-ness and iphone/objc fixes git-svn-id: https://zxing.googlecode.com/svn/trunk@2628 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/common/GreyscaleLuminanceSource.cpp | 11 +-- .../zxing/common/GreyscaleLuminanceSource.h | 3 +- .../common/GreyscaleRotatedLuminanceSource.h | 4 - cpp/zxing-cpp.xcodeproj/project.pbxproj | 4 + .../ZXingWidget.xcodeproj/project.pbxproj | 8 ++ objc/src/ZXing/CGImageLuminanceSource.mm | 81 +++++++++---------- objc/src/ZXing/ZXCGImageLuminanceSource.h | 28 ++----- objc/src/ZXing/ZXCGImageLuminanceSource.mm | 10 +-- objc/src/ZXing/ZXCapture.mm | 14 ++-- 9 files changed, 70 insertions(+), 93 deletions(-) diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp index f5806b65b..949d22e83 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp @@ -69,15 +69,12 @@ ArrayRef GreyscaleLuminanceSource::getMatrix() const { return result; } - extern "C" void abort(); - -Ref GreyscaleLuminanceSource::rotateCounterClockwise() { +Ref GreyscaleLuminanceSource::rotateCounterClockwise() const { // Intentionally flip the left, top, width, and height arguments as // needed. dataWidth and dataHeight are always kept unrotated. Ref result ( - new GreyscaleRotatedLuminanceSource(greyData_, - dataWidth_, dataHeight_, - top_, left_, getHeight(), getWidth()) - ); + new GreyscaleRotatedLuminanceSource(greyData_, + dataWidth_, dataHeight_, + top_, left_, getHeight(), getWidth())); return result; } diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h index d548164a6..8275323b6 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h @@ -45,8 +45,7 @@ public: return true; } - Ref rotateCounterClockwise(); - + Ref rotateCounterClockwise() const; }; } diff --git a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h index 77d6ffbf6..6af60f92f 100644 --- a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h +++ b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h @@ -39,10 +39,6 @@ public: ArrayRef getRow(int y, ArrayRef row) const; ArrayRef getMatrix() const; - - bool isRotateSupported() const { - return false; - } }; } diff --git a/cpp/zxing-cpp.xcodeproj/project.pbxproj b/cpp/zxing-cpp.xcodeproj/project.pbxproj index 68c5d5c5b..30692fc8d 100644 --- a/cpp/zxing-cpp.xcodeproj/project.pbxproj +++ b/cpp/zxing-cpp.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 3B5A493617091826004E112F /* CodaBarReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B5A493217091826004E112F /* CodaBarReader.h */; }; 3B5A493717091826004E112F /* Code93Reader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A493317091826004E112F /* Code93Reader.cpp */; }; 3B5A493817091826004E112F /* Code93Reader.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B5A493417091826004E112F /* Code93Reader.h */; }; + 3B6AA6711710C83200DF84C0 /* InvertedLuminanceSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B6AA6701710C83200DF84C0 /* InvertedLuminanceSource.cpp */; }; 3B83E992158E66C600A3B31F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B83E991158E66C600A3B31F /* Foundation.framework */; }; 3B83EA63158E679000A3B31F /* AztecDetectorResult.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B83E9A4158E679000A3B31F /* AztecDetectorResult.cpp */; }; 3B83EA64158E679000A3B31F /* AztecDetectorResult.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B83E9A5158E679000A3B31F /* AztecDetectorResult.h */; }; @@ -196,6 +197,7 @@ 3B5A493217091826004E112F /* CodaBarReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CodaBarReader.h; sourceTree = ""; }; 3B5A493317091826004E112F /* Code93Reader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Code93Reader.cpp; sourceTree = ""; }; 3B5A493417091826004E112F /* Code93Reader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Code93Reader.h; sourceTree = ""; }; + 3B6AA6701710C83200DF84C0 /* InvertedLuminanceSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InvertedLuminanceSource.cpp; sourceTree = ""; }; 3B83E98E158E66C600A3B31F /* libzxing-cpp.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libzxing-cpp.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 3B83E991158E66C600A3B31F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 3B83E9A4158E679000A3B31F /* AztecDetectorResult.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AztecDetectorResult.cpp; sourceTree = ""; }; @@ -419,6 +421,7 @@ 3B83E9A2158E679000A3B31F /* zxing */ = { isa = PBXGroup; children = ( + 3B6AA6701710C83200DF84C0 /* InvertedLuminanceSource.cpp */, 3B5A492D17091807004E112F /* ChecksumException.cpp */, 3B5A492E17091807004E112F /* ChecksumException.h */, 3B83E9A3158E679000A3B31F /* aztec */, @@ -980,6 +983,7 @@ 3B5A492F17091807004E112F /* ChecksumException.cpp in Sources */, 3B5A493517091826004E112F /* CodaBarReader.cpp in Sources */, 3B5A493717091826004E112F /* Code93Reader.cpp in Sources */, + 3B6AA6711710C83200DF84C0 /* InvertedLuminanceSource.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iphone/ZXingWidget/ZXingWidget.xcodeproj/project.pbxproj b/iphone/ZXingWidget/ZXingWidget.xcodeproj/project.pbxproj index b7c777d73..4e48a5f48 100644 --- a/iphone/ZXingWidget/ZXingWidget.xcodeproj/project.pbxproj +++ b/iphone/ZXingWidget/ZXingWidget.xcodeproj/project.pbxproj @@ -141,6 +141,8 @@ 3B27C5E2144735E6005157C4 /* CharacterSetECI.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B27C5DC144735E6005157C4 /* CharacterSetECI.h */; }; 3B27C5E5144735E6005157C4 /* StringUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B27C5DF144735E6005157C4 /* StringUtils.cpp */; }; 3B27C5E6144735E6005157C4 /* StringUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B27C5E0144735E6005157C4 /* StringUtils.h */; }; + 3B6AA66C1710C34600DF84C0 /* InvertedLuminanceSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B6AA66A1710C34600DF84C0 /* InvertedLuminanceSource.cpp */; }; + 3B6AA66D1710C34600DF84C0 /* InvertedLuminanceSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B6AA66B1710C34600DF84C0 /* InvertedLuminanceSource.h */; }; 3B6C805717090ED300D22AB2 /* CodaBarReader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B6C805317090ED300D22AB2 /* CodaBarReader.cpp */; }; 3B6C805817090ED300D22AB2 /* CodaBarReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B6C805417090ED300D22AB2 /* CodaBarReader.h */; }; 3B6C805917090ED300D22AB2 /* Code93Reader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B6C805517090ED300D22AB2 /* Code93Reader.cpp */; }; @@ -435,6 +437,8 @@ 3B27C5DC144735E6005157C4 /* CharacterSetECI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CharacterSetECI.h; sourceTree = ""; }; 3B27C5DF144735E6005157C4 /* StringUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringUtils.cpp; sourceTree = ""; }; 3B27C5E0144735E6005157C4 /* StringUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringUtils.h; sourceTree = ""; }; + 3B6AA66A1710C34600DF84C0 /* InvertedLuminanceSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InvertedLuminanceSource.cpp; sourceTree = ""; }; + 3B6AA66B1710C34600DF84C0 /* InvertedLuminanceSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InvertedLuminanceSource.h; sourceTree = ""; }; 3B6C805317090ED300D22AB2 /* CodaBarReader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CodaBarReader.cpp; sourceTree = ""; }; 3B6C805417090ED300D22AB2 /* CodaBarReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CodaBarReader.h; sourceTree = ""; }; 3B6C805517090ED300D22AB2 /* Code93Reader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Code93Reader.cpp; sourceTree = ""; }; @@ -754,6 +758,8 @@ E53458B411987396000CB77F /* zxing */ = { isa = PBXGroup; children = ( + 3B6AA66A1710C34600DF84C0 /* InvertedLuminanceSource.cpp */, + 3B6AA66B1710C34600DF84C0 /* InvertedLuminanceSource.h */, 3B6C805B17090EFC00D22AB2 /* ChecksumException.cpp */, 3B6C805C17090EFC00D22AB2 /* ChecksumException.h */, 3B248F8314EF3A980065F0DA /* aztec */, @@ -1226,6 +1232,7 @@ 3B6C805817090ED300D22AB2 /* CodaBarReader.h in Headers */, 3B6C805A17090ED300D22AB2 /* Code93Reader.h in Headers */, 3B6C805E17090EFC00D22AB2 /* ChecksumException.h in Headers */, + 3B6AA66D1710C34600DF84C0 /* InvertedLuminanceSource.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1474,6 +1481,7 @@ 3B6C805717090ED300D22AB2 /* CodaBarReader.cpp in Sources */, 3B6C805917090ED300D22AB2 /* Code93Reader.cpp in Sources */, 3B6C805D17090EFC00D22AB2 /* ChecksumException.cpp in Sources */, + 3B6AA66C1710C34600DF84C0 /* InvertedLuminanceSource.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/objc/src/ZXing/CGImageLuminanceSource.mm b/objc/src/ZXing/CGImageLuminanceSource.mm index 588a18892..30fc8c448 100644 --- a/objc/src/ZXing/CGImageLuminanceSource.mm +++ b/objc/src/ZXing/CGImageLuminanceSource.mm @@ -92,53 +92,52 @@ CGImageRef CGImageLuminanceSource::createImageFromBuffer (int)CVPixelBufferGetHeight(buffer)); } -CGImageLuminanceSource::CGImageLuminanceSource(CVPixelBufferRef buffer) -{ +CGImageLuminanceSource* CGImageLuminanceSource::create(CVPixelBufferRef buffer) { + // Will leak if there's an exception ... CGImageRef image = createImageFromBuffer(buffer); - init(image); + CGImageLuminanceSource* source = new CGImageLuminanceSource(image); CGImageRelease(image); + return source; } -CGImageLuminanceSource::CGImageLuminanceSource - (CVPixelBufferRef buffer, int left, int top, int width, int height) -{ +CGImageLuminanceSource::CGImageLuminanceSource(CVPixelBufferRef buffer, + int left, int top, int width, int height) +: Super(width, height) { CGImageRef image = createImageFromBuffer(buffer, left, top, width, height); init(image); CGImageRelease(image); } -CGImageLuminanceSource::CGImageLuminanceSource(CGImageRef cgimage) -{ +CGImageLuminanceSource::CGImageLuminanceSource(CGImageRef cgimage) +: Super((int)CGImageGetWidth(cgimage), (int)CGImageGetHeight(cgimage)) { init(cgimage); } -CGImageLuminanceSource::CGImageLuminanceSource - (CGImageRef cgimage, int left, int top, int width, int height) -{ - init(cgimage, left, top, width, height); +CGImageLuminanceSource::CGImageLuminanceSource(CGImageRef cgimage, + int left, + int top, + int width, + int height) +: Super(width, height) { + init(cgimage, left, top); } void -CGImageLuminanceSource::init(CGImageRef cgimage) -{ - init(cgimage, - 0, 0, (int)CGImageGetWidth(cgimage), (int)CGImageGetHeight(cgimage)); +CGImageLuminanceSource::init(CGImageRef cgimage) { + init(cgimage, 0, 0); } void -CGImageLuminanceSource::init - (CGImageRef cgimage, int left, int top, int width, int height) { +CGImageLuminanceSource::init(CGImageRef cgimage, int left, int top) { data_ = 0; image_ = cgimage; left_ = left; top_ = top; - width_ = width; - height_ = height; dataWidth_ = (int)CGImageGetWidth(image_); dataHeight_ = (int)CGImageGetHeight(image_); - if (left_ + width_ > dataWidth_ || - top_ + height_ > dataHeight_ || + if (left_ + getWidth() > dataWidth_ || + top_ + getHeight() > dataHeight_ || top_ < 0 || left_ < 0) { throw IllegalArgumentException @@ -155,28 +154,28 @@ CGImageLuminanceSource::init CGColorSpaceRef gray = CGColorSpaceCreateDeviceGray(); CGContextRef ctx = CGBitmapContextCreate(0, - width, - height, + getWidth(), + getHeight(), 8, - width, + getWidth(), gray, kCGImageAlphaNone); CGColorSpaceRelease(gray); if (top || left) { - CGContextClipToRect(ctx, CGRectMake(0, 0, width, height)); + CGContextClipToRect(ctx, CGRectMake(0, 0, getWidth(), getHeight())); } - CGContextDrawImage(ctx, CGRectMake(-left, -top, width, height), image_); + CGContextDrawImage(ctx, CGRectMake(-left, -top, getWidth(), getHeight()), image_); image_ = CGBitmapContextCreateImage(ctx); - bytesPerRow_ = width; + bytesPerRow_ = getWidth(); top_ = 0; left_ = 0; - dataWidth_ = width; - dataHeight_ = height; + dataWidth_ = getWidth(); + dataHeight_ = getHeight(); CGContextRelease(ctx); } else { @@ -196,7 +195,7 @@ CGImageLuminanceSource::~CGImageLuminanceSource() { } } -ArrayRef CGImageLuminanceSource::getRow(int y, ArrayRef row) { +ArrayRef CGImageLuminanceSource::getRow(int y, ArrayRef row) const { if (y < 0 || y >= this->getHeight()) { ostringstream msg; msg << "Requested row is outside the image: " << y; @@ -205,7 +204,7 @@ ArrayRef CGImageLuminanceSource::getRow(int y, ArrayRef row) { int width = getWidth(); // TODO(flyashi): determine if row has enough size. if (!row) { - row = ArrayRef(width_); + row = ArrayRef(getWidth()); } int offset = (y + top_) * dataWidth_ + left_; char* v = &row->values()[0]; @@ -213,26 +212,22 @@ ArrayRef CGImageLuminanceSource::getRow(int y, ArrayRef row) { return row; } -ArrayRef CGImageLuminanceSource::getMatrix() { - int size = width_ * height_; +ArrayRef CGImageLuminanceSource::getMatrix() const { + int size = getWidth() * getHeight(); ArrayRef result (size); if (left_ == 0 && top_ == 0 && - dataWidth_ == width_ && - dataHeight_ == height_) { + dataWidth_ == getWidth() && + dataHeight_ == getHeight()) { char* v = &result->values()[0]; CFDataGetBytes(data_, CFRangeMake(0, size), (unsigned char*)v); } else { - for (int row = 0; row < height_; row++) { + for (int row = 0; row < getHeight(); row++) { char* v = &result->values()[0]; CFDataGetBytes(data_, - CFRangeMake((top_ + row) * dataWidth_ + left_, width_), - (unsigned char*)v + row * width_); + CFRangeMake((top_ + row) * dataWidth_ + left_, getWidth()), + (unsigned char*)v + row * getWidth()); } } return result; } - -Ref CGImageLuminanceSource::rotateCounterClockwise() { - return Ref(0); -} diff --git a/objc/src/ZXing/ZXCGImageLuminanceSource.h b/objc/src/ZXing/ZXCGImageLuminanceSource.h index 9c3b1d30b..9ee62d0d3 100644 --- a/objc/src/ZXing/ZXCGImageLuminanceSource.h +++ b/objc/src/ZXing/ZXCGImageLuminanceSource.h @@ -21,14 +21,12 @@ namespace zxing { class CGImageLuminanceSource : public LuminanceSource { - private: + typedef LuminanceSource Super; CGImageRef image_; CFDataRef data_; int left_; int top_; - int width_; - int height_; int dataWidth_; int dataHeight_; int bytesPerRow_; @@ -42,12 +40,13 @@ class CGImageLuminanceSource : public LuminanceSource { int width, int height); + static CGImageLuminanceSource* create(CVPixelBufferRef buffer); + CGImageLuminanceSource(CVPixelBufferRef buffer, int left, int top, int width, int height); - CGImageLuminanceSource(CVPixelBufferRef buffer); CGImageLuminanceSource(CGImageRef image, int left, @@ -60,27 +59,12 @@ class CGImageLuminanceSource : public LuminanceSource { CGImageRef image() { return image_; } CGImageRef image(size_t width, size_t height); - ArrayRef getRow(int y, ArrayRef row); - ArrayRef getMatrix(); - - bool isRotateSupported() const { - return true; - } - - int getWidth() const { - return width_; - } - - int getHeight() const { - return height_; - } - - Ref rotateCounterClockwise(); + ArrayRef getRow(int y, ArrayRef row) const; + ArrayRef getMatrix() const; private: - void init(CGImageRef image); - void init(CGImageRef image, int left, int top, int width, int height); + void init(CGImageRef image, int left, int top); }; } /* namespace */ diff --git a/objc/src/ZXing/ZXCGImageLuminanceSource.mm b/objc/src/ZXing/ZXCGImageLuminanceSource.mm index 25191cf6b..1f980d6fa 100644 --- a/objc/src/ZXing/ZXCGImageLuminanceSource.mm +++ b/objc/src/ZXing/ZXCGImageLuminanceSource.mm @@ -78,7 +78,7 @@ } - (id)initWithBuffer:(CVPixelBufferRef)buffer { - self = [super initWithNative:new zxing::CGImageLuminanceSource(buffer)]; + self = [super initWithNative:zxing::CGImageLuminanceSource::create(buffer)]; return self; } @@ -88,12 +88,4 @@ return typed->image(); } -/* -- (CGImageRef)createImageWithWidth:(size_t)width andHeight:(size_t)height { - zxing::LuminanceSource* source = native; - zxing::CGImageLuminanceSource* typed = (zxing::CGImageLuminanceSource*)source; - return typed->createImage(width, height); -} -*/ - @end diff --git a/objc/src/ZXing/ZXCapture.mm b/objc/src/ZXing/ZXCapture.mm index a8c2c05e4..3798063ca 100644 --- a/objc/src/ZXing/ZXCapture.mm +++ b/objc/src/ZXing/ZXCapture.mm @@ -222,12 +222,14 @@ static bool isIPad(); NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithObject:value forKey:key]; - key = (NSString*)kCVPixelBufferWidthKey; - value = [NSNumber numberWithUnsignedLong:width]; - [attributes setObject:value forKey:key]; - key = (NSString*)kCVPixelBufferHeightKey; - value = [NSNumber numberWithUnsignedLong:height]; - [attributes setObject:value forKey:key]; + ZXQT({ + key = (NSString*)kCVPixelBufferWidthKey; + value = [NSNumber numberWithUnsignedLong:width]; + [attributes setObject:value forKey:key]; + key = (NSString*)kCVPixelBufferHeightKey; + value = [NSNumber numberWithUnsignedLong:height]; + [attributes setObject:value forKey:key]; + }); [output ZXQT(setPixelBufferAttributes:)ZXAV(setVideoSettings:)attributes]; }