libpuzzle 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 063f0b11bf6d3bfdfac0d2ceb1a43164ba932efd
4
+ data.tar.gz: a5ee426bba01a0111e00224702a188be9418d7d7
5
+ SHA512:
6
+ metadata.gz: 7dd841ae56dba6c16104a067a78cb906fd54c94a3d116c659906cf3c479f08bd27e1d680de79df58eaf61d063541458541ab0f4e859acfaa34fb5232302ad29c
7
+ data.tar.gz: 113975d66374e75161b0a2f17b530d3167e9d5a3ba74bb01aa047bdfcdaf85c2bff617db5361a730f91df41a9eded5cd34d8e1beb211eeb2c050d368003607d1
data/ext/extconf.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+ have_library('puzzle')
3
+ have_header('puzzle.h')
4
+ create_makefile('puzzle_ext')
data/ext/libpuzzle.c ADDED
@@ -0,0 +1,88 @@
1
+ #include <stddef.h>
2
+ #include <puzzle.h>
3
+ #include "ruby.h"
4
+
5
+ VALUE rb_cPuzzle, rb_eError;
6
+
7
+ typedef struct Puzzle{
8
+ PuzzleContext context;
9
+ PuzzleCvec vector;
10
+ } Puzzle;
11
+
12
+ void free_puzzle(Puzzle *puzzle){
13
+ puzzle_free_context(&puzzle->context);
14
+ puzzle_free_cvec(&puzzle->context, &puzzle->vector);
15
+ }
16
+
17
+ VALUE new(VALUE klass, VALUE file_path){
18
+ Puzzle *puzzle;
19
+ VALUE ret = Data_Make_Struct(klass, Puzzle, 0, free_puzzle, puzzle);
20
+ puzzle_init_context(&puzzle->context);
21
+ puzzle_init_cvec(&puzzle->context, &puzzle->vector);
22
+
23
+ if(puzzle_fill_cvec_from_file(&puzzle->context, &puzzle->vector, StringValueCStr(file_path)) < 0)
24
+ rb_raise(rb_eError, "failed to open file");
25
+
26
+ VALUE rb_vector = rb_str_new((const char*)puzzle->vector.vec, puzzle->vector.sizeof_vec);
27
+
28
+ rb_iv_set(ret, "@vector", rb_vector);
29
+ return ret;
30
+ }
31
+
32
+ double _distance(VALUE p1, VALUE p2){
33
+ Puzzle *puzzle_1, *puzzle_2;
34
+ Data_Get_Struct(p1, Puzzle, puzzle_1);
35
+ Data_Get_Struct(p2, Puzzle, puzzle_2);
36
+ double dist = puzzle_vector_normalized_distance(&puzzle_1->context, &puzzle_1->vector, &puzzle_2->vector, 1);
37
+ if(dist < 0) rb_raise(rb_eError, "failed to compare vectors");
38
+ return dist;
39
+ }
40
+
41
+ VALUE distance(VALUE p1, VALUE p2){
42
+ return rb_float_new(_distance(p1, p2));
43
+ }
44
+
45
+ VALUE equal(VALUE p1, VALUE p2){
46
+ return _distance(p1, p2) < 0.6 ? Qtrue : Qfalse;
47
+ }
48
+
49
+ VALUE compress(VALUE self){
50
+ Puzzle *puzzle;
51
+ PuzzleCompressedCvec c_cvec;
52
+ Data_Get_Struct(self, Puzzle, puzzle);
53
+ puzzle_init_compressed_cvec(&puzzle->context, &c_cvec);
54
+
55
+ puzzle_compress_cvec(&puzzle->context, &c_cvec, &puzzle->vector);
56
+ VALUE ret = rb_str_new((const char*)c_cvec.vec, c_cvec.sizeof_compressed_vec);
57
+ puzzle_free_compressed_cvec(&puzzle->context, &c_cvec);
58
+ return ret;
59
+ }
60
+
61
+ VALUE uncompress(VALUE klass, VALUE compressed_string){
62
+ Puzzle *puzzle;
63
+ PuzzleCompressedCvec c_cvec;
64
+
65
+ VALUE ret = Data_Make_Struct(klass, Puzzle, 0, free_puzzle, puzzle);
66
+ puzzle_init_context(&puzzle->context);
67
+ puzzle_init_cvec(&puzzle->context, &puzzle->vector);
68
+ c_cvec.vec = (unsigned char*)RSTRING_PTR(compressed_string);
69
+ c_cvec.sizeof_compressed_vec = RSTRING_LEN(compressed_string);
70
+
71
+ puzzle_uncompress_cvec(&puzzle->context, &c_cvec, &puzzle->vector);
72
+
73
+ VALUE rb_vector = rb_str_new((const char*)puzzle->vector.vec, puzzle->vector.sizeof_vec);
74
+ rb_iv_set(ret, "@vector", rb_vector);
75
+ return ret;
76
+ }
77
+
78
+ void Init_puzzle_ext(void){
79
+ rb_cPuzzle = rb_define_class("Puzzle", rb_cObject);
80
+ rb_eError = rb_define_class_under(rb_cPuzzle, "Error", rb_eRuntimeError);
81
+
82
+ rb_define_attr(rb_cPuzzle, "vector", 1, 0);
83
+ rb_define_singleton_method(rb_cPuzzle, "new", new, 1);
84
+ rb_define_singleton_method(rb_cPuzzle, "uncompress", uncompress, 1);
85
+ rb_define_method(rb_cPuzzle, "distance", distance, 1);
86
+ rb_define_method(rb_cPuzzle, "==", equal, 1);
87
+ rb_define_method(rb_cPuzzle, "compress", compress, 0);
88
+ }
data/lib/libpuzzle.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'puzzle_ext'
2
+ require 'puzzle/version'
@@ -0,0 +1,3 @@
1
+ class Puzzle
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libpuzzle
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jian Weihang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This is a C extension for libpuzzle to find similar pictures with easy
70
+ API.
71
+ email: tonytonyjan@gmail.com
72
+ executables: []
73
+ extensions:
74
+ - ext/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ext/extconf.rb
78
+ - ext/libpuzzle.c
79
+ - lib/libpuzzle.rb
80
+ - lib/puzzle/version.rb
81
+ homepage: https://github.com/tonytonyjan/libpuzzle_ruby
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A library to find similar pictures with libpuzzle
105
+ test_files: []