compix 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb42dd67dc46a435223d649a19c74494e9f6a920
4
+ data.tar.gz: 4608581180744a8fc6df686c71fdcc99df1fa673
5
+ SHA512:
6
+ metadata.gz: 5861d769b5ee93c95c6811a931dd782e7f8def8117f04162b7c9150a7255e10648e2197bf843de3bc98e31466b9d20e4b3c157dee16ee42250474ef16822ac4f
7
+ data.tar.gz: a5328685d480d67477f3624b00e8a2c09648bdadf1c02f5ee0c835c2869ca0fbf5f7686e5733ae085acf6582884bcb23ca44e37bb9222629de1c677ba7a9752f
@@ -0,0 +1,146 @@
1
+ #include <ruby.h>
2
+ #include <stdbool.h>
3
+
4
+ // Types and macros from OilyPNG for compatibility
5
+ typedef unsigned int PIXEL; // Pixels use 32 bits unsigned integers
6
+ typedef unsigned char BYTE; // Bytes use 8 bits unsigned integers
7
+ #define R_BYTE(pixel) ((BYTE) (((pixel) & (PIXEL) 0xff000000) >> 24))
8
+ #define G_BYTE(pixel) ((BYTE) (((pixel) & (PIXEL) 0x00ff0000) >> 16))
9
+ #define B_BYTE(pixel) ((BYTE) (((pixel) & (PIXEL) 0x0000ff00) >> 8))
10
+ #define A_BYTE(pixel) ((BYTE) (((pixel) & (PIXEL) 0x000000ff)))
11
+
12
+ void Init_compix();
13
+ VALUE compix_compare_pixels(VALUE self, VALUE subimage, VALUE bigimage,
14
+ VALUE minCoord, VALUE maxCoord, VALUE threshold);
15
+
16
+ void Init_compix(){
17
+ VALUE klass = rb_define_class("Compix", rb_cObject);
18
+ rb_define_singleton_method(klass, "compare_pixels", compix_compare_pixels, 5);
19
+ }
20
+
21
+ int getImgWidth(VALUE img){
22
+ int width = NUM2INT(rb_funcall(img, rb_intern("width"), 0));
23
+ return width;
24
+ }
25
+
26
+ int getImgHeight(VALUE img){
27
+ int height = NUM2INT(rb_funcall(img, rb_intern("height"), 0));
28
+ return height;
29
+ }
30
+
31
+ int comparePixels(PIXEL pone, PIXEL ptwo, int threshold){
32
+ unsigned char ored = R_BYTE(pone);
33
+ unsigned char ogreen = G_BYTE(pone);
34
+ unsigned char oblue = B_BYTE(pone);
35
+ unsigned char tred = R_BYTE(ptwo);
36
+ unsigned char tgreen = G_BYTE(ptwo);
37
+ unsigned char tblue = B_BYTE(ptwo);
38
+ if(ored - tred > threshold || tred - ored > threshold ||
39
+ ogreen - tgreen > threshold || tgreen - ogreen > threshold ||
40
+ oblue - tblue > threshold || tblue - oblue > threshold){
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+
46
+ int compareAt(unsigned int *pixelsSmall, unsigned int *pixelsBig, int thres, int bigXoffset, int bigYoffset,
47
+ int siwidth, int siheight, int sswidth, int ssheight){
48
+ PIXEL firstPixel;
49
+ PIXEL secPixel;
50
+ int match = 0;
51
+ int unmatch = 0;
52
+ int x, y;
53
+ int small_image_coord;
54
+ int bigCoord = 0;
55
+ for(y = 0; y < siheight; y++){
56
+ for(x = 0; x < siwidth; x++){
57
+ small_image_coord = y * siwidth + x;
58
+ firstPixel = pixelsSmall[small_image_coord];
59
+ bigCoord = sswidth*(y+bigYoffset)+(x+bigXoffset);
60
+ if(bigCoord >= (sswidth)*(ssheight) - 2 ){
61
+ break;
62
+ }
63
+ secPixel = pixelsBig[bigCoord];
64
+ if(comparePixels(firstPixel, secPixel, thres)){
65
+ match++;
66
+ }
67
+ else {
68
+ unmatch++;
69
+ }
70
+ }
71
+ if(unmatch > match){
72
+ break;
73
+ }
74
+ }
75
+ return match;
76
+ }
77
+
78
+ VALUE compix_compare_pixels(VALUE self, VALUE subimage, VALUE bigimage,
79
+ VALUE minCoord, VALUE maxCoord, VALUE threshold){
80
+ VALUE pixelsf = rb_funcall(subimage, rb_intern("pixels"), 0);
81
+ VALUE pixelss = rb_funcall(bigimage, rb_intern("pixels"), 0);
82
+
83
+ int siwidth = getImgWidth(subimage);
84
+ int siheight = getImgHeight(subimage);
85
+ int sswidth = getImgWidth(bigimage);
86
+ int ssheight = getImgHeight(bigimage);
87
+
88
+ unsigned int *bytearray_subimg = malloc(siwidth * siheight * sizeof(unsigned int));
89
+ unsigned int *bytearray_screenshot = malloc(sswidth * ssheight * sizeof(unsigned int));
90
+
91
+ int thres = NUM2UINT(threshold);
92
+ int match = 0;
93
+ int maxmatch = 0;
94
+ int maxX = -1;
95
+ int maxY = -1;
96
+ int xLeftLimit = NUM2INT(rb_ary_entry(minCoord, 0));
97
+ int yTopLimit = NUM2INT(rb_ary_entry(minCoord, 1));
98
+ int xRightLimit = NUM2INT(rb_ary_entry(maxCoord, 0));
99
+ int yBottomLimit = NUM2INT(rb_ary_entry(maxCoord, 1));
100
+ // I didn't want to break backwards compatibility, so while this is less legible
101
+ // and slower if old version of compix is used, it will still work.
102
+ // The third and fourth entry in the array specifies from where to start checking the big image.
103
+ // Used for e.g. findAllSubImages in rubypng
104
+ int xstart = 0; //NUM2INT(rb_ary_entry(minCoord, 2));
105
+ int ystart = 0 ; //NUM2INT(rb_ary_entry(minCoord, 3));
106
+ int x, y;
107
+ int pixels = 0;
108
+ int compPix = siwidth * siheight;
109
+ VALUE retArr;
110
+ int matchPercent;
111
+
112
+ // Create bytearrays for pixels
113
+ for(y = 0 ; y < siheight ; y++){
114
+ for(x = 0 ; x < siwidth ; x++){
115
+ bytearray_subimg[y*siwidth + x] = NUM2UINT(rb_ary_entry(pixelsf,y*siwidth + x));
116
+ }
117
+ }
118
+ for(y = 0 ; y < ssheight ; y++){
119
+ for(x = 0 ; x < sswidth ; x++){
120
+ bytearray_screenshot[y*sswidth + x] = NUM2UINT(rb_ary_entry(pixelss,y*sswidth + x));
121
+ }
122
+ }
123
+
124
+
125
+ for(y = ystart ; y < yBottomLimit-siheight; y++){
126
+ for(x = xstart ; x < xRightLimit-siwidth; x++){
127
+ match = compareAt(bytearray_subimg, bytearray_screenshot, thres, x, y,
128
+ siwidth, siheight, sswidth, ssheight);
129
+ if(match > maxmatch){
130
+ maxmatch = match;
131
+ maxX = x;
132
+ maxY = y;
133
+ }
134
+ }
135
+ }
136
+ free(bytearray_subimg);
137
+ free(bytearray_screenshot);
138
+ pixels = x * ( ssheight - siheight ) + y;
139
+ matchPercent = (100 * maxmatch) / compPix;
140
+ retArr = rb_ary_new();
141
+ rb_ary_push(retArr, INT2NUM(matchPercent));
142
+ rb_ary_push(retArr, INT2NUM(maxX));
143
+ rb_ary_push(retArr, INT2NUM(maxY));
144
+ rb_ary_push(retArr, INT2NUM(pixels));
145
+ return retArr;
146
+ }
@@ -0,0 +1,6 @@
1
+ # external/comparepixels/extconf.rb
2
+
3
+ require 'mkmf'
4
+ extension_name = 'compix'
5
+ dir_config(extension_name)
6
+ create_makefile(extension_name)
@@ -0,0 +1,7 @@
1
+ require 'compix.so'
2
+
3
+ #class Compix
4
+ # module CompareImagePixels
5
+ #
6
+ # end
7
+ #end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Jon Appelberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
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
+ description:
28
+ email:
29
+ executables: []
30
+ extensions:
31
+ - ext/compix/extconf.rb
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ext/compix/compix.c
35
+ - ext/compix/extconf.rb
36
+ - lib/compix.rb
37
+ homepage:
38
+ licenses: []
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.4.2
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Compix for oilypng
60
+ test_files: []
61
+ has_rdoc: