quirc-rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bba78f2f6bfc2db3f2fcfb40bbf48ebcc0225e2ea917a1cdde546c2b0d9dfd41
4
+ data.tar.gz: 7b9a66a2b6c741af5444e921c8e18137e17283404666d3a255fec6b8577b5df2
5
+ SHA512:
6
+ metadata.gz: f53c2b498c8a6e42ae3ba3436a03bbf5421aeac4dda015fac0cf2610d00a4c1ca4868f3d5d52b41ff173f5be1b713508bdab1208da3ad18968f5c4e22b6e6af1
7
+ data.tar.gz: f72e0f0ba6fefa71f1647fff6e43ffdcd45c174b4ec61eb70ce65eb04a2082dfcb2450c5a1f4e4ef387243443ca1a2e1fb7ca95fb58e5777e24019b66db390db
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ *.log
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in quirc.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rake-compiler"
11
+
12
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ quirc-rb (0.1.0)
5
+ image_processing (~> 1.12, >= 1.12.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ffi (1.15.3)
11
+ image_processing (1.12.1)
12
+ mini_magick (>= 4.9.5, < 5)
13
+ ruby-vips (>= 2.0.17, < 3)
14
+ mini_magick (4.11.0)
15
+ minitest (5.14.4)
16
+ rake (13.0.3)
17
+ rake-compiler (1.1.1)
18
+ rake
19
+ ruby-vips (2.1.2)
20
+ ffi (~> 1.12)
21
+
22
+ PLATFORMS
23
+ x86_64-darwin-20
24
+
25
+ DEPENDENCIES
26
+ minitest (~> 5.0)
27
+ quirc-rb!
28
+ rake (~> 13.0)
29
+ rake-compiler
30
+
31
+ BUNDLED WITH
32
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 songji
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Quirc
2
+
3
+ QRcode recognizer
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "quirc-rb"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install quirc-rb
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "quirc"
25
+
26
+ Quirc.recognize("qrcode.png")
27
+ Quirc.recognize("qrcode.png", image_processor: :vips) # default
28
+ Quirc.recognize("qrcode.png", image_processor: :mini_magick)
29
+ Quirc.recognize("qrcode.png", image_processor: :vips, width: 200) # resize
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/songjiz/quirc-rb.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "fileutils"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ end
12
+
13
+ require "rake/extensiontask"
14
+
15
+ task build: :compile
16
+
17
+ Rake::ExtensionTask.new("quirc") do |ext|
18
+ ext.name = "quirc"
19
+ ext.lib_dir = "lib/quirc"
20
+ end
21
+
22
+ desc "git clone https://github.com/dlbeer/quirc.git"
23
+ task :clone do
24
+ FileUtils.remove_dir("ext/src", true)
25
+ FileUtils.remove_dir("tmp/quirc", true)
26
+ FileUtils.mkdir("ext/src")
27
+ system("git clone https://github.com/dlbeer/quirc.git tmp/quirc")
28
+ FileUtils.copy_entry("tmp/quirc/lib", "ext/src/lib")
29
+ FileUtils.copy_file("tmp/quirc/Makefile", "ext/src/Makefile")
30
+ end
31
+
32
+ task default: %i[clobber compile test]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "quirc"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require "mkmf"
2
+ require "fileutils"
3
+
4
+ src_dir = File.expand_path(File.join(__dir__, "../src"))
5
+ lib_dir = "#{src_dir}/lib"
6
+ inc_dir = "#{src_dir}/lib"
7
+
8
+ Dir.chdir(src_dir) do
9
+ system("make clean")
10
+ system("make libquirc.a")
11
+ FileUtils.mv(Dir.glob("*.a"), "lib")
12
+ end
13
+
14
+ $libs << " -lquirc"
15
+ $CPPFLAGS << " -I#{inc_dir}"
16
+ $LDFLAGS << " -L#{lib_dir}"
17
+
18
+ have_header("quirc.h")
19
+ have_library("quirc")
20
+ have_func("quirc_version")
21
+ have_func("quirc_new")
22
+ have_func("quirc_begin")
23
+ have_func("quirc_end")
24
+ have_func("quirc_destroy")
25
+ have_func("quirc_resize")
26
+ have_func("quirc_count")
27
+
28
+ create_makefile("quirc/quirc")
data/ext/quirc/quirc.c ADDED
@@ -0,0 +1,146 @@
1
+ #include <ruby.h>
2
+ #include "quirc_internal.h"
3
+
4
+ #define LIB_VERSION rb_str_new_cstr(quirc_version())
5
+
6
+ VALUE mQuirc;
7
+ VALUE cQuircRecognizer;
8
+ VALUE cQuircData;
9
+
10
+ typedef struct {
11
+ struct quirc *quirc;
12
+ } quirc_recognizer_t;
13
+
14
+ static void quirc_recognizer_t_dfree(void* _qr) {
15
+ quirc_recognizer_t* qr = (quirc_recognizer_t*)_qr;
16
+ if (qr->quirc != NULL) {
17
+ quirc_destroy(qr->quirc);
18
+ }
19
+ free(qr);
20
+ }
21
+
22
+ static size_t quirc_recognizer_t_dsize(const void* _qr) {
23
+ size_t size;
24
+ quirc_recognizer_t* qr = (quirc_recognizer_t*)_qr;
25
+ size = sizeof(quirc_recognizer_t);
26
+ if (qr->quirc != NULL) {
27
+ size += sizeof(struct quirc);
28
+ size += sizeof(quirc_pixel_t);
29
+ // image byte size
30
+ size += qr->quirc->w * qr->quirc->h;
31
+ size += sizeof(struct quirc_flood_fill_vars);
32
+ }
33
+ return size;
34
+ }
35
+
36
+ // static void quirc_recognizer_t_dmark(void* _qr) {
37
+ // quirc_recognizer_t* qr = (quirc_recognizer_t*)_qr;
38
+ // }
39
+
40
+ static const rb_data_type_t quirc_recognizer_data_type = {
41
+ .wrap_struct_name = "quirc recognizer object",
42
+ .function = {
43
+ // .dmark = quirc_recognizer_t_dmark,
44
+ .dfree = quirc_recognizer_t_dfree,
45
+ .dsize = quirc_recognizer_t_dsize,
46
+ },
47
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
48
+ };
49
+
50
+ VALUE quirc_recognizer_t_alloc(VALUE self) {
51
+ quirc_recognizer_t *qr = malloc(sizeof(quirc_recognizer_t));
52
+
53
+ qr->quirc = quirc_new();
54
+
55
+ if (qr->quirc == NULL) {
56
+ free(qr);
57
+ rb_raise(rb_eNoMemError, "%s", "(quirc_new) failed to allocate memory");
58
+ }
59
+
60
+ return TypedData_Wrap_Struct(self, &quirc_recognizer_data_type, qr);
61
+ }
62
+
63
+ VALUE quirc_recognizer_t_recognize(VALUE self, VALUE image_data, VALUE width, VALUE height) {
64
+ quirc_recognizer_t *qr;
65
+ uint8_t *image;
66
+ int w, h;
67
+ int total_pixels;
68
+ int total_codes;
69
+ VALUE results;
70
+
71
+ Check_Type(image_data, T_STRING);
72
+
73
+ total_pixels = NUM2INT(width) * NUM2INT(height);
74
+
75
+ TypedData_Get_Struct(self, quirc_recognizer_t, &quirc_recognizer_data_type, qr);
76
+
77
+ if (quirc_resize(qr->quirc, NUM2INT(width), NUM2INT(height)) == -1) {
78
+ rb_raise(rb_eNoMemError, "%s", "(quirc_resize) failed to allocate memory");
79
+ }
80
+
81
+ image = quirc_begin(qr->quirc, &w, &h);
82
+ total_pixels = w * h;
83
+
84
+ // Filling the image buffer. One byte per pixel.
85
+ if (RSTRING_LEN(image_data) >= total_pixels) {
86
+ memcpy(image, StringValuePtr(image_data), total_pixels);
87
+ } else {
88
+ // rb_raise(rb_eArgError, "%s", "Expect an 8-bit image");
89
+ }
90
+
91
+ quirc_end(qr->quirc);
92
+
93
+ total_codes = quirc_count(qr->quirc);
94
+ results = rb_ary_new_capa(total_codes);
95
+
96
+ for (int i = 0; i < total_codes; ++i) {
97
+ struct quirc_code code;
98
+ struct quirc_data data;
99
+ quirc_decode_error_t err;
100
+ VALUE result;
101
+
102
+ quirc_extract(qr->quirc, i, &code);
103
+
104
+ err = quirc_decode(&code, &data);
105
+
106
+ if (err == QUIRC_ERROR_DATA_ECC) {
107
+ quirc_flip(&code);
108
+ err = quirc_decode(&code, &data);
109
+ }
110
+
111
+ if (err != QUIRC_SUCCESS) {
112
+ rb_warn("(quirc_decode) %s", quirc_strerror(err));
113
+ continue;
114
+ }
115
+
116
+ result = rb_funcall(cQuircData, rb_intern("new"), 0);
117
+ rb_iv_set(result, "@version", INT2FIX(data.version));
118
+ rb_iv_set(result, "@ecc_level", INT2FIX(data.ecc_level));
119
+ rb_iv_set(result, "@mask", INT2FIX(data.mask));
120
+ rb_iv_set(result, "@data_type", INT2FIX(data.data_type));
121
+ rb_iv_set(result, "@payload_len", INT2FIX(data.payload_len));
122
+ rb_iv_set(result, "@payload", rb_str_new_cstr((const char *)data.payload));
123
+ rb_iv_set(result, "@eci", INT2NUM(data.eci));
124
+ rb_ary_push(results, result);
125
+ }
126
+
127
+ return results;
128
+ }
129
+
130
+ void Init_quirc(void) {
131
+ mQuirc = rb_define_module("Quirc");
132
+ rb_define_const(mQuirc, "LIB_VERSION", LIB_VERSION);
133
+
134
+ cQuircRecognizer = rb_define_class_under(mQuirc, "Recognizer", rb_cObject);
135
+ rb_define_alloc_func(cQuircRecognizer, quirc_recognizer_t_alloc);
136
+ rb_define_method(cQuircRecognizer, "recognize", quirc_recognizer_t_recognize, 3);
137
+
138
+ cQuircData = rb_define_class_under(mQuirc, "Data", rb_cObject);
139
+ rb_define_attr(cQuircData, "version", 1, 0);
140
+ rb_define_attr(cQuircData, "ecc_level", 1, 0);
141
+ rb_define_attr(cQuircData, "mask", 1, 0);
142
+ rb_define_attr(cQuircData, "data_type", 1, 0);
143
+ rb_define_attr(cQuircData, "payload_len", 1, 0);
144
+ rb_define_attr(cQuircData, "payload", 1, 0);
145
+ rb_define_attr(cQuircData, "eci", 1, 0);
146
+ }
data/ext/src/Makefile ADDED
@@ -0,0 +1,103 @@
1
+ # quirc -- QR-code recognition library
2
+ # Copyright (C) 2010-2012 Daniel Beer <dlbeer@gmail.com>
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for any
5
+ # purpose with or without fee is hereby granted, provided that the above
6
+ # copyright notice and this permission notice appear in all copies.
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+
16
+ CC ?= gcc
17
+ PREFIX ?= /usr/local
18
+ SDL_CFLAGS != pkg-config --cflags sdl
19
+ SDL_LIBS != pkg-config --libs sdl
20
+
21
+ LIB_VERSION = 1.0
22
+
23
+ CFLAGS ?= -O3 -Wall -fPIC
24
+ QUIRC_CFLAGS = -Ilib $(CFLAGS) $(SDL_CFLAGS)
25
+ LIB_OBJ = \
26
+ lib/decode.o \
27
+ lib/identify.o \
28
+ lib/quirc.o \
29
+ lib/version_db.o
30
+ DEMO_OBJ = \
31
+ demo/camera.o \
32
+ demo/mjpeg.o \
33
+ demo/convert.o
34
+ DEMO_UTIL_OBJ = \
35
+ demo/dthash.o \
36
+ demo/demoutil.o
37
+
38
+ OPENCV_CFLAGS != pkg-config --cflags opencv4
39
+ OPENCV_LIBS != pkg-config --libs opencv4
40
+ QUIRC_CXXFLAGS = $(QUIRC_CFLAGS) $(OPENCV_CFLAGS) --std=c++17
41
+
42
+ all: libquirc.so qrtest inspect quirc-demo quirc-scanner
43
+
44
+ qrtest: tests/dbgutil.o tests/qrtest.o libquirc.a
45
+ $(CC) -o $@ tests/dbgutil.o tests/qrtest.o libquirc.a $(LDFLAGS) -lm -ljpeg -lpng
46
+
47
+ inspect: tests/dbgutil.o tests/inspect.o libquirc.a
48
+ $(CC) -o $@ tests/dbgutil.o tests/inspect.o libquirc.a $(LDFLAGS) -lm -ljpeg -lpng $(SDL_LIBS) -lSDL_gfx
49
+
50
+ quirc-demo: $(DEMO_OBJ) $(DEMO_UTIL_OBJ) demo/demo.o libquirc.a
51
+ $(CC) -o $@ $(DEMO_OBJ) $(DEMO_UTIL_OBJ) demo/demo.o libquirc.a $(LDFLAGS) -lm -ljpeg $(SDL_LIBS) -lSDL_gfx
52
+
53
+ quirc-demo-opencv: $(DEMO_UTIL_OBJ) demo/demo_opencv.o libquirc.a
54
+ $(CXX) -o $@ $(DEMO_UTIL_OBJ) demo/demo_opencv.o libquirc.a $(LDFLAGS) -lm $(OPENCV_LIBS)
55
+
56
+ quirc-scanner: $(DEMO_OBJ) $(DEMO_UTIL_OBJ) demo/scanner.o libquirc.a
57
+ $(CC) -o $@ $(DEMO_OBJ) $(DEMO_UTIL_OBJ) demo/scanner.o libquirc.a $(LDFLAGS) -lm -ljpeg
58
+
59
+ libquirc.a: $(LIB_OBJ)
60
+ rm -f $@
61
+ ar cru $@ $(LIB_OBJ)
62
+ ranlib $@
63
+
64
+ .PHONY: libquirc.so
65
+ libquirc.so: libquirc.so.$(LIB_VERSION)
66
+
67
+ libquirc.so.$(LIB_VERSION): $(LIB_OBJ)
68
+ $(CC) -shared -o $@ $(LIB_OBJ) $(LDFLAGS) -lm
69
+
70
+ .c.o:
71
+ $(CC) $(QUIRC_CFLAGS) -o $@ -c $<
72
+
73
+ .SUFFIXES: .cxx
74
+ .cxx.o:
75
+ $(CXX) $(QUIRC_CXXFLAGS) -o $@ -c $<
76
+
77
+ install: libquirc.a libquirc.so.$(LIB_VERSION) quirc-demo quirc-scanner
78
+ install -o root -g root -m 0644 lib/quirc.h $(DESTDIR)$(PREFIX)/include
79
+ install -o root -g root -m 0644 libquirc.a $(DESTDIR)$(PREFIX)/lib
80
+ install -o root -g root -m 0755 libquirc.so.$(LIB_VERSION) \
81
+ $(DESTDIR)$(PREFIX)/lib
82
+ install -o root -g root -m 0755 quirc-demo $(DESTDIR)$(PREFIX)/bin
83
+ # install -o root -g root -m 0755 quirc-demo-opencv $(DESTDIR)$(PREFIX)/bin
84
+ install -o root -g root -m 0755 quirc-scanner $(DESTDIR)$(PREFIX)/bin
85
+
86
+ uninstall:
87
+ rm -f $(DESTDIR)$(PREFIX)/include/quirc.h
88
+ rm -f $(DESTDIR)$(PREFIX)/lib/libquirc.so.$(LIB_VERSION)
89
+ rm -f $(DESTDIR)$(PREFIX)/lib/libquirc.a
90
+ rm -f $(DESTDIR)$(PREFIX)/bin/quirc-demo
91
+ rm -f $(DESTDIR)$(PREFIX)/bin/quirc-demo-opencv
92
+ rm -f $(DESTDIR)$(PREFIX)/bin/quirc-scanner
93
+
94
+ clean:
95
+ rm -f */*.o
96
+ rm -f */*.lo
97
+ rm -f libquirc.a
98
+ rm -f libquirc.so.$(LIB_VERSION)
99
+ rm -f qrtest
100
+ rm -f inspect
101
+ rm -f quirc-demo
102
+ rm -f quirc-demo-opencv
103
+ rm -f quirc-scanner