Rdmtx 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ext/rdmtx/Rdmtx.c +133 -0
  2. data/ext/rdmtx/extconf.rb +4 -0
  3. metadata +65 -0
@@ -0,0 +1,133 @@
1
+ /*
2
+ Rdmtx - Ruby wrapper for libdmtx
3
+
4
+ Copyright (C) 2008 Romain Goyet
5
+
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, write to the Free Software
18
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+
20
+ Contact: r.goyet@gmail.com
21
+ */
22
+
23
+ /* $Id:$ */
24
+
25
+ #include <ruby.h>
26
+ #include <dmtx.h>
27
+
28
+ static VALUE rdmtx_init(VALUE self) {
29
+ return self;
30
+ }
31
+
32
+ static VALUE rdmtx_decode(VALUE self, VALUE image /* Image from RMagick (Magick::Image) */, VALUE timeout /* Timeout in msec */) {
33
+
34
+ VALUE rawImageString = rb_funcall(image, rb_intern("export_pixels_to_str"), 0);
35
+
36
+ VALUE safeImageString = StringValue(rawImageString);
37
+
38
+ char * imageBuffer = RSTRING_PTR(safeImageString);
39
+
40
+ int width = NUM2INT(rb_funcall(image, rb_intern("columns"), 0));
41
+ int height = NUM2INT(rb_funcall(image, rb_intern("rows"), 0));
42
+
43
+ DmtxImage *dmtxImage = dmtxImageCreate((unsigned char *)imageBuffer, width,
44
+ height, DmtxPack24bppRGB);
45
+
46
+ VALUE results = rb_ary_new();
47
+
48
+ /* Initialize decode struct for newly loaded image */
49
+ DmtxDecode * decode = dmtxDecodeCreate(dmtxImage, 1);
50
+
51
+ DmtxRegion * region;
52
+
53
+ int intTimeout = NUM2INT(timeout);
54
+ DmtxTime dmtxTimeout = dmtxTimeAdd(dmtxTimeNow(), intTimeout);
55
+
56
+ for(;;) {
57
+ if (intTimeout == 0) {
58
+ region = dmtxRegionFindNext(decode, NULL);
59
+ } else {
60
+ region = dmtxRegionFindNext(decode, &dmtxTimeout);
61
+ }
62
+
63
+ if (region == NULL )
64
+ break;
65
+
66
+ DmtxMessage * message = dmtxDecodeMatrixRegion(decode, region, DmtxUndefined);
67
+ if (message != NULL) {
68
+ VALUE outputString = rb_str_new2((char *)message->output);
69
+ rb_ary_push(results, outputString);
70
+ dmtxMessageDestroy(&message);
71
+ }
72
+
73
+ dmtxRegionDestroy(&region);
74
+ }
75
+
76
+ dmtxDecodeDestroy(&decode);
77
+ dmtxImageDestroy(&dmtxImage);
78
+
79
+ return results;
80
+ }
81
+
82
+ static VALUE rdmtx_encode(VALUE self, VALUE string, VALUE margin, VALUE module) {
83
+
84
+ /* Create and initialize libdmtx structures */
85
+ DmtxEncode * enc = dmtxEncodeCreate();
86
+
87
+ VALUE safeString = StringValue(string);
88
+ int safeMargin = NUM2INT(margin);
89
+ int safeModule = NUM2INT(module);
90
+
91
+ dmtxEncodeSetProp(enc, DmtxPropPixelPacking, DmtxPack24bppRGB);
92
+ dmtxEncodeSetProp(enc, DmtxPropSizeRequest, DmtxSymbolSquareAuto);
93
+
94
+ dmtxEncodeSetProp(enc, DmtxPropMarginSize, safeMargin);
95
+ dmtxEncodeSetProp(enc, DmtxPropModuleSize, safeModule);
96
+
97
+ /* Create barcode image */
98
+ if (dmtxEncodeDataMatrix(enc, RSTRING_LEN(safeString),
99
+ (unsigned char *)RSTRING_PTR(safeString)) == DmtxFail) {
100
+ // printf("Fatal error !\n");
101
+ dmtxEncodeDestroy(&enc);
102
+ return Qnil;
103
+ }
104
+
105
+ int width = dmtxImageGetProp(enc->image, DmtxPropWidth);
106
+ int height = dmtxImageGetProp(enc->image, DmtxPropHeight);
107
+
108
+ VALUE magickImageClass = rb_path2class("Magick::Image");
109
+ VALUE outputImage = rb_funcall(magickImageClass, rb_intern("new"), 2, INT2NUM(width), INT2NUM(height));
110
+
111
+ rb_funcall(outputImage, rb_intern("import_pixels"), 7,
112
+ INT2NUM(0),
113
+ INT2NUM(0),
114
+ INT2NUM(width),
115
+ INT2NUM(height),
116
+ rb_str_new("RGB", 3),
117
+ rb_str_new((char *)enc->image->pxl, 3*width*height),
118
+ // rb_const_get("Magick" ,rb_intern("CharPixel"))
119
+ rb_eval_string("Magick::CharPixel"));
120
+
121
+ /* Clean up */
122
+ dmtxEncodeDestroy(&enc);
123
+
124
+ return outputImage;
125
+ }
126
+
127
+ VALUE cRdmtx;
128
+ void Init_Rdmtx() {
129
+ cRdmtx = rb_define_class("Rdmtx", rb_cObject);
130
+ rb_define_method(cRdmtx, "initialize", rdmtx_init, 0);
131
+ rb_define_method(cRdmtx, "decode", rdmtx_decode, 2);
132
+ rb_define_method(cRdmtx, "encode", rdmtx_encode, 3);
133
+ }
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+ dir_config('dmtx')
3
+ have_library('dmtx')
4
+ create_makefile('Rdmtx')
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Rdmtx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Srijan Choudhary
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rmagick
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: This is a ruby wrapper for libdmtx, which is a open source software for
31
+ reading and writing Data Matrix barcodes.
32
+ email: srijan4@gmail.com
33
+ executables: []
34
+ extensions:
35
+ - ext/rdmtx/extconf.rb
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ext/rdmtx/Rdmtx.c
39
+ - ext/rdmtx/extconf.rb
40
+ homepage: https://github.com/srijan/ruby-dmtx
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements:
59
+ - libdmtx
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.25
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Ruby libdmtx wrapper
65
+ test_files: []