chunky_png_subimage 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +34 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +4 -0
- data/Rakefile +12 -0
- data/chunky_png_subimage.gemspec +30 -0
- data/example/test.rb +7 -0
- data/ext/chunky_png_subimage/chunky_png_subimage.c +183 -0
- data/ext/chunky_png_subimage/chunky_png_subimage.h +8 -0
- data/ext/chunky_png_subimage/extconf.rb +5 -0
- data/lib/chunky_png_subimage/search.rb +29 -0
- data/lib/chunky_png_subimage/version.rb +3 -0
- data/lib/chunky_png_subimage.rb +4 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2e9df7bd32bd43906739c994e0dfc54f9b1df175
|
4
|
+
data.tar.gz: 2210412b77754fad0cea4efa4fb940b7ee74da8a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4865f1d99cb6ca3a48d07f63c44ab737ef068ed5bb5cd5bfbe8f6f9ba4eddd6302ec063804749d44047de0ba4fa61b60bd53acdc100b02b741f7138d3926ef8
|
7
|
+
data.tar.gz: 3a50a3a5a383b57e1e81468229694d0f14e4ae03c88e977ddc740eabada4a7a80afc5a7834f6aa545540f51328272ed371cb450317a3eeb0810293ee99c96116
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
# Gemfile.lock
|
30
|
+
# .ruby-version
|
31
|
+
# .ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Denis Mingulov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake/extensiontask'
|
4
|
+
Rake::ExtensionTask.new('chunky_png_subimage') do |ext|
|
5
|
+
ext.lib_dir = File.join('lib', 'chunky_png_subimage')
|
6
|
+
#ext.config_options = '--with-cflags="-std=c99"'
|
7
|
+
end
|
8
|
+
|
9
|
+
#require 'rspec/core/rake_task'
|
10
|
+
#RSpec::Core::RakeTask.new(:spec)
|
11
|
+
|
12
|
+
task :default => [:compile]
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'chunky_png_subimage/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "chunky_png_subimage"
|
8
|
+
spec.version = ChunkyPNGSubimage::VERSION
|
9
|
+
spec.authors = ["Denis Mingulov"]
|
10
|
+
spec.email = ["denis@mingulov.com"]
|
11
|
+
spec.description = %q{Support to search subimages for ChunkyPNG - fast with ruby extension}
|
12
|
+
spec.summary = %q{Support to search subimages for ChunkyPNG}
|
13
|
+
spec.homepage = "http://github.com/mingulov/chunky_png_subimage"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.required_ruby_version = '>= 1.9'
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.extensions = spec.files.grep(%r{/extconf.rb$})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency('chunky_png', '~> 1.3')
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "rspec", "~> 2.6"
|
29
|
+
spec.add_development_dependency "rake-compiler"
|
30
|
+
end
|
data/example/test.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
#include "chunky_png_subimage.h"
|
2
|
+
|
3
|
+
static inline int is_color_usable(unsigned int color)
|
4
|
+
{
|
5
|
+
return (color & 0xff) == 0xff;
|
6
|
+
}
|
7
|
+
|
8
|
+
static VALUE search_single_subimage(const unsigned int *img, const int imgWidth, const int imgHeight,
|
9
|
+
const int searchX, const int searchY, const int searchWidth, const int searchHeight,
|
10
|
+
const unsigned int *sub, const int subWidth, const int subHeight, int singleMode)
|
11
|
+
{
|
12
|
+
VALUE result = rb_ary_new();
|
13
|
+
|
14
|
+
int s1y = 0;
|
15
|
+
int s1x = 0;
|
16
|
+
unsigned int c;
|
17
|
+
int y;
|
18
|
+
|
19
|
+
/* search 1st point usable to do the main search */
|
20
|
+
for (s1y = 0; s1y < subHeight; s1y++)
|
21
|
+
{
|
22
|
+
for (s1x = 0; s1x < subWidth; s1x++)
|
23
|
+
{
|
24
|
+
if (is_color_usable(sub[s1y * subWidth + s1x]))
|
25
|
+
break;
|
26
|
+
}
|
27
|
+
if (s1x != subWidth)
|
28
|
+
break;
|
29
|
+
}
|
30
|
+
|
31
|
+
if (s1y == subHeight)
|
32
|
+
rb_raise(rb_eStandardError, "Empty subimage is provided, no usable pixels");
|
33
|
+
|
34
|
+
c = sub[s1y * subWidth + s1x];
|
35
|
+
|
36
|
+
for (y = searchY; y < searchY + searchHeight; y++)
|
37
|
+
{
|
38
|
+
int x = 0;
|
39
|
+
for (x = searchX; x < searchX + searchWidth; x++)
|
40
|
+
{
|
41
|
+
if (img[(y + s1y) * imgWidth + x + s1x] == c)
|
42
|
+
{
|
43
|
+
/* found 1st possible point, do the full check */
|
44
|
+
int sy = 0;
|
45
|
+
for (sy = s1y; sy < subHeight; sy++)
|
46
|
+
{
|
47
|
+
int sx = 0;
|
48
|
+
for (sx = (sy == s1y) ? s1x : 0; sx < subWidth; sx++)
|
49
|
+
{
|
50
|
+
const unsigned int sc = sub[sy * subWidth + sx];
|
51
|
+
if (is_color_usable(sc) && sc != img[(y + sy) * imgWidth + x + sx])
|
52
|
+
break;
|
53
|
+
}
|
54
|
+
if (sx != subWidth)
|
55
|
+
break;
|
56
|
+
}
|
57
|
+
|
58
|
+
if (sy == subHeight)
|
59
|
+
{
|
60
|
+
/* subimage is found */
|
61
|
+
VALUE coord = rb_ary_new();
|
62
|
+
rb_ary_push(coord, INT2FIX(x));
|
63
|
+
rb_ary_push(coord, INT2FIX(y));
|
64
|
+
rb_ary_push(result, coord);
|
65
|
+
|
66
|
+
if (singleMode & 2)
|
67
|
+
break;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
/* if 'break' has been used */
|
72
|
+
if (x != searchX + searchWidth)
|
73
|
+
break;
|
74
|
+
}
|
75
|
+
|
76
|
+
return result;
|
77
|
+
}
|
78
|
+
|
79
|
+
/* convert image to int*, xfree must be used later */
|
80
|
+
static unsigned int *convert_image(const VALUE *imgPixels, const int imgWidth, const int imgHeight)
|
81
|
+
{
|
82
|
+
unsigned int *imgReady = xmalloc(sizeof(unsigned int) * imgWidth * imgHeight);
|
83
|
+
int i;
|
84
|
+
for(i = 0; i < imgHeight * imgWidth; i++)
|
85
|
+
{
|
86
|
+
imgReady[i] = FIX2UINT(imgPixels[i]);
|
87
|
+
}
|
88
|
+
|
89
|
+
return imgReady;
|
90
|
+
}
|
91
|
+
|
92
|
+
VALUE search_subimages(int argc, VALUE *argv, VALUE self)
|
93
|
+
{
|
94
|
+
/*
|
95
|
+
params = [img.pixels, img.width, img.height, x, y, width, height, single, subimages.length]
|
96
|
+
subimages.each do |s|
|
97
|
+
params << s.pixels
|
98
|
+
params << s.width
|
99
|
+
params << s.height
|
100
|
+
end
|
101
|
+
*/
|
102
|
+
|
103
|
+
VALUE result = rb_ary_new();
|
104
|
+
const VALUE *imgPixels;
|
105
|
+
int imgWidth;
|
106
|
+
int imgHeight;
|
107
|
+
int searchX;
|
108
|
+
int searchY;
|
109
|
+
int searchWidth;
|
110
|
+
int searchHeight;
|
111
|
+
int singleMode;
|
112
|
+
int amountSubimages;
|
113
|
+
const unsigned int *imgReady;
|
114
|
+
int cur = 9;
|
115
|
+
int i;
|
116
|
+
|
117
|
+
if (argc < 12 || (argc - 9) % 3 != 0)
|
118
|
+
rb_raise(rb_eArgError, "wrong number of arguments");
|
119
|
+
|
120
|
+
imgPixels = RARRAY_PTR(argv[0]);
|
121
|
+
imgWidth = FIX2INT(argv[1]);
|
122
|
+
imgHeight = FIX2INT(argv[2]);
|
123
|
+
searchX = FIX2INT(argv[3]);
|
124
|
+
searchY = FIX2INT(argv[4]);
|
125
|
+
searchWidth = FIX2INT(argv[5]);
|
126
|
+
searchHeight = FIX2INT(argv[6]);
|
127
|
+
singleMode = FIX2INT(argv[7]);
|
128
|
+
amountSubimages = FIX2INT(argv[8]);
|
129
|
+
|
130
|
+
/* some parameters checks */
|
131
|
+
if (searchX < 0)
|
132
|
+
searchX = 0;
|
133
|
+
if (searchY < 0)
|
134
|
+
searchY = 0;
|
135
|
+
if (searchX >= imgWidth)
|
136
|
+
searchX = imgWidth - 1;
|
137
|
+
if (searchY >= imgHeight)
|
138
|
+
searchY = imgHeight - 1;
|
139
|
+
if (searchWidth <= 0)
|
140
|
+
searchWidth = imgWidth;
|
141
|
+
if (searchHeight <= 0)
|
142
|
+
searchHeight = imgHeight;
|
143
|
+
if (searchX + searchWidth >= imgWidth)
|
144
|
+
searchWidth = imgWidth - searchX;
|
145
|
+
if (searchY + searchHeight >= imgHeight)
|
146
|
+
searchHeight = imgHeight - searchY;
|
147
|
+
|
148
|
+
/* prepare image */
|
149
|
+
imgReady = convert_image(imgPixels, imgWidth, imgHeight);
|
150
|
+
|
151
|
+
for (i = 0; i < amountSubimages; i++)
|
152
|
+
{
|
153
|
+
/* cur++ inside RARRAY_PTR might not be used as it is used in the macro multiple times */
|
154
|
+
const VALUE *subPixels = RARRAY_PTR(argv[cur]);
|
155
|
+
int subWidth = FIX2INT(argv[cur + 1]);
|
156
|
+
int subHeight = FIX2INT(argv[cur + 2]);
|
157
|
+
const unsigned int *subReady = convert_image(subPixels, subWidth, subHeight);
|
158
|
+
|
159
|
+
VALUE r = search_single_subimage(imgReady, imgWidth, imgHeight,
|
160
|
+
searchX, searchY, searchWidth, searchHeight,
|
161
|
+
subReady, subWidth, subHeight, singleMode);
|
162
|
+
|
163
|
+
xfree((void *)subReady);
|
164
|
+
|
165
|
+
rb_ary_push(result, r);
|
166
|
+
|
167
|
+
if ((singleMode & 1) && RARRAY_LEN(r) > 0)
|
168
|
+
break;
|
169
|
+
|
170
|
+
cur += 3;
|
171
|
+
}
|
172
|
+
|
173
|
+
xfree((void *)imgReady);
|
174
|
+
|
175
|
+
return result;
|
176
|
+
}
|
177
|
+
|
178
|
+
void Init_chunky_png_subimage()
|
179
|
+
{
|
180
|
+
VALUE mainModule = rb_define_module("ChunkyPNGSubimage");
|
181
|
+
|
182
|
+
rb_define_singleton_method(mainModule, "_search_subimage", search_subimages, -1);
|
183
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'chunky_png'
|
2
|
+
require 'chunky_png_subimage/chunky_png_subimage'
|
3
|
+
|
4
|
+
module ChunkyPNGSubimage
|
5
|
+
#
|
6
|
+
def self.search_subimage img, subimages, single = :single_same_subimage, x = 0, y = 0, width = 0, height = 0
|
7
|
+
many_subimages = subimages.is_a? Array
|
8
|
+
subimages = [subimages] unless many_subimages
|
9
|
+
case single
|
10
|
+
when :single_everywhere
|
11
|
+
single = 3
|
12
|
+
when :single_same_subimage # just 1 result is needed per subimage
|
13
|
+
single = 2
|
14
|
+
when :single_subimage # single image to find
|
15
|
+
single = 1
|
16
|
+
else
|
17
|
+
single = 0
|
18
|
+
end
|
19
|
+
params = [img.pixels, img.width, img.height, x, y, width, height, single, subimages.length]
|
20
|
+
subimages.each do |s|
|
21
|
+
params << s.pixels
|
22
|
+
params << s.width
|
23
|
+
params << s.height
|
24
|
+
end
|
25
|
+
res = self._search_subimage *params
|
26
|
+
res = res[0] if (!many_subimages && res.is_a?(Array))
|
27
|
+
res
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chunky_png_subimage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Mingulov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: chunky_png
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake-compiler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Support to search subimages for ChunkyPNG - fast with ruby extension
|
84
|
+
email:
|
85
|
+
- denis@mingulov.com
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/chunky_png_subimage/extconf.rb
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- chunky_png_subimage.gemspec
|
98
|
+
- example/test.rb
|
99
|
+
- ext/chunky_png_subimage/chunky_png_subimage.c
|
100
|
+
- ext/chunky_png_subimage/chunky_png_subimage.h
|
101
|
+
- ext/chunky_png_subimage/extconf.rb
|
102
|
+
- lib/chunky_png_subimage.rb
|
103
|
+
- lib/chunky_png_subimage/search.rb
|
104
|
+
- lib/chunky_png_subimage/version.rb
|
105
|
+
homepage: http://github.com/mingulov/chunky_png_subimage
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.9'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Support to search subimages for ChunkyPNG
|
129
|
+
test_files: []
|