chunky_png_subimage 0.1.2 → 0.2.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 +4 -4
- data/Rakefile +9 -3
- data/ext/chunky_png_subimage/chunky_png_subimage.c +64 -1
- data/ext/chunky_png_subimage/chunky_png_subimage.h +27 -1
- data/ext/chunky_png_subimage/extconf.rb +0 -2
- data/lib/chunky_png_subimage/search.rb +51 -1
- data/lib/chunky_png_subimage/version.rb +1 -1
- data/lib/chunky_png_subimage.rb +0 -1
- data/spec/spec.rb +39 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6426dbb98ddd9eb1ec1f48050cbab98e45c44ea8
|
4
|
+
data.tar.gz: 6c0a714e56825f2f6d58a73be354e42944a66348
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 111903fb0d4a9336f2c09bac57cdfe2714c7618f6a7fa8c0fba40f6af2aafb91a660dbe568e27b9a18353fdb79fb1575d50431a329e2ff96efe760dc7cb5c414
|
7
|
+
data.tar.gz: a674d5e4901cc83e5d871b2fd162bca7792a3458407352c1b2d5ca2c5016e644baf37c69c92455d676f1ff237c84b31405a04e65002d5fdbe6e234522d00a259
|
data/Rakefile
CHANGED
@@ -6,7 +6,13 @@ Rake::ExtensionTask.new('chunky_png_subimage') do |ext|
|
|
6
6
|
#ext.config_options = '--with-cflags="-std=c99"'
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
#RSpec::Core::RakeTask.new(:spec)
|
9
|
+
require 'rspec/core/rake_task'
|
11
10
|
|
12
|
-
|
11
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
12
|
+
task.pattern = "./spec/spec.rb"
|
13
|
+
task.rspec_opts = ['--color']
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::Task['spec'].prerequisites << :compile
|
17
|
+
|
18
|
+
task :default => [:spec]
|
@@ -1,10 +1,54 @@
|
|
1
|
+
/** Ruby C extension 'chunky_png_subimage', source
|
2
|
+
*
|
3
|
+
* The MIT License (MIT)
|
4
|
+
*
|
5
|
+
* Copyright (c) 2014 Denis Mingulov
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
12
|
+
* furnished to do so, subject to the following conditions:
|
13
|
+
*
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
15
|
+
* copies or substantial portions of the Software.
|
16
|
+
*
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
* SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
1
26
|
#include "chunky_png_subimage.h"
|
2
27
|
|
28
|
+
/** Check that the color from subimage is usable to search
|
29
|
+
* @param color Color to be checked
|
30
|
+
* @return 0 whether color is not usable, non-0 otherwise
|
31
|
+
*/
|
3
32
|
static inline int is_color_usable(unsigned int color)
|
4
33
|
{
|
34
|
+
/* currently - fully opaque points are used only */
|
5
35
|
return (color & 0xff) == 0xff;
|
6
36
|
}
|
7
37
|
|
38
|
+
/** Search a single subimage inside image
|
39
|
+
* @param img Pointer to uint colors for image (must be imgWidth * imgHeight)
|
40
|
+
* @param imgWidth Image width
|
41
|
+
* @param imgHeight Image height
|
42
|
+
* @param searchX Offset X of 1st possible point for subimage
|
43
|
+
* @param searchY Offset Y of 1st possible point for subimage
|
44
|
+
* @param searchWidth Width for the search region
|
45
|
+
* @param searchHeight Height for the search region
|
46
|
+
* @param sub Pointer to uint colors for subimage (must be subWidth * subHeight)
|
47
|
+
* @param subWidth Subimage width
|
48
|
+
* @param subHeight Subimage height
|
49
|
+
* @param singleMode 0 whether search every image, non-0 - to stop after 1st found
|
50
|
+
* @return Ruby array with [x, y] entries - found positions of subimage
|
51
|
+
*/
|
8
52
|
static VALUE search_single_subimage(const unsigned int *img, const int imgWidth, const int imgHeight,
|
9
53
|
const int searchX, const int searchY, const int searchWidth, const int searchHeight,
|
10
54
|
const unsigned int *sub, const int subWidth, const int subHeight, int singleMode)
|
@@ -76,7 +120,12 @@ static VALUE search_single_subimage(const unsigned int *img, const int imgWidth,
|
|
76
120
|
return result;
|
77
121
|
}
|
78
122
|
|
79
|
-
|
123
|
+
/** convert image with Ruby values to unsigned int*, xfree must be used later
|
124
|
+
* @param imgPixels Pointer to 1st element of Ruby array with data points (should be imgWidth * imgHeight)
|
125
|
+
* @param imgWidth Width of the image
|
126
|
+
* @param imgHeight Height of the image
|
127
|
+
* @return Pointer to unsigned int values, xfree must be used later
|
128
|
+
*/
|
80
129
|
static unsigned int *convert_image(const VALUE *imgPixels, const int imgWidth, const int imgHeight)
|
81
130
|
{
|
82
131
|
unsigned int *imgReady = xmalloc(sizeof(unsigned int) * imgWidth * imgHeight);
|
@@ -89,6 +138,18 @@ static unsigned int *convert_image(const VALUE *imgPixels, const int imgWidth, c
|
|
89
138
|
return imgReady;
|
90
139
|
}
|
91
140
|
|
141
|
+
/** Exportable function to ruby, to search subimages in the image
|
142
|
+
* @param argc Amount of elements in argv
|
143
|
+
* @param argv Parameters:
|
144
|
+
* img array of pixels, img width, img height,
|
145
|
+
* search offset x, search offset y, search width, search height,
|
146
|
+
* singleMode (bit 1 - for subimage search, bit 0 - to stop after 1st found subimage),
|
147
|
+
* amount of subimages,
|
148
|
+
* for every subimage:
|
149
|
+
* subimage array of pixels, subimage width, subimage height
|
150
|
+
* @param self Object
|
151
|
+
* @return Array of found subimages' positions
|
152
|
+
*/
|
92
153
|
VALUE search_subimages(int argc, VALUE *argv, VALUE self)
|
93
154
|
{
|
94
155
|
/*
|
@@ -164,6 +225,7 @@ VALUE search_subimages(int argc, VALUE *argv, VALUE self)
|
|
164
225
|
|
165
226
|
rb_ary_push(result, r);
|
166
227
|
|
228
|
+
/* stop if the proper single mode and result is found */
|
167
229
|
if ((singleMode & 1) && RARRAY_LEN(r) > 0)
|
168
230
|
break;
|
169
231
|
|
@@ -175,6 +237,7 @@ VALUE search_subimages(int argc, VALUE *argv, VALUE self)
|
|
175
237
|
return result;
|
176
238
|
}
|
177
239
|
|
240
|
+
/** Entry point for an initialization */
|
178
241
|
void Init_chunky_png_subimage()
|
179
242
|
{
|
180
243
|
VALUE mainModule = rb_define_module("ChunkyPNGSubimage");
|
@@ -1,8 +1,34 @@
|
|
1
|
+
/** Ruby C extension 'chunky_png_subimage', header
|
2
|
+
*
|
3
|
+
* The MIT License (MIT)
|
4
|
+
*
|
5
|
+
* Copyright (c) 2014 Denis Mingulov
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
12
|
+
* furnished to do so, subject to the following conditions:
|
13
|
+
*
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
15
|
+
* copies or substantial portions of the Software.
|
16
|
+
*
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
* SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
1
26
|
#ifndef CHUNKY_PNG_SUBIMAGE_H
|
2
27
|
#define CHUNKY_PNG_SUBIMAGE_H
|
3
28
|
|
4
29
|
#include <ruby.h>
|
5
30
|
|
31
|
+
/* Entry point for an initialization */
|
6
32
|
void Init_chunky_png_subimage();
|
7
33
|
|
8
|
-
#endif
|
34
|
+
#endif
|
@@ -1,7 +1,57 @@
|
|
1
|
-
|
1
|
+
# = ChunkyPNGSubimage
|
2
|
+
#
|
3
|
+
# Search subimages inside image - fast as ruby C extension is used.
|
4
|
+
#
|
5
|
+
# == Synopsys
|
6
|
+
#
|
7
|
+
# require 'chunky_png'
|
8
|
+
# require 'chunky_png_subimage'
|
9
|
+
#
|
10
|
+
# img = ChunkyPNG::Image.new(10, 10, ChunkyPNG::Color::WHITE)
|
11
|
+
# sub1 = ChunkyPNG::Image.new(5, 5, ChunkyPNG::Color::BLACK)
|
12
|
+
# sub2 = ChunkyPNG::Image.new(9, 9, ChunkyPNG::Color::WHITE)
|
13
|
+
#
|
14
|
+
# puts ChunkyPNGSubimage.search_subimage img, sub1
|
15
|
+
# puts ChunkyPNGSubimage.search_subimage img, [sub1, sub2]
|
16
|
+
#
|
17
|
+
# == Introduction
|
18
|
+
#
|
19
|
+
# Quite often it is required to find the position of some image on the bigger image.
|
20
|
+
# For example use case - a test with watir-webdriver for the external Flash page.
|
21
|
+
# Or just some automation for the some other application with some other screenshot maker.
|
22
|
+
# This library allows fastly to find subimages on the big image, so you might react
|
23
|
+
# almost instantly to the change.
|
24
|
+
# Of couse it can be used for any other reasons.
|
25
|
+
|
2
26
|
require 'chunky_png_subimage/chunky_png_subimage'
|
3
27
|
|
4
28
|
module ChunkyPNGSubimage
|
29
|
+
|
30
|
+
# Search subimages on the image.
|
31
|
+
#
|
32
|
+
# ==== Attributes
|
33
|
+
#
|
34
|
+
# * +img+ - Image where search for subimages will be done
|
35
|
+
# * +subimages+ - Subimage or an array of subimages. Just fully opaque pixels are used from any subimage.
|
36
|
+
# * +single+ - 'single mode', when to stop if an subimage is found, possible values:
|
37
|
+
# :single_everywhere - fully stop after 1st subimage is found;
|
38
|
+
# :single_same_subimage - search maximum 1 subimage position, for every provided subimage;
|
39
|
+
# :single_subimage - stop after 1st subimage is found, but find every possible position for that subimage;
|
40
|
+
# anything else - search for every possible subimage multiple times.
|
41
|
+
# * +x+ - if find subimages inside some region of image - offset x (0 by default)
|
42
|
+
# * +y+ - if find subimages inside some region of image - offset y (0 by default)
|
43
|
+
# * +width+ - if find subimages inside some region of image - width of that region (full image by default)
|
44
|
+
# * +height+ - if find subimages inside some region of image - height of that region (full image by default)
|
45
|
+
#
|
46
|
+
# ==== Output
|
47
|
+
#
|
48
|
+
# Method returns:
|
49
|
+
# * An array of arrays with positions of found subimages.
|
50
|
+
# * If just single subimage is provided (without array) - then the output also does not contain outer array.
|
51
|
+
#
|
52
|
+
# ==== Examples
|
53
|
+
#
|
54
|
+
# ChunkyPNGSubimage.search_subimage img, sub
|
5
55
|
#
|
6
56
|
def self.search_subimage img, subimages, single = :single_same_subimage, x = 0, y = 0, width = 0, height = 0
|
7
57
|
many_subimages = subimages.is_a? Array
|
data/lib/chunky_png_subimage.rb
CHANGED
data/spec/spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'chunky_png_subimage'
|
2
|
+
require 'chunky_png'
|
3
|
+
|
4
|
+
describe ChunkyPNGSubimage do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@white_big = ChunkyPNG::Image.new(10, 10, ChunkyPNG::Color::WHITE)
|
8
|
+
@white_medium = ChunkyPNG::Image.new(5, 5, ChunkyPNG::Color::WHITE)
|
9
|
+
@black_big = ChunkyPNG::Image.new(10, 10, ChunkyPNG::Color::BLACK)
|
10
|
+
@black_medium = ChunkyPNG::Image.new(5, 5, ChunkyPNG::Color::BLACK)
|
11
|
+
@black_big_tr = ChunkyPNG::Image.new(5, 5, ChunkyPNG::Color::BLACK)
|
12
|
+
@black_big_tr[1, 1] = ChunkyPNG::Color::WHITE
|
13
|
+
@black_big_tr[3, 1] = ChunkyPNG::Color::WHITE
|
14
|
+
@black_big_tr[1, 3] = ChunkyPNG::Color::WHITE
|
15
|
+
@black_big_tr[3, 3] = ChunkyPNG::Color::WHITE
|
16
|
+
@black_small_tr = ChunkyPNG::Image.new(3, 3, ChunkyPNG::Color::BLACK)
|
17
|
+
@black_small_tr[1, 1] = 0
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#search_subimage' do
|
21
|
+
#search_subimage img, subimages, single = :single_same_subimage, x = 0, y = 0, width = 0, height = 0
|
22
|
+
|
23
|
+
it "white on black should not find anything" do
|
24
|
+
ChunkyPNGSubimage.search_subimage(@black_big, @white_medium).should == []
|
25
|
+
ChunkyPNGSubimage.search_subimage(@black_big, @white_medium, :single_same_subimage).should == []
|
26
|
+
ChunkyPNGSubimage.search_subimage(@black_big, @white_medium, :single_everywhere).should == []
|
27
|
+
ChunkyPNGSubimage.search_subimage(@black_big, @white_medium, :single_subimage).should == []
|
28
|
+
ChunkyPNGSubimage.search_subimage(@black_big, @white_medium, :everything).should == []
|
29
|
+
end
|
30
|
+
|
31
|
+
it "default mode is :single_same_subimage" do
|
32
|
+
ChunkyPNGSubimage.search_subimage(@black_big, @black_medium, :single_same_subimage).should == ChunkyPNGSubimage.search_subimage(@black_big, @black_medium)
|
33
|
+
ChunkyPNGSubimage.search_subimage(@white_big, @black_medium, :single_same_subimage).should == ChunkyPNGSubimage.search_subimage(@white_big, @black_medium)
|
34
|
+
ChunkyPNGSubimage.search_subimage(@white_big, @white_medium, :single_same_subimage).should == ChunkyPNGSubimage.search_subimage(@white_big, @white_medium)
|
35
|
+
ChunkyPNGSubimage.search_subimage(@black_big, [@black_medium, @black_small_tr], :single_same_subimage).should == ChunkyPNGSubimage.search_subimage(@black_big, [@black_medium, @black_small_tr])
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chunky_png_subimage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Mingulov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chunky_png
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- lib/chunky_png_subimage.rb
|
103
103
|
- lib/chunky_png_subimage/search.rb
|
104
104
|
- lib/chunky_png_subimage/version.rb
|
105
|
+
- spec/spec.rb
|
105
106
|
homepage: http://github.com/mingulov/chunky_png_subimage
|
106
107
|
licenses:
|
107
108
|
- MIT
|
@@ -126,4 +127,5 @@ rubygems_version: 2.2.2
|
|
126
127
|
signing_key:
|
127
128
|
specification_version: 4
|
128
129
|
summary: Support to search subimages for ChunkyPNG
|
129
|
-
test_files:
|
130
|
+
test_files:
|
131
|
+
- spec/spec.rb
|