[zbar-commits] push rev [340]: fix C++ wrapper bugs (bug #3396068)
Status: Beta
Brought to you by:
spadix
From: <sp...@us...> - 2011-09-18 18:39:34
|
changeset: 340:7dfcfb9f5e6b user: sp...@us... date: Sun Sep 18 11:38:54 2011 -0700 details: http://zbar.hg.sourceforge.net:8000/hgroot/zbar/zbarzbar/rev/7dfcfb9f5e6b description: fix C++ wrapper bugs (bug #3396068) - thanks to anotheruser1 for reporting this! - add new C++ wrapper test diffstat: ChangeLog | 3 + include/zbar/Image.h | 13 ++- include/zbar/Symbol.h | 17 ++- test/Makefile.am.inc | 9 +- test/test_cpp_img.cpp | 234 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_images.c | 205 ++++++++++++++++++++++++++++++++++-------- test/test_images.h | 15 +++ 7 files changed, 444 insertions(+), 52 deletions(-) diffs (truncated from 671 to 300 lines): diff -r a111d70d612d -r 7dfcfb9f5e6b ChangeLog --- a/ChangeLog Tue Aug 23 12:17:23 2011 -0700 +++ b/ChangeLog Sun Sep 18 11:38:54 2011 -0700 @@ -1,4 +1,7 @@ current: + * fix C++ wrapper bugs (bug #3396068) + - thanks to anotheruser1 for reporting this! + - add new C++ wrapper test * fix avoid compositing ISBN10 data * add support for GraphicsMagick as ImageMagick alternative * mention xmlto in HACKING (patch #3297039) diff -r a111d70d612d -r 7dfcfb9f5e6b include/zbar/Image.h --- a/include/zbar/Image.h Tue Aug 23 12:17:23 2011 -0700 +++ b/include/zbar/Image.h Sun Sep 18 11:38:54 2011 -0700 @@ -115,8 +115,8 @@ ~Image () { - set_data(NULL, 0); - zbar_image_set_userdata(_img, NULL); + if(zbar_image_get_userdata(_img) == this) + zbar_image_set_userdata(_img, NULL); zbar_image_ref(_img, -1); } @@ -255,6 +255,14 @@ throw FormatError(); } + /// image format conversion. + /// see zbar_image_convert() + Image convert (std::string format) const + { + unsigned long fourcc = zbar_fourcc_parse(format.c_str()); + return(convert(fourcc)); + } + /// image format conversion with crop/pad. /// see zbar_image_convert_resize() /// @since 0.4 @@ -309,7 +317,6 @@ { // by default nothing is cleaned assert(img); - assert(zbar_image_get_userdata(img)); } private: diff -r a111d70d612d -r 7dfcfb9f5e6b include/zbar/Symbol.h --- a/include/zbar/Symbol.h Tue Aug 23 12:17:23 2011 -0700 +++ b/include/zbar/Symbol.h Sun Sep 18 11:38:54 2011 -0700 @@ -151,7 +151,8 @@ : _sym(sym), _index(index) { - sym->ref(1); + if(sym) + sym->ref(1); if(!sym || (unsigned)_index >= zbar_symbol_get_loc_size(*_sym)) _index = -1; @@ -162,20 +163,24 @@ : _sym(iter._sym), _index(iter._index) { - _sym->ref(); + if(_sym) + _sym->ref(); } /// destructor. ~PointIterator () { - _sym->ref(-1); + if(_sym) + _sym->ref(-1); } /// assignment. PointIterator& operator= (const PointIterator& iter) { - iter._sym->ref(); - _sym->ref(-1); + if(iter._sym) + iter._sym->ref(); + if(_sym) + _sym->ref(-1); _sym = iter._sym; _index = iter._index; return(*this); @@ -191,7 +196,7 @@ PointIterator& operator++ () { unsigned int i = ++_index; - if(i >= zbar_symbol_get_loc_size(*_sym)) + if(!_sym || i >= zbar_symbol_get_loc_size(*_sym)) _index = -1; return(*this); } diff -r a111d70d612d -r 7dfcfb9f5e6b test/Makefile.am.inc --- a/test/Makefile.am.inc Tue Aug 23 12:17:23 2011 -0700 +++ b/test/Makefile.am.inc Sun Sep 18 11:38:54 2011 -0700 @@ -28,6 +28,10 @@ test_test_cpp_SOURCES = test/test_cpp.cpp test_test_cpp_LDADD = zbar/libzbar.la $(AM_LDADD) +check_PROGRAMS += test/test_cpp_img +test_test_cpp_img_SOURCES = test/test_cpp_img.cpp $(TEST_IMAGE_SOURCES) +test_test_cpp_img_LDADD = zbar/libzbar.la $(AM_LDADD) + if HAVE_JPEG check_PROGRAMS += test/test_jpeg test_test_jpeg_SOURCES = test/test_jpeg.c @@ -68,13 +72,16 @@ test/.libs/test_convert test/.libs/test_window \ test/.libs/test_video test/.libs/dbg_scan test/.libs/test_gtk +check-cpp: test/test_cpp_img + test/test_cpp_img + check-decoder: test/test_decode test/test_decode -q regress-decoder: test/test_decode test/test_decode -n 100000 -check-local: check-decoder check-images +check-local: check-cpp check-decoder check-images regress: regress-decoder regress-images .PHONY: check-decoder check-images regress-decoder regress-images regress diff -r a111d70d612d -r 7dfcfb9f5e6b test/test_cpp_img.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/test_cpp_img.cpp Sun Sep 18 11:38:54 2011 -0700 @@ -0,0 +1,234 @@ +//------------------------------------------------------------------------ +// Copyright 2007-2009 (c) Jeff Brown <sp...@us...> +// +// This file is part of the ZBar Bar Code Reader. +// +// The ZBar Bar Code Reader is free software; you can redistribute it +// and/or modify it under the terms of the GNU Lesser Public License as +// published by the Free Software Foundation; either version 2.1 of +// the License, or (at your option) any later version. +// +// The ZBar Bar Code Reader is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser Public License for more details. +// +// You should have received a copy of the GNU Lesser Public License +// along with the ZBar Bar Code Reader; if not, write to the Free +// Software Foundation, Inc., 51 Franklin St, Fifth Floor, +// Boston, MA 02110-1301 USA +// +// http://sourceforge.net/projects/zbar +//------------------------------------------------------------------------ + +// NB do not put anything before this header +// it's here to check that we didn't omit any dependencies +#include <zbar.h> + +#include <iostream> +#include <sstream> +#include <iomanip> +#include "test_images.h" + +bool debug = false; +bool verbose = false; +int errors = 0; +zbar::zbar_symbol_type_t expect_type = zbar::ZBAR_NONE; +std::string expect_data; + +template <class T> +inline std::string to_string (const T& t) +{ + std::stringstream ss; + ss << t; + return ss.str(); +} + +static inline int +error (const std::string &msg) +{ + errors++; + std::cerr << "ERROR: " << msg << std::endl; + if(debug) + abort(); + return(-1); +} + +static inline int +check_loc (const zbar::Image &img, + const zbar::Symbol &sym) +{ + int n = 0; + int w = img.get_width(); + int h = img.get_height(); + for(zbar::Symbol::PointIterator p(sym.point_begin()); + p != sym.point_end(); + ++p, n++) + { + zbar::Symbol::Point q(*p); + if(q.x < 0 || q.x >= w || + q.y < 0 || q.y >= h) + error("location point out of range"); + } + return(!n); +} + +static inline int +check_symbol (const zbar::Image &img, + const zbar::Symbol &sym) +{ + zbar::zbar_symbol_type_t type(sym.get_type()); + std::string data(sym.get_data()); + + bool pass = + expect_type && + type == expect_type && + data == expect_data && + sym.get_data_length() == expect_data.length(); + if(pass) + pass = !check_loc(img, sym); + + if(verbose || !pass) + std::cerr << "decode Symbol: " << sym << std::endl; + + if(!expect_type) + error("unexpected"); + else if(!pass) + error(std::string("expected: ") + + zbar::zbar_get_symbol_name(expect_type) + + " " + expect_data); + + expect_type = zbar::ZBAR_NONE; + expect_data = ""; + return(!pass); +} + +static inline int +check_image (const zbar::Image &img) +{ + zbar::SymbolSet syms(img.get_symbols()); + int setn = syms.get_size(), countn = 0; + + int rc = 0; + for(zbar::SymbolIterator sym(syms.symbol_begin()); + sym != syms.symbol_end(); + ++sym, ++countn) + rc |= check_symbol(img, *sym); + + if(countn != setn) + rc |= error("SymbolSet size mismatch: exp=" + to_string(setn) + + " act=" + to_string(countn)); + return(rc); +} + +static inline void +expect (zbar::zbar_symbol_type_t type, + std::string data) +{ + if(expect_type) + error(std::string("missing: ") + zbar_get_symbol_name(expect_type) + + " " + expect_data); + expect_type = type; + expect_data = data; +} + +class Handler : public zbar::Image::Handler { + void image_callback(zbar::Image &img); +}; + +void +Handler::image_callback (zbar::Image &img) +{ + bool unexpected = !expect_type; + if(unexpected) + error("unexpected image callback"); + check_image(img); +} + +static inline int +test_processor () +{ + // create processor w/no video and no window + zbar::Processor proc(debug, NULL); + Handler handler; + proc.set_handler(handler); + if(debug) { + proc.set_visible(); + proc.user_wait(); + } + + // generate barcode test image + zbar::Image rgb3(0, 0, "RGB3"); + + // test cast to C image + if(test_image_ean13(rgb3)) + error("failed to generate image"); |