potracer 1.0.9 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/potracer/potracer.c +189 -13
- data/lib/potracer.rb +43 -1
- metadata +2 -2
data/ext/potracer/potracer.c
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
#include "potracer.h"
|
7
7
|
|
8
8
|
static int
|
9
|
-
is_colored (char *str, int row, int col, int cols, int size)
|
9
|
+
is_colored (unsigned char *str, int row, int col, int cols, int size)
|
10
10
|
{
|
11
11
|
int i, start;
|
12
12
|
unsigned char v, max, min;
|
@@ -36,6 +36,11 @@ params_alloc (VALUE klass)
|
|
36
36
|
return Data_Wrap_Struct(klass, 0, potrace_param_free, params);
|
37
37
|
}
|
38
38
|
|
39
|
+
/**
|
40
|
+
* call-seq: turd_size
|
41
|
+
*
|
42
|
+
* Get the current turd size
|
43
|
+
*/
|
39
44
|
static VALUE
|
40
45
|
params_get_turdsize (VALUE klass)
|
41
46
|
{
|
@@ -44,6 +49,13 @@ params_get_turdsize (VALUE klass)
|
|
44
49
|
return rb_int_new(params->turdsize);
|
45
50
|
}
|
46
51
|
|
52
|
+
/**
|
53
|
+
* call-seq: turd_size = threshold
|
54
|
+
*
|
55
|
+
* Set the turd size
|
56
|
+
* * +threshold+ - used to "despeckle" the bitmap to be traced, by removing all
|
57
|
+
* curves whose enclosed area is below the given threshold. Default is 2.
|
58
|
+
*/
|
47
59
|
static VALUE
|
48
60
|
params_set_turdsize (VALUE klass, VALUE size)
|
49
61
|
{
|
@@ -53,6 +65,11 @@ params_set_turdsize (VALUE klass, VALUE size)
|
|
53
65
|
return size;
|
54
66
|
}
|
55
67
|
|
68
|
+
/**
|
69
|
+
* call-seq: turn_policy
|
70
|
+
*
|
71
|
+
* Get the current Turnpolicy
|
72
|
+
*/
|
56
73
|
static VALUE
|
57
74
|
params_get_turnpolicy (VALUE klass)
|
58
75
|
{
|
@@ -61,6 +78,11 @@ params_get_turnpolicy (VALUE klass)
|
|
61
78
|
return rb_int_new(params->turnpolicy);
|
62
79
|
}
|
63
80
|
|
81
|
+
/**
|
82
|
+
* call-seq: turn_policy = policy
|
83
|
+
*
|
84
|
+
* Set the current Turnpolicy
|
85
|
+
*/
|
64
86
|
static VALUE
|
65
87
|
params_set_turnpolicy (VALUE klass, VALUE policy)
|
66
88
|
{
|
@@ -70,6 +92,11 @@ params_set_turnpolicy (VALUE klass, VALUE policy)
|
|
70
92
|
return policy;
|
71
93
|
}
|
72
94
|
|
95
|
+
/**
|
96
|
+
* call-seq: alpha_max
|
97
|
+
*
|
98
|
+
* Get the current alpha max
|
99
|
+
*/
|
73
100
|
static VALUE
|
74
101
|
params_get_alphamax (VALUE klass)
|
75
102
|
{
|
@@ -78,6 +105,15 @@ params_get_alphamax (VALUE klass)
|
|
78
105
|
return rb_float_new(params->alphamax);
|
79
106
|
}
|
80
107
|
|
108
|
+
/**
|
109
|
+
* call-seq: alpha_max = max
|
110
|
+
*
|
111
|
+
* Set the alpha max
|
112
|
+
*
|
113
|
+
* * +max+ - threshold for the detection of corners. It controls the smoothness
|
114
|
+
* of the traced curve, as shown in Figure 9. The current default is 1.0. The
|
115
|
+
* useful range of this parameter is from 0.0 (polygon) to 1.3334 (no corners).
|
116
|
+
*/
|
81
117
|
static VALUE
|
82
118
|
params_set_alphamax (VALUE klass, VALUE max)
|
83
119
|
{
|
@@ -87,6 +123,11 @@ params_set_alphamax (VALUE klass, VALUE max)
|
|
87
123
|
return max;
|
88
124
|
}
|
89
125
|
|
126
|
+
/**
|
127
|
+
* call-seq: optimized?
|
128
|
+
*
|
129
|
+
* Are curves optimized
|
130
|
+
*/
|
90
131
|
static VALUE
|
91
132
|
params_get_opticurve (VALUE klass)
|
92
133
|
{
|
@@ -95,6 +136,11 @@ params_get_opticurve (VALUE klass)
|
|
95
136
|
return (params->opticurve == 1) ? Qtrue : Qfalse;
|
96
137
|
}
|
97
138
|
|
139
|
+
/**
|
140
|
+
* call-seq: optimize!
|
141
|
+
*
|
142
|
+
* Turn on curve optimization
|
143
|
+
*/
|
98
144
|
static VALUE
|
99
145
|
params_set_opticurve_true (VALUE klass)
|
100
146
|
{
|
@@ -104,6 +150,11 @@ params_set_opticurve_true (VALUE klass)
|
|
104
150
|
return Qtrue;
|
105
151
|
}
|
106
152
|
|
153
|
+
/**
|
154
|
+
* call-seq: unoptimize!
|
155
|
+
*
|
156
|
+
* Turn off curve optimization
|
157
|
+
*/
|
107
158
|
static VALUE
|
108
159
|
params_set_opticurve_false (VALUE klass)
|
109
160
|
{
|
@@ -113,6 +164,11 @@ params_set_opticurve_false (VALUE klass)
|
|
113
164
|
return Qfalse;
|
114
165
|
}
|
115
166
|
|
167
|
+
/**
|
168
|
+
* call-seq: tolerance
|
169
|
+
*
|
170
|
+
* Get current curve optimization tolerance
|
171
|
+
*/
|
116
172
|
static VALUE
|
117
173
|
params_get_opttolerance (VALUE klass)
|
118
174
|
{
|
@@ -121,6 +177,18 @@ params_get_opttolerance (VALUE klass)
|
|
121
177
|
return rb_float_new(params->opttolerance);
|
122
178
|
}
|
123
179
|
|
180
|
+
/**
|
181
|
+
* call-seq: tolerance = amount
|
182
|
+
*
|
183
|
+
* Set current curve optimization tolerance
|
184
|
+
*
|
185
|
+
* * +amount+ - defines the amount of error allowed in curve simplification.
|
186
|
+
* This value only applies if optimization is true. The current default is
|
187
|
+
* 0.2. Larger values tend to decrease the number of segments, at the expense
|
188
|
+
* of less accuracy. The useful range is from 0 to infinity, although in
|
189
|
+
* practice one would hardly choose values greater than 1 or so. For most
|
190
|
+
* purposes, the default value is a good tradeoff between space and accuracy.
|
191
|
+
*/
|
124
192
|
static VALUE
|
125
193
|
params_set_opttolerance (VALUE klass, VALUE tolerance)
|
126
194
|
{
|
@@ -166,6 +234,27 @@ trace_trace (VALUE obj, VALUE bitmap, VALUE params)
|
|
166
234
|
return obj;
|
167
235
|
}
|
168
236
|
|
237
|
+
/**
|
238
|
+
* call-seq: to_svg
|
239
|
+
*
|
240
|
+
* Render the traced bitmap as an SVG
|
241
|
+
*
|
242
|
+
* ==== Example
|
243
|
+
*
|
244
|
+
* bmp = Potracer::Bitmap.new(5, 5, [
|
245
|
+
* [1, 1, 1, 1, 1],
|
246
|
+
* [1, 0, 0, 0, 1],
|
247
|
+
* [1, 0, 0, 0, 1],
|
248
|
+
* [1, 0, 0, 0, 1],
|
249
|
+
* [1, 1, 1, 1, 1]
|
250
|
+
* ])
|
251
|
+
* params = Potracer::Params.new
|
252
|
+
* trace = Potracer::Trace.new
|
253
|
+
* trace.trace(bmp, params).to_svg # =>
|
254
|
+
* # <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="5" height="5">
|
255
|
+
* # <path fill-rule="evenodd" fill="rgb(0,0,0)" d="M 0.000000 2.500000 C 0.000000 0.500000 0.500000 0.000000 2.500000 0.000000C 4.500000 0.000000 5.000000 0.500000 5.000000 2.500000C 5.000000 4.500000 4.500000 5.000000 2.500000 5.000000C 0.500000 5.000000 0.000000 4.500000 0.000000 2.500000M 4.000000 2.500000 C 4.000000 1.675000 3.325000 1.000000 2.500000 1.000000C 1.675000 1.000000 1.000000 1.675000 1.000000 2.500000C 1.000000 3.325000 1.675000 4.000000 2.500000 4.000000C 3.325000 4.000000 4.000000 3.325000 4.000000 2.500000" />
|
256
|
+
* # </svg>"
|
257
|
+
*/
|
169
258
|
static VALUE
|
170
259
|
trace_as_svg (VALUE klass)
|
171
260
|
{
|
@@ -217,6 +306,28 @@ trace_as_svg (VALUE klass)
|
|
217
306
|
return svg;
|
218
307
|
}
|
219
308
|
|
309
|
+
/**
|
310
|
+
* call-seq: to_a
|
311
|
+
*
|
312
|
+
* Convert the traced bitmap to an array in the form:
|
313
|
+
*
|
314
|
+
* [
|
315
|
+
* {:area=>25, :sign=>"+", :parts=> [
|
316
|
+
* [:moveto, 0.0, 2.5],
|
317
|
+
* [:curveto, 0.0, 0.5, 0.5, 0.0, 2.5, 0.0],
|
318
|
+
* [:curveto, 4.5, 0.0, 5.0, 0.5, 5.0, 2.5],
|
319
|
+
* [:curveto, 5.0, 4.5, 4.5, 5.0, 2.5, 5.0],
|
320
|
+
* [:curveto, 0.5, 5.0, 0.0, 4.5, 0.0, 2.5]
|
321
|
+
* ]},
|
322
|
+
* {:area=>9, :sign=>"-", :parts=> [
|
323
|
+
* [:moveto, 4.0, 2.5],
|
324
|
+
* [:curveto, 4.0, 1.6749999999999998, 3.325, 1.0, 2.5, 1.0],
|
325
|
+
* [:curveto, 1.6749999999999998, 1.0, 1.0, 1.6749999999999998, 1.0, 2.5],
|
326
|
+
* [:curveto, 1.0, 3.325, 1.6749999999999998, 4.0, 2.5, 4.0],
|
327
|
+
* [:curveto, 3.325, 4.0, 4.0, 3.325, 4.0, 2.5]
|
328
|
+
* ]}
|
329
|
+
* ]
|
330
|
+
*/
|
220
331
|
static VALUE
|
221
332
|
trace_as_array (VALUE klass)
|
222
333
|
{
|
@@ -273,16 +384,21 @@ bitmap_free (potrace_bitmap_t *bm)
|
|
273
384
|
free(bm);
|
274
385
|
}
|
275
386
|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
387
|
+
/**
|
388
|
+
* call-seq: new(width=250, height=250, bits='000...', map='RGB')
|
389
|
+
*
|
390
|
+
* Creat a new Bitmap
|
391
|
+
*
|
392
|
+
* * +width+ - width of the bitmap to be traced
|
393
|
+
* * +height+ - height of the bitmap to be traced
|
394
|
+
* * +bits+ - bitmap data. Can be a multi-dimensional array or a string of pixel
|
395
|
+
* data
|
396
|
+
* * +map+ - how pixel data is mapped if +bits+ is a string
|
397
|
+
*/
|
282
398
|
static VALUE
|
283
399
|
bitmap_new (int argc, VALUE *argv, VALUE klass)
|
284
400
|
{
|
285
|
-
int i, j, m
|
401
|
+
int i, j, m;
|
286
402
|
unsigned char *bits;
|
287
403
|
potrace_bitmap_t *bm;
|
288
404
|
VALUE bdata, row;
|
@@ -296,7 +412,7 @@ bitmap_new (int argc, VALUE *argv, VALUE klass)
|
|
296
412
|
|
297
413
|
if (argc > 2) {
|
298
414
|
if (T_STRING == TYPE(argv[2])) {
|
299
|
-
bits = StringValuePtr(argv[2]);
|
415
|
+
bits = (unsigned char *)StringValuePtr(argv[2]);
|
300
416
|
m = strlen(StringValuePtr(argv[3]));
|
301
417
|
for (i = 0; i < bm->h; i++) {
|
302
418
|
for (j = 0; j < bm->w; j++) {
|
@@ -316,6 +432,11 @@ bitmap_new (int argc, VALUE *argv, VALUE klass)
|
|
316
432
|
return bdata;
|
317
433
|
}
|
318
434
|
|
435
|
+
/**
|
436
|
+
* call-seq: width
|
437
|
+
*
|
438
|
+
* Get the width in pixels
|
439
|
+
*/
|
319
440
|
static VALUE
|
320
441
|
bitmap_get_width (VALUE klass)
|
321
442
|
{
|
@@ -324,6 +445,11 @@ bitmap_get_width (VALUE klass)
|
|
324
445
|
return rb_int_new(bm->w);
|
325
446
|
}
|
326
447
|
|
448
|
+
/**
|
449
|
+
* call-seq: height
|
450
|
+
*
|
451
|
+
* Get the height in pixels
|
452
|
+
*/
|
327
453
|
static VALUE
|
328
454
|
bitmap_get_height (VALUE klass)
|
329
455
|
{
|
@@ -332,6 +458,11 @@ bitmap_get_height (VALUE klass)
|
|
332
458
|
return rb_int_new(bm->h);
|
333
459
|
}
|
334
460
|
|
461
|
+
/**
|
462
|
+
* call-seq: to_a
|
463
|
+
*
|
464
|
+
* Retrieve the bitmap data as a multi-dimensional array of 1s and 0s
|
465
|
+
*/
|
335
466
|
static VALUE
|
336
467
|
bitmap_as_array (VALUE klass)
|
337
468
|
{
|
@@ -353,8 +484,12 @@ bitmap_as_array (VALUE klass)
|
|
353
484
|
|
354
485
|
void
|
355
486
|
Init_potracer () {
|
356
|
-
|
487
|
+
/**
|
488
|
+
* Define-module: Potracer
|
489
|
+
* A home for all things Potrace
|
490
|
+
*/
|
357
491
|
rb_mPotracer = rb_define_module("Potracer");
|
492
|
+
/* VERSION: Version of Potrace */
|
358
493
|
rb_define_const(rb_mPotracer, "VERSION", rb_str_new2(potrace_version()));
|
359
494
|
|
360
495
|
// Define the Trace class inside the Potracer module
|
@@ -364,7 +499,11 @@ Init_potracer () {
|
|
364
499
|
rb_define_method(rb_cPotracerTrace, "to_a", trace_as_array, 0);
|
365
500
|
rb_define_method(rb_cPotracerTrace, "to_svg", trace_as_svg, 0);
|
366
501
|
|
367
|
-
|
502
|
+
/**
|
503
|
+
* Document-class: Potracer::Params
|
504
|
+
*
|
505
|
+
* Params define how the Bitmap is traced
|
506
|
+
*/
|
368
507
|
rb_cPotracerParams = rb_define_class_under(rb_mPotracer, "Params", rb_cObject);
|
369
508
|
rb_define_alloc_func(rb_cPotracerParams, params_alloc);
|
370
509
|
rb_define_method(rb_cPotracerParams, "turd_size", params_get_turdsize, 0);
|
@@ -379,7 +518,41 @@ Init_potracer () {
|
|
379
518
|
rb_define_method(rb_cPotracerParams, "tolerance", params_get_opttolerance, 0);
|
380
519
|
rb_define_method(rb_cPotracerParams, "tolerance=", params_set_opttolerance, 1);
|
381
520
|
|
382
|
-
|
521
|
+
/**
|
522
|
+
* Document-const: BLACK
|
523
|
+
* prefers to connect black (foreground) components
|
524
|
+
*/
|
525
|
+
/**
|
526
|
+
* Document-const: WHITE
|
527
|
+
* prefers to connect white (background) components
|
528
|
+
*/
|
529
|
+
/**
|
530
|
+
* Document-const: LEFT
|
531
|
+
* always take a left turn
|
532
|
+
*/
|
533
|
+
/**
|
534
|
+
* Document-const: RIGHT
|
535
|
+
* always take a right turn
|
536
|
+
*/
|
537
|
+
/**
|
538
|
+
* Document-const: MINORITY
|
539
|
+
* prefers to connect the color (black or white) that occurs least
|
540
|
+
* frequently in a local neighborhood of the current position
|
541
|
+
*/
|
542
|
+
/**
|
543
|
+
* Document-const: MAJORITY
|
544
|
+
* prefers to connect the color (black or white) that occurs most
|
545
|
+
* frequently in a local neighborhood of the current position
|
546
|
+
*/
|
547
|
+
/**
|
548
|
+
* Document-const: RANDOM
|
549
|
+
* choose pseudo-ramdomly
|
550
|
+
*/
|
551
|
+
/**
|
552
|
+
* Document-class: Potracer::Turnpolicy
|
553
|
+
* The Turnpolicy determines how to resolve ambiguities during decomposition
|
554
|
+
* of bitmaps into paths.
|
555
|
+
*/
|
383
556
|
rb_mTurnpolicy = rb_define_module_under(rb_mPotracer, "Turnpolicy");
|
384
557
|
rb_define_const(rb_mTurnpolicy, "BLACK", rb_int_new(POTRACE_TURNPOLICY_BLACK));
|
385
558
|
rb_define_const(rb_mTurnpolicy, "WHITE", rb_int_new(POTRACE_TURNPOLICY_WHITE));
|
@@ -389,7 +562,10 @@ Init_potracer () {
|
|
389
562
|
rb_define_const(rb_mTurnpolicy, "MAJORITY", rb_int_new(POTRACE_TURNPOLICY_MAJORITY));
|
390
563
|
rb_define_const(rb_mTurnpolicy, "RANDOM", rb_int_new(POTRACE_TURNPOLICY_RANDOM));
|
391
564
|
|
392
|
-
|
565
|
+
/**
|
566
|
+
* Document-class: Potracer::Bitmap
|
567
|
+
* The Bitmap is Potracer's representation of a bitmap in memory
|
568
|
+
*/
|
393
569
|
rb_cPotracerBitmap = rb_define_class_under(rb_mPotracer, "Bitmap", rb_cObject);
|
394
570
|
rb_define_singleton_method(rb_cPotracerBitmap, "new", bitmap_new, -1);
|
395
571
|
rb_define_method(rb_cPotracerBitmap, "width", bitmap_get_width, 0);
|
data/lib/potracer.rb
CHANGED
@@ -1,7 +1,27 @@
|
|
1
|
-
|
1
|
+
begin
|
2
|
+
require_relative '../build/potracer'
|
3
|
+
rescue LoadError
|
4
|
+
require_relative '../ext/potracer/potracer'
|
5
|
+
end
|
2
6
|
|
3
7
|
module Potracer
|
8
|
+
|
9
|
+
##
|
10
|
+
# This class represents a trace of a Potracer::Bitmap
|
11
|
+
|
4
12
|
class Trace
|
13
|
+
|
14
|
+
##
|
15
|
+
# Trace the given +bitmap+
|
16
|
+
#
|
17
|
+
# ==== Attributes
|
18
|
+
#
|
19
|
+
# * +bitmap+ - an instance of Potracer::Bitmap. If not given the +@bitmap+
|
20
|
+
# is used.
|
21
|
+
# * +params+ - an instance of Potracer::Params. If not given +@params+ is
|
22
|
+
# used.
|
23
|
+
# * +block+ - optional block called to report trace progress
|
24
|
+
|
5
25
|
def trace(bitmap=nil, params=nil, &block)
|
6
26
|
if block_given?
|
7
27
|
self.do_trace(bitmap || @bitmap, params || @params, &block)
|
@@ -10,6 +30,28 @@ module Potracer
|
|
10
30
|
end
|
11
31
|
end
|
12
32
|
|
33
|
+
##
|
34
|
+
# Trace a bitmap
|
35
|
+
#
|
36
|
+
# ==== Attributes
|
37
|
+
#
|
38
|
+
# * +bmp+ - mapped to a Potracer::Bitmap either a multi-dimensional array of
|
39
|
+
# bits or a string of image data
|
40
|
+
# * +width+ - width of the image to be mapped if +bmp+ is a string
|
41
|
+
# * +height+ - height of the image to be mapped if +bmp+ is a string
|
42
|
+
# * +map+ - pixel data format if +bmp+ is a string
|
43
|
+
# * +block+ - optional block called to report trace progress
|
44
|
+
#
|
45
|
+
# ==== Example
|
46
|
+
#
|
47
|
+
# require 'ruby-progressbar'
|
48
|
+
#
|
49
|
+
# pbar = ProgressBar.create(title: 'Tracing')
|
50
|
+
# Potracer::Trace.bitmap(bits) do |percent|
|
51
|
+
# pbar.progress = percent
|
52
|
+
# end
|
53
|
+
# pbar.finish
|
54
|
+
|
13
55
|
def self.bitmap(bmp, width=nil, height=nil, map='RGB', &block)
|
14
56
|
width ||= bmp.map {|r| r.length}.max
|
15
57
|
height ||= bmp.length
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: potracer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Ruby bindings for the potrace library.
|
15
15
|
email: k.parnell@gmail.com
|