-
Notifications
You must be signed in to change notification settings - Fork 8
/
Rdmtx.c
197 lines (155 loc) · 7.15 KB
/
Rdmtx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Rdmtx - Ruby wrapper for libdmtx
Copyright (C) 2008 Romain Goyet
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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 General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Contact: [email protected]
*/
/* $Id:$ */
#include <ruby.h>
#include <dmtx.h>
static VALUE rdmtx_init(VALUE self) {
return self;
}
static VALUE rdmtx_decode(VALUE self, VALUE image /* Image from RMagick (Magick::Image) */, VALUE timeout /* Timeout in msec */) {
VALUE rawImageString = rb_funcall(image, rb_intern("export_pixels_to_str"), 0);
VALUE safeImageString = StringValue(rawImageString);
char * imageBuffer = RSTRING_PTR(safeImageString);
int width = NUM2INT(rb_funcall(image, rb_intern("columns"), 0));
int height = NUM2INT(rb_funcall(image, rb_intern("rows"), 0));
DmtxImage *dmtxImage = dmtxImageCreate((unsigned char *)imageBuffer, width,
height, DmtxPack24bppRGB);
VALUE results = rb_ary_new();
/* Initialize decode struct for newly loaded image */
DmtxDecode * decode = dmtxDecodeCreate(dmtxImage, 1);
DmtxRegion * region;
DmtxMessage * message;
int intTimeout = NUM2INT(timeout);
DmtxTime dmtxTimeout = dmtxTimeAdd(dmtxTimeNow(), intTimeout);
for(;;) {
if (intTimeout == 0) {
region = dmtxRegionFindNext(decode, NULL);
} else {
region = dmtxRegionFindNext(decode, &dmtxTimeout);
}
if (region == NULL )
break;
message = dmtxDecodeMatrixRegion(decode, region, DmtxUndefined);
if (message != NULL) {
VALUE outputString = rb_str_new2((char *)message->output);
rb_ary_push(results, outputString);
dmtxMessageDestroy(&message);
}
dmtxRegionDestroy(®ion);
}
dmtxDecodeDestroy(&decode);
dmtxImageDestroy(&dmtxImage);
return results;
}
static VALUE rdmtx_encode(int argc, VALUE * argv, VALUE self) {
VALUE string, margin, module, size;
VALUE safeString;
VALUE magickImageClass;
VALUE outputImage;
int safeMargin, safeModule, safeSize;
int width;
int height;
DmtxEncode * enc;
rb_scan_args(argc, argv, "13", &string,
&margin, &module, &size);
safeString = StringValue(string);
if(NIL_P(margin)) {
safeMargin = 5;
} else {
safeMargin = NUM2INT(margin);
}
if(NIL_P(module)) {
safeModule = 5;
} else {
safeModule = NUM2INT(module);
}
if(NIL_P(size)) {
safeSize = DmtxSymbolSquareAuto;
} else {
safeSize = NUM2INT(size);
}
// printf("Margin = %d, Module = %d, Size = %d\n", safeMargin, safeModule, safeSize);
/* Create and initialize libdmtx structures */
enc = dmtxEncodeCreate();
dmtxEncodeSetProp(enc, DmtxPropPixelPacking, DmtxPack24bppRGB);
dmtxEncodeSetProp(enc, DmtxPropSizeRequest, safeSize);
dmtxEncodeSetProp(enc, DmtxPropMarginSize, safeMargin);
dmtxEncodeSetProp(enc, DmtxPropModuleSize, safeModule);
/* Create barcode image */
if (dmtxEncodeDataMatrix(enc, RSTRING_LEN(safeString),
(unsigned char *)RSTRING_PTR(safeString)) == DmtxFail) {
// printf("Fatal error !\n");
dmtxEncodeDestroy(&enc);
return Qnil;
}
width = dmtxImageGetProp(enc->image, DmtxPropWidth);
height = dmtxImageGetProp(enc->image, DmtxPropHeight);
magickImageClass = rb_path2class("Magick::Image");
outputImage = rb_funcall(magickImageClass, rb_intern("new"), 2, INT2NUM(width), INT2NUM(height));
rb_funcall(outputImage, rb_intern("import_pixels"), 7,
INT2NUM(0),
INT2NUM(0),
INT2NUM(width),
INT2NUM(height),
rb_str_new("RGB", 3),
rb_str_new((char *)enc->image->pxl, 3*width*height),
// rb_const_get("Magick" ,rb_intern("CharPixel"))
rb_eval_string("Magick::CharPixel"));
/* Clean up */
dmtxEncodeDestroy(&enc);
return outputImage;
}
VALUE cRdmtx;
void Init_Rdmtx() {
cRdmtx = rb_define_class("Rdmtx", rb_cObject);
rb_define_method(cRdmtx, "initialize", rdmtx_init, 0);
rb_define_method(cRdmtx, "decode", rdmtx_decode, 2);
rb_define_method(cRdmtx, "encode", rdmtx_encode, -1);
rb_define_global_const("DmtxSymbolRectAuto", INT2FIX(DmtxSymbolRectAuto));
rb_define_global_const("DmtxSymbolSquareAuto", INT2FIX(DmtxSymbolSquareAuto));
rb_define_global_const("DmtxSymbolShapeAuto", INT2FIX(DmtxSymbolShapeAuto));
rb_define_global_const("DmtxSymbol10x10", INT2FIX(DmtxSymbol10x10));
rb_define_global_const("DmtxSymbol12x12", INT2FIX(DmtxSymbol12x12));
rb_define_global_const("DmtxSymbol14x14", INT2FIX(DmtxSymbol14x14));
rb_define_global_const("DmtxSymbol16x16", INT2FIX(DmtxSymbol16x16));
rb_define_global_const("DmtxSymbol18x18", INT2FIX(DmtxSymbol18x18));
rb_define_global_const("DmtxSymbol20x20", INT2FIX(DmtxSymbol20x20));
rb_define_global_const("DmtxSymbol22x22", INT2FIX(DmtxSymbol22x22));
rb_define_global_const("DmtxSymbol24x24", INT2FIX(DmtxSymbol24x24));
rb_define_global_const("DmtxSymbol26x26", INT2FIX(DmtxSymbol26x26));
rb_define_global_const("DmtxSymbol32x32", INT2FIX(DmtxSymbol32x32));
rb_define_global_const("DmtxSymbol36x36", INT2FIX(DmtxSymbol36x36));
rb_define_global_const("DmtxSymbol40x40", INT2FIX(DmtxSymbol40x40));
rb_define_global_const("DmtxSymbol44x44", INT2FIX(DmtxSymbol44x44));
rb_define_global_const("DmtxSymbol48x48", INT2FIX(DmtxSymbol48x48));
rb_define_global_const("DmtxSymbol52x52", INT2FIX(DmtxSymbol52x52));
rb_define_global_const("DmtxSymbol64x64", INT2FIX(DmtxSymbol64x64));
rb_define_global_const("DmtxSymbol72x72", INT2FIX(DmtxSymbol72x72));
rb_define_global_const("DmtxSymbol80x80", INT2FIX(DmtxSymbol80x80));
rb_define_global_const("DmtxSymbol88x88", INT2FIX(DmtxSymbol88x88));
rb_define_global_const("DmtxSymbol96x96", INT2FIX(DmtxSymbol96x96));
rb_define_global_const("DmtxSymbol104x104", INT2FIX(DmtxSymbol104x104));
rb_define_global_const("DmtxSymbol120x120", INT2FIX(DmtxSymbol120x120));
rb_define_global_const("DmtxSymbol132x132", INT2FIX(DmtxSymbol132x132));
rb_define_global_const("DmtxSymbol144x144", INT2FIX(DmtxSymbol144x144));
rb_define_global_const("DmtxSymbol8x18", INT2FIX(DmtxSymbol8x18));
rb_define_global_const("DmtxSymbol8x32", INT2FIX(DmtxSymbol8x32));
rb_define_global_const("DmtxSymbol12x26", INT2FIX(DmtxSymbol12x26));
rb_define_global_const("DmtxSymbol12x36", INT2FIX(DmtxSymbol12x36));
rb_define_global_const("DmtxSymbol16x36", INT2FIX(DmtxSymbol16x36));
rb_define_global_const("DmtxSymbol16x48", INT2FIX(DmtxSymbol16x48));
}