nl-knd_client 0.0.0.pre.usegit
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +25 -0
- data/.rspec +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +53 -0
- data/LICENSE +661 -0
- data/README.md +59 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/ext/Makefile +36 -0
- data/ext/front.c +26 -0
- data/ext/front_grid.c +43 -0
- data/ext/kinutils/extconf.rb +8 -0
- data/ext/kinutils/kinutils.c +411 -0
- data/ext/kinutils/unpack.c +212 -0
- data/ext/kinutils/unpack.h +108 -0
- data/ext/overhead.c +26 -0
- data/ext/overhead_grid.c +42 -0
- data/ext/side.c +26 -0
- data/ext/side_grid.c +42 -0
- data/ext/unpacktest.c +69 -0
- data/lib/nl/knd_client.rb +21 -0
- data/lib/nl/knd_client/em_knd_client.rb +904 -0
- data/lib/nl/knd_client/em_knd_command.rb +77 -0
- data/lib/nl/knd_client/simple_knd_client.rb +106 -0
- data/lib/nl/knd_client/version.rb +5 -0
- data/lib/nl/knd_client/zone.rb +227 -0
- data/nl-knd_client.gemspec +37 -0
- metadata +145 -0
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# NL::KndClient
|
2
|
+
|
3
|
+
Client library for interacting with the Nitrogen Logic [KND][0] (Kinematic
|
4
|
+
Network Daemon) server, which provides zone-based data from a Kinect.
|
5
|
+
|
6
|
+
There are two clients provided:
|
7
|
+
|
8
|
+
- `NL::KndClient::EMKndClient` – a complex, older, but full-featured
|
9
|
+
asynchronous client based on EventMachine. This is only available if the
|
10
|
+
EventMachine gem is already present in your application's dependencies.
|
11
|
+
- `NL::KndClient::SimpleKndClient` – a simple, newer, quick-and-dirty
|
12
|
+
`Thread`-based client that is easier to use, but does not support all KND
|
13
|
+
features.
|
14
|
+
|
15
|
+
Some data decoding functions are also provided as a C extension in
|
16
|
+
`NL::KndClient::Kinutils`.
|
17
|
+
|
18
|
+
## License
|
19
|
+
|
20
|
+
NL::KndClient is ©2011-2020 Mike Bourgeous.
|
21
|
+
|
22
|
+
NL::KndClient is licensed under the Affero GPL version 3 (AGPLv3). Feel free
|
23
|
+
to get in touch if you would like to discuss more permissive terms.
|
24
|
+
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
This gem depends on the Nitrogen Logic C utility library, [nlutils][1].
|
29
|
+
|
30
|
+
After installing nlutils, add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'nl-knd_client', git: 'git@github.com:nitrogenlogic/nl-knd_client.git'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
```bash
|
39
|
+
$ bundle install
|
40
|
+
```
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
TODO: Write usage instructions here
|
45
|
+
|
46
|
+
### Standalone command-line processing
|
47
|
+
|
48
|
+
There is a Makefile in the `ext/` directory that will build standalone tools
|
49
|
+
for unpacking and projecting raw depth data.
|
50
|
+
|
51
|
+
```bash
|
52
|
+
cd ext/
|
53
|
+
make
|
54
|
+
|
55
|
+
cat depth11.raw | ./unpack -i | ./overhead | convert -size 500x500 -depth 8 GRAY:- /tmp/overhead.png
|
56
|
+
```
|
57
|
+
|
58
|
+
[0]: https://github.com/nitrogenlogic/knd
|
59
|
+
[1]: https://github.com/nitrogenlogic/nlutils
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/ext/Makefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# This Makefile is for building standalone executables for processing raw
|
2
|
+
# 11-bit Kinect depth data from the command line.
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
# cat depth11.raw | ./unpack -i | ./overhead | convert -size 500x500 -depth 8 GRAY:- /tmp/overhead.png
|
6
|
+
.PHONY: clean all
|
7
|
+
|
8
|
+
RUBY?=/usr/bin/env ruby
|
9
|
+
|
10
|
+
CFLAGS=-Wall -Wextra -std=gnu99 -O3 -D_GNU_SOURCE
|
11
|
+
|
12
|
+
all: unpack overhead side front ext overhead_grid side_grid front_grid
|
13
|
+
|
14
|
+
unpack: kinutils/unpack.c unpacktest.c Makefile
|
15
|
+
gcc kinutils/unpack.c unpacktest.c -o unpack $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
16
|
+
|
17
|
+
overhead: kinutils/unpack.c overhead.c Makefile
|
18
|
+
gcc kinutils/unpack.c overhead.c -o overhead $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
19
|
+
|
20
|
+
side: kinutils/unpack.c side.c Makefile
|
21
|
+
gcc kinutils/unpack.c side.c -o side $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
22
|
+
|
23
|
+
front: kinutils/unpack.c front.c Makefile
|
24
|
+
gcc kinutils/unpack.c front.c -o front $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
25
|
+
|
26
|
+
overhead_grid: kinutils/unpack.c overhead_grid.c Makefile
|
27
|
+
gcc kinutils/unpack.c overhead_grid.c -o overhead_grid $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
28
|
+
|
29
|
+
side_grid: kinutils/unpack.c side_grid.c Makefile
|
30
|
+
gcc kinutils/unpack.c side_grid.c -o side_grid $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
31
|
+
|
32
|
+
front_grid: kinutils/unpack.c front_grid.c Makefile
|
33
|
+
gcc kinutils/unpack.c front_grid.c -o front_grid $(CFLAGS) -Ikinutils -lm $(EXTRACFLAGS)
|
34
|
+
|
35
|
+
clean:
|
36
|
+
rm -f unpack overhead side front overhead_grid side_grid front_grid
|
data/ext/front.c
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
/*
|
2
|
+
* Experimental Kinect depth image unpacking code.
|
3
|
+
* (C)2011 Mike Bourgeous
|
4
|
+
*/
|
5
|
+
#include <stdio.h>
|
6
|
+
#include <stdint.h>
|
7
|
+
|
8
|
+
#include "unpack.h"
|
9
|
+
|
10
|
+
int main()
|
11
|
+
{
|
12
|
+
uint16_t in[640 * 480];
|
13
|
+
uint8_t out[XPIX * YPIX];
|
14
|
+
|
15
|
+
if(fread(in, 2, 640 * 480, stdin) != 640 * 480) {
|
16
|
+
fprintf(stderr, "Must provide %d bytes of unpacked depth data to stdin.\n", 640 * 480 * 2);
|
17
|
+
}
|
18
|
+
|
19
|
+
ku_init_lut();
|
20
|
+
plot_front(in, out);
|
21
|
+
|
22
|
+
fwrite(out, 1, sizeof(out), stdout);
|
23
|
+
|
24
|
+
return 0;
|
25
|
+
}
|
26
|
+
|
data/ext/front_grid.c
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
/*
|
2
|
+
* Code to render a basic grid for the side view, for later manipulation.
|
3
|
+
* The final grid will be created from this program's output using the GIMP.
|
4
|
+
* (C)2012 Mike Bourgeous
|
5
|
+
*/
|
6
|
+
#include <stdio.h>
|
7
|
+
#include <stdint.h>
|
8
|
+
#include <string.h>
|
9
|
+
|
10
|
+
#include "unpack.h"
|
11
|
+
|
12
|
+
void draw_grid(uint8_t *grid, uint8_t color, int spacing)
|
13
|
+
{
|
14
|
+
int x, y;
|
15
|
+
|
16
|
+
// Loop through x in world space, y in pixels
|
17
|
+
for(x = 0; x < XMAX / 2; x += spacing) {
|
18
|
+
for(y = 0; y < YPIX; y++) {
|
19
|
+
grid[XPIX / 2 + x * XPIX / XMAX + y * XPIX] = color;
|
20
|
+
grid[XPIX / 2 - x * XPIX / XMAX + y * XPIX] = color;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
// Loop through y in world space, x in pixels
|
25
|
+
for(y = 0; y < YMAX / 2; y += spacing) {
|
26
|
+
for(x = 0; x < XPIX; x++) {
|
27
|
+
grid[x + YPIX / 2 * XPIX + y * YPIX / YMAX * XPIX] = color;
|
28
|
+
grid[x + YPIX / 2 * XPIX - y * YPIX / YMAX * XPIX] = color;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
int main()
|
34
|
+
{
|
35
|
+
uint8_t out[XPIX * YPIX];
|
36
|
+
|
37
|
+
memset(out, 0, sizeof(out));
|
38
|
+
draw_grid(out, 128, 500);
|
39
|
+
draw_grid(out, 255, 1000);
|
40
|
+
fwrite(out, 1, sizeof(out), stdout);
|
41
|
+
|
42
|
+
return 0;
|
43
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
extension_name='kinutils'
|
3
|
+
|
4
|
+
raise 'libnlutils not found' unless have_library("nlutils", "nl_unescape_string")
|
5
|
+
|
6
|
+
with_cflags("#{$CFLAGS} -O3 -Wall -Wextra #{ENV['EXTRACFLAGS']} -std=c99 -D_XOPEN_SOURCE=700 -D_ISOC99_SOURCE -D_GNU_SOURCE") do
|
7
|
+
create_makefile('nl/knd_client/kinutils')
|
8
|
+
end
|
@@ -0,0 +1,411 @@
|
|
1
|
+
/*
|
2
|
+
* Kinect data manipulation utilities for Ruby.
|
3
|
+
* (C)2011 Mike Bourgeous
|
4
|
+
*
|
5
|
+
* References:
|
6
|
+
* http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
|
7
|
+
* http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
|
8
|
+
*/
|
9
|
+
#include <ctype.h>
|
10
|
+
#include <ruby.h>
|
11
|
+
#include <ruby/encoding.h>
|
12
|
+
#include <ruby/thread.h>
|
13
|
+
#include <nlutils/nlutils.h>
|
14
|
+
|
15
|
+
#include "unpack.h"
|
16
|
+
|
17
|
+
struct plot_info {
|
18
|
+
const uint16_t *in;
|
19
|
+
uint8_t *out;
|
20
|
+
};
|
21
|
+
|
22
|
+
struct unpack_info {
|
23
|
+
const uint8_t *in;
|
24
|
+
uint16_t *out;
|
25
|
+
size_t len;
|
26
|
+
};
|
27
|
+
|
28
|
+
struct kvp_info {
|
29
|
+
VALUE hash;
|
30
|
+
unsigned int symbolize:1;
|
31
|
+
};
|
32
|
+
|
33
|
+
// The KinUtils Ruby module
|
34
|
+
VALUE KinUtils = Qnil;
|
35
|
+
static rb_encoding *utf8;
|
36
|
+
|
37
|
+
void *unpack_blocking(void *data)
|
38
|
+
{
|
39
|
+
struct unpack_info *info = data;
|
40
|
+
size_t i, o;
|
41
|
+
|
42
|
+
for(i = 0, o = 0; i < info->len; i += 11, o += 8) {
|
43
|
+
unpack11_to_16(info->in + i, info->out + o);
|
44
|
+
}
|
45
|
+
|
46
|
+
return NULL;
|
47
|
+
}
|
48
|
+
|
49
|
+
// Ruby function to unpack 11-bit depth data to 16-bit left-aligned values
|
50
|
+
VALUE rb_unpack11_to_16(VALUE self, VALUE data)
|
51
|
+
{
|
52
|
+
size_t len, newlen;
|
53
|
+
VALUE outbuf;
|
54
|
+
|
55
|
+
Check_Type(data, T_STRING);
|
56
|
+
len = RSTRING_LEN(data);
|
57
|
+
if(len < 11) {
|
58
|
+
rb_raise(rb_eArgError, "Input data must be at least 11 bytes long (got %zu).", len);
|
59
|
+
}
|
60
|
+
|
61
|
+
newlen = len * 16 / 11;
|
62
|
+
outbuf = rb_str_buf_new(newlen - 1);
|
63
|
+
rb_str_resize(outbuf, newlen); // Prevent GC from shrinking the buffer
|
64
|
+
|
65
|
+
rb_thread_call_without_gvl(
|
66
|
+
unpack_blocking,
|
67
|
+
&(struct unpack_info){.in = (uint8_t *)RSTRING_PTR(data), .out = (uint16_t *)RSTRING_PTR(outbuf), .len = len},
|
68
|
+
NULL,
|
69
|
+
NULL
|
70
|
+
);
|
71
|
+
|
72
|
+
return outbuf;
|
73
|
+
}
|
74
|
+
|
75
|
+
VALUE plot_linear_blocking(void *data)
|
76
|
+
{
|
77
|
+
struct plot_info *info = data;
|
78
|
+
plot_linear(info->in, info->out);
|
79
|
+
return Qnil;
|
80
|
+
}
|
81
|
+
|
82
|
+
// Ruby function to plot perspective view of linear depth data. Input:
|
83
|
+
// 640x480x16bit gray depth image. Output: 640x480 8-bit gray image.
|
84
|
+
VALUE rb_plot_linear(VALUE self, VALUE data)
|
85
|
+
{
|
86
|
+
VALUE outbuf;
|
87
|
+
size_t len;
|
88
|
+
|
89
|
+
// TODO: Allow reusing a previously-allocated output string
|
90
|
+
|
91
|
+
Check_Type(data, T_STRING);
|
92
|
+
len = RSTRING_LEN(data);
|
93
|
+
if(len < 640 * 480 * 2) {
|
94
|
+
rb_raise(rb_eArgError, "Input data must be at least 640*480*2 bytes (got %zu).", len);
|
95
|
+
}
|
96
|
+
|
97
|
+
// It seems rb_str_buf_new() adds a byte for terminating NUL, but
|
98
|
+
// rb_str_resize() does not.
|
99
|
+
outbuf = rb_str_buf_new(640 * 480 - 1);
|
100
|
+
rb_str_resize(outbuf, 640 * 480);
|
101
|
+
|
102
|
+
rb_thread_call_without_gvl(
|
103
|
+
plot_linear_blocking,
|
104
|
+
&(struct plot_info){.in = (uint16_t *)RSTRING_PTR(data), .out = (uint8_t *)RSTRING_PTR(outbuf)},
|
105
|
+
NULL,
|
106
|
+
NULL
|
107
|
+
);
|
108
|
+
return outbuf;
|
109
|
+
}
|
110
|
+
|
111
|
+
VALUE plot_overhead_blocking(void *data)
|
112
|
+
{
|
113
|
+
struct plot_info *info = data;
|
114
|
+
plot_overhead(info->in, info->out);
|
115
|
+
return Qnil;
|
116
|
+
}
|
117
|
+
|
118
|
+
// Ruby function to plot overhead view of depth data. Input: 640x480x16bit
|
119
|
+
// gray depth image. Output: XPIXxZPIX 8-bit gray image.
|
120
|
+
VALUE rb_plot_overhead(VALUE self, VALUE data)
|
121
|
+
{
|
122
|
+
VALUE outbuf;
|
123
|
+
size_t len;
|
124
|
+
|
125
|
+
// TODO: Allow reusing a previously-allocated output string
|
126
|
+
|
127
|
+
Check_Type(data, T_STRING);
|
128
|
+
len = RSTRING_LEN(data);
|
129
|
+
if(len < 640 * 480 * 2) {
|
130
|
+
rb_raise(rb_eArgError, "Input data must be at least 640*480*2 bytes (got %zu).", len);
|
131
|
+
}
|
132
|
+
|
133
|
+
// It seems rb_str_buf_new() adds a byte for terminating NUL, but
|
134
|
+
// rb_str_resize() does not.
|
135
|
+
outbuf = rb_str_buf_new(XPIX * ZPIX - 1);
|
136
|
+
rb_str_resize(outbuf, XPIX * ZPIX);
|
137
|
+
|
138
|
+
rb_thread_call_without_gvl(
|
139
|
+
plot_overhead_blocking,
|
140
|
+
&(struct plot_info){.in = (uint16_t *)RSTRING_PTR(data), .out = (uint8_t *)RSTRING_PTR(outbuf)},
|
141
|
+
NULL,
|
142
|
+
NULL
|
143
|
+
);
|
144
|
+
return outbuf;
|
145
|
+
}
|
146
|
+
|
147
|
+
VALUE plot_side_blocking(void *data)
|
148
|
+
{
|
149
|
+
struct plot_info *info = data;
|
150
|
+
plot_side(info->in, info->out);
|
151
|
+
return Qnil;
|
152
|
+
}
|
153
|
+
|
154
|
+
// Ruby function to plot side view of depth data. Input: 640x480x16bit
|
155
|
+
// gray depth image. Output: ZPIXxYPIX 8-bit gray image.
|
156
|
+
VALUE rb_plot_side(VALUE self, VALUE data)
|
157
|
+
{
|
158
|
+
VALUE outbuf;
|
159
|
+
size_t len;
|
160
|
+
|
161
|
+
// TODO: Allow reusing a previously-allocated output string
|
162
|
+
|
163
|
+
Check_Type(data, T_STRING);
|
164
|
+
len = RSTRING_LEN(data);
|
165
|
+
if(len < 640 * 480 * 2) {
|
166
|
+
rb_raise(rb_eArgError, "Input data must be at least 640*480*2 bytes (got %zu).", len);
|
167
|
+
}
|
168
|
+
|
169
|
+
// It seems rb_str_buf_new() adds a byte for terminating NUL, but
|
170
|
+
// rb_str_resize() does not.
|
171
|
+
outbuf = rb_str_buf_new(ZPIX * YPIX - 1);
|
172
|
+
rb_str_resize(outbuf, ZPIX * YPIX);
|
173
|
+
|
174
|
+
rb_thread_call_without_gvl(
|
175
|
+
plot_side_blocking,
|
176
|
+
&(struct plot_info){.in = (uint16_t *)RSTRING_PTR(data), .out = (uint8_t *)RSTRING_PTR(outbuf)},
|
177
|
+
NULL,
|
178
|
+
NULL
|
179
|
+
);
|
180
|
+
|
181
|
+
return outbuf;
|
182
|
+
}
|
183
|
+
|
184
|
+
VALUE plot_front_blocking(void *data)
|
185
|
+
{
|
186
|
+
struct plot_info *info = data;
|
187
|
+
plot_front(info->in, info->out);
|
188
|
+
return Qnil;
|
189
|
+
}
|
190
|
+
|
191
|
+
// Ruby function to plot front view of depth data. Input: 640x480x16bit
|
192
|
+
// gray depth image. Output: XPIXxYPIX 8-bit gray image.
|
193
|
+
VALUE rb_plot_front(VALUE self, VALUE data)
|
194
|
+
{
|
195
|
+
VALUE outbuf;
|
196
|
+
size_t len;
|
197
|
+
|
198
|
+
// TODO: Allow reusing a previously-allocated output string
|
199
|
+
|
200
|
+
Check_Type(data, T_STRING);
|
201
|
+
len = RSTRING_LEN(data);
|
202
|
+
if(len < 640 * 480 * 2) {
|
203
|
+
rb_raise(rb_eArgError, "Input data must be at least 640*480*2 bytes (got %zu).", len);
|
204
|
+
}
|
205
|
+
|
206
|
+
// It seems rb_str_buf_new() adds a byte for terminating NUL, but
|
207
|
+
// rb_str_resize() does not.
|
208
|
+
outbuf = rb_str_buf_new(XPIX * YPIX - 1);
|
209
|
+
rb_str_resize(outbuf, XPIX * YPIX);
|
210
|
+
|
211
|
+
rb_thread_call_without_gvl(
|
212
|
+
plot_front_blocking,
|
213
|
+
&(struct plot_info){.in = (uint16_t *)RSTRING_PTR(data), .out = (uint8_t *)RSTRING_PTR(outbuf)},
|
214
|
+
NULL,
|
215
|
+
NULL
|
216
|
+
);
|
217
|
+
|
218
|
+
return outbuf;
|
219
|
+
}
|
220
|
+
|
221
|
+
// Unescapes a copy of the given string
|
222
|
+
// TODO: merge with rb_unescape_modify
|
223
|
+
VALUE rb_unescape(int argc, VALUE *args, VALUE self)
|
224
|
+
{
|
225
|
+
int dequote = 0;
|
226
|
+
int include_zero = 0;
|
227
|
+
VALUE str;
|
228
|
+
long len;
|
229
|
+
int ret;
|
230
|
+
|
231
|
+
if(argc > 2) {
|
232
|
+
rb_raise(rb_eArgError, "Only 0 to 2 parameters supported.");
|
233
|
+
}
|
234
|
+
if(argc >= 1) {
|
235
|
+
Check_Type(args[0], T_FIXNUM);
|
236
|
+
dequote = FIX2INT(args[0]);
|
237
|
+
if(dequote < 0 || dequote > 2) {
|
238
|
+
rb_raise(rb_eArgError, "First parameter (dequote) must be one of the ESCAPE_* constants.");
|
239
|
+
}
|
240
|
+
}
|
241
|
+
if(argc == 2) {
|
242
|
+
if(args[1] == Qtrue) {
|
243
|
+
include_zero = 1;
|
244
|
+
} else if(args[1] == Qfalse) {
|
245
|
+
include_zero = 0;
|
246
|
+
} else {
|
247
|
+
rb_raise(rb_eArgError, "Second parameter (include_zero) must be true or false.");
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
str = rb_str_dup(self);
|
252
|
+
if(!rb_enc_asciicompat(rb_enc_get(self))) {
|
253
|
+
str = rb_str_export_to_enc(str, utf8);
|
254
|
+
}
|
255
|
+
|
256
|
+
len = RSTRING_LEN(str);
|
257
|
+
ret = nl_unescape_string(RSTRING_PTR(str), include_zero, dequote);
|
258
|
+
if(ret == -1) {
|
259
|
+
rb_raise(rb_eRuntimeError, "Error unescaping string.");
|
260
|
+
}
|
261
|
+
|
262
|
+
rb_str_resize(str, len - ret);
|
263
|
+
|
264
|
+
return str;
|
265
|
+
}
|
266
|
+
|
267
|
+
// Unescapes the given string in place
|
268
|
+
VALUE rb_unescape_modify(int argc, VALUE *args, VALUE self)
|
269
|
+
{
|
270
|
+
int dequote = 0;
|
271
|
+
int include_zero = 0;
|
272
|
+
long len;
|
273
|
+
int ret;
|
274
|
+
|
275
|
+
if(argc > 2) {
|
276
|
+
rb_raise(rb_eArgError, "Only 0 to 2 parameters supported.");
|
277
|
+
}
|
278
|
+
if(argc >= 1) {
|
279
|
+
Check_Type(args[0], T_FIXNUM);
|
280
|
+
dequote = FIX2INT(args[0]);
|
281
|
+
if(dequote < 0 || dequote > 2) {
|
282
|
+
rb_raise(rb_eArgError, "First parameter (dequote) must be one of the ESCAPE_* constants.");
|
283
|
+
}
|
284
|
+
}
|
285
|
+
if(argc == 2) {
|
286
|
+
if(args[1] == Qtrue) {
|
287
|
+
include_zero = 1;
|
288
|
+
} else if(args[1] == Qfalse) {
|
289
|
+
include_zero = 0;
|
290
|
+
} else {
|
291
|
+
rb_raise(rb_eArgError, "Second parameter (include_zero) must be true or false.");
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
rb_check_frozen(self);
|
296
|
+
|
297
|
+
if(!rb_enc_asciicompat(rb_enc_get(self))) {
|
298
|
+
rb_raise(rb_eRuntimeError, "This method only works with ASCII-compatible encodings.");
|
299
|
+
}
|
300
|
+
|
301
|
+
len = RSTRING_LEN(self);
|
302
|
+
ret = nl_unescape_string(RSTRING_PTR(self), include_zero, dequote);
|
303
|
+
if(ret == -1) {
|
304
|
+
rb_raise(rb_eRuntimeError, "Error unescaping string.");
|
305
|
+
}
|
306
|
+
|
307
|
+
rb_str_resize(self, len - ret);
|
308
|
+
|
309
|
+
return self;
|
310
|
+
}
|
311
|
+
|
312
|
+
// Parsing callback for rb_kvp
|
313
|
+
static void kvp_hashcb(void *data, char *key, char *strvalue, struct nl_variant value)
|
314
|
+
{
|
315
|
+
struct kvp_info *info = data;
|
316
|
+
VALUE rbkey;
|
317
|
+
|
318
|
+
if (info->symbolize) {
|
319
|
+
rbkey = ID2SYM(rb_intern(key));
|
320
|
+
} else {
|
321
|
+
rbkey = rb_str_new2(key);
|
322
|
+
}
|
323
|
+
|
324
|
+
switch(value.type) {
|
325
|
+
case INTEGER:
|
326
|
+
rb_hash_aset(info->hash, rbkey, INT2NUM(value.value.integer));
|
327
|
+
break;
|
328
|
+
|
329
|
+
case FLOAT:
|
330
|
+
rb_hash_aset(info->hash, rbkey, rb_float_new(value.value.floating));
|
331
|
+
break;
|
332
|
+
|
333
|
+
case STRING:
|
334
|
+
rb_hash_aset(info->hash, rbkey, rb_str_new2(value.value.string));
|
335
|
+
break;
|
336
|
+
|
337
|
+
default:
|
338
|
+
rb_hash_aset(info->hash, rbkey, rb_str_new2(strvalue));
|
339
|
+
break;
|
340
|
+
}
|
341
|
+
}
|
342
|
+
|
343
|
+
// Parses a key-value pair string into a hash. The :symbolize_keys option may
|
344
|
+
// be specified to use symbols instead of strings for the hash keys.
|
345
|
+
VALUE rb_kvp(int argc, VALUE *argv, VALUE self)
|
346
|
+
{
|
347
|
+
VALUE hash = rb_hash_new();
|
348
|
+
|
349
|
+
if (argc < 0 || argc > 1 || (argc == 1 && !RB_TYPE_P(argv[0], T_HASH))) {
|
350
|
+
rb_raise(rb_eArgError, "Call with no parameters, or with an options Hash.");
|
351
|
+
}
|
352
|
+
|
353
|
+
VALUE symbolize = Qnil;
|
354
|
+
|
355
|
+
if (argc == 1) {
|
356
|
+
symbolize = rb_hash_lookup(argv[0], ID2SYM(rb_intern("symbolize_keys")));
|
357
|
+
}
|
358
|
+
|
359
|
+
struct kvp_info info = {
|
360
|
+
.hash = hash,
|
361
|
+
.symbolize = RB_TEST(symbolize),
|
362
|
+
};
|
363
|
+
|
364
|
+
nl_parse_kvp(RSTRING_PTR(self), nl_kvp_wrapper, &(struct nl_kvp_wrap){kvp_hashcb, &info});
|
365
|
+
|
366
|
+
return hash;
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
void Init_kinutils()
|
371
|
+
{
|
372
|
+
ku_init_lut();
|
373
|
+
|
374
|
+
VALUE nl = rb_define_module("NL");
|
375
|
+
VALUE knd_client = rb_define_module_under(nl, "KndClient");
|
376
|
+
|
377
|
+
KinUtils = rb_define_module_under(knd_client, "Kinutils");
|
378
|
+
|
379
|
+
utf8 = rb_enc_find("UTF-8");
|
380
|
+
if(!utf8) {
|
381
|
+
rb_raise(rb_eException, "No UTF-8 encoding.");
|
382
|
+
}
|
383
|
+
|
384
|
+
// FIXME: don't define global constants, put them in a namespace
|
385
|
+
|
386
|
+
rb_define_global_const("KNC_XPIX", INT2FIX(XPIX));
|
387
|
+
rb_define_global_const("KNC_YPIX", INT2FIX(YPIX));
|
388
|
+
rb_define_global_const("KNC_ZPIX", INT2FIX(ZPIX));
|
389
|
+
rb_define_global_const("KNC_PXZMAX", INT2FIX(PXZMAX));
|
390
|
+
rb_define_global_const("KNC_XMAX", INT2FIX(XMAX));
|
391
|
+
rb_define_global_const("KNC_YMAX", INT2FIX(YMAX));
|
392
|
+
rb_define_global_const("KNC_ZMAX", INT2FIX(ZMAX));
|
393
|
+
|
394
|
+
rb_define_global_const("ESCAPE_NO_DEQUOTE", INT2FIX(ESCAPE_NO_DEQUOTE));
|
395
|
+
rb_define_global_const("ESCAPE_DEQUOTE", INT2FIX(ESCAPE_DEQUOTE));
|
396
|
+
rb_define_global_const("ESCAPE_IF_QUOTED", INT2FIX(ESCAPE_IF_QUOTED));
|
397
|
+
|
398
|
+
rb_define_module_function(KinUtils, "unpack11_to_16", rb_unpack11_to_16, 1);
|
399
|
+
rb_define_module_function(KinUtils, "plot_linear", rb_plot_linear, 1);
|
400
|
+
rb_define_module_function(KinUtils, "plot_overhead", rb_plot_overhead, 1);
|
401
|
+
rb_define_module_function(KinUtils, "plot_side", rb_plot_side, 1);
|
402
|
+
rb_define_module_function(KinUtils, "plot_front", rb_plot_front, 1);
|
403
|
+
|
404
|
+
// TODO: Move to a different extension
|
405
|
+
rb_define_method(rb_cString, "kin_unescape", rb_unescape, -1);
|
406
|
+
rb_define_method(rb_cString, "kin_unescape!", rb_unescape_modify, -1);
|
407
|
+
|
408
|
+
rb_define_method(rb_cString, "kin_kvp", rb_kvp, -1);
|
409
|
+
|
410
|
+
// TODO: Add xworld,yworld,lut,reverse_lut,unpack_to_world/unpack_to_8 functions
|
411
|
+
}
|