mork 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 023acadd6339d125a7764a4af516d86b7da951c3
4
- data.tar.gz: fffbcdd3a134b60d02f46a380ba5192812c13b76
3
+ metadata.gz: 8f86f0bbe05ae9f4eb62d0d61598a76fcd595e08
4
+ data.tar.gz: 547f41d90cc045f797ca98254638723c650fd2f1
5
5
  SHA512:
6
- metadata.gz: 8bb3ec4c6ec605ce6d7866550ab7d66025520d99961a388ae303b513a2a5f57252818725ae8d8d64f891eab54f55d0f7c3d61cae3c16cea873612f53344ac31a
7
- data.tar.gz: ad6e6df8e2ce80126e270760bc2909a2810555d1f68196e919f635feec29d55974444b98bda28e93dfdd510d938566289e4882ce4f96cb04460e0b385f2f3f59
6
+ metadata.gz: cdfe684595e84ccc61c03f07e30c0a171fa438be0b9a7e46d0cf563e77f17b15772f0b80f8d7e7a69eb3079fab46d971eef3337f6e97661785a515efc864dc45
7
+ data.tar.gz: 37b009ebc52a62517ee8c03a55e3163747f49632f721fa585d8561bb4a304eefda61dc208430b5b99e298d40905111cc580305f57051abc11ecc19608beee4af
data/lib/mork/grid_omr.rb CHANGED
@@ -42,6 +42,11 @@ module Mork
42
42
  end
43
43
  end
44
44
 
45
+ def cell_corner_size
46
+ d = choice_cell_area(0,0)
47
+ (d[:w]-d[:h]).abs
48
+ end
49
+
45
50
  def barcode_bit_area(bit)
46
51
  {
47
52
  x: (cx * barcode_bit_x(bit)).round,
data/lib/mork/mimage.rb CHANGED
@@ -9,6 +9,8 @@ module Mork
9
9
  @path = path
10
10
  @grom = grom
11
11
  @grom.set_page_size width, height
12
+ @rm = {} # registration mark centers
13
+ @rmsa = {} # registration mark search area
12
14
  @status = register
13
15
  @cmd = []
14
16
  end
@@ -54,7 +56,7 @@ module Mork
54
56
  @cmd << [:stroke, 'green']
55
57
  @cmd << [:strokewidth, '4']
56
58
  @cmd << [:fill, 'none']
57
- array_of(cells).each do |c|
59
+ coordinates_of(cells).each do |c|
58
60
  roundedness ||= [c[:h], c[:w]].min / 2
59
61
  pts = [c[:x], c[:y], c[:x]+c[:w], c[:y]+c[:h], roundedness, roundedness].join ' '
60
62
  @cmd << [:draw, "roundrectangle #{pts}"]
@@ -73,7 +75,7 @@ module Mork
73
75
  return if cells.empty?
74
76
  @cmd << [:stroke, 'none']
75
77
  @cmd << [:fill, 'rgba(255, 255, 0, 0.3)']
76
- array_of(cells).each do |c|
78
+ coordinates_of(cells).each do |c|
77
79
  roundedness ||= [c[:h], c[:w]].min / 2
78
80
  pts = [c[:x], c[:y], c[:x]+c[:w], c[:y]+c[:h], roundedness, roundedness].join ' '
79
81
  @cmd << [:draw, "roundrectangle #{pts}"]
@@ -101,32 +103,51 @@ module Mork
101
103
  end
102
104
  end
103
105
 
104
- def cross_cells!(cells)
106
+ def cross(cells)
107
+ return if cells.empty?
105
108
  cells = [cells] if cells.is_a? Hash
106
- cells.each do |c|
107
- out = Magick::Draw.new
108
- out.stroke 'yellow'
109
- out.stroke_width 3
110
- out.line
109
+ @cmd << [:stroke, 'red']
110
+ @cmd << [:strokewidth, '3']
111
+ coordinates_of(cells).each do |c|
112
+ pts = [
113
+ c[:x]+corner,
114
+ c[:y]+corner,
115
+ c[:x]+c[:w]-corner,
116
+ c[:y]+c[:h]-corner
117
+ ].join ' '
118
+ @cmd << [:draw, "line #{pts}"]
119
+ pts = [
120
+ c[:x]+corner,
121
+ c[:y]+c[:h]-corner,
122
+ c[:x]+c[:w]-corner,
123
+ c[:y]+corner
124
+ ].join ' '
125
+ @cmd << [:draw, "line #{pts}"]
111
126
  end
112
127
  end
113
128
 
114
129
  # write the underlying MiniMagick::Image to disk;
130
+ # if no file name is given, image is processed in-place;
115
131
  # if the 2nd arg is false, then stretching is not applied
116
- def write(fname, reg=true)
117
- img = MiniMagick::Image.open @path
118
- img.combine_options do |c|
119
- c.distort(:perspective, perspective_points) if reg
120
- @cmd.each do |cmd|
121
- c.send *cmd
122
- end
132
+ def write(fname=nil, reg=true)
133
+ if fname
134
+ img = MiniMagick::Image.open @path
135
+ img.combine_options {|c| exec_mm_cmd c, reg }
136
+ img.write fname
137
+ else
138
+ MiniMagick::Image.new(@path) { |c| exec_mm_cmd c, reg }
123
139
  end
124
- img.write fname
125
140
  end
126
141
 
127
142
  # ============================================================#
128
143
  private #
129
144
  # ============================================================#
145
+ def exec_mm_cmd(c, reg)
146
+ c.distort(:perspective, perspective_points) if reg
147
+ @cmd.each do |cmd|
148
+ c.send *cmd
149
+ end
150
+ end
130
151
 
131
152
  def img_size
132
153
  @img_size ||= IO.read("|identify -format '%w,%h' #{@path}").split ','
@@ -163,20 +184,18 @@ module Mork
163
184
  @cmd << [:draw, "polygon #{pts}"]
164
185
  end
165
186
 
166
- def array_of(cells)
167
- out = []
168
- cells.each_with_index do |q, i|
169
- q.each do |c|
170
- out << @grom.choice_cell_area(i, c)
171
- end
172
- end
173
- out
187
+ def coordinates_of(cells)
188
+ cells.collect.each_with_index do |q, i|
189
+ q.collect { |c| @grom.choice_cell_area(i, c) }
190
+ end.flatten
191
+ end
192
+
193
+ def corner
194
+ @corner_size ||= @grom.cell_corner_size
174
195
  end
175
196
 
176
197
  def register
177
198
  # find the XY coordinates of the 4 registration marks
178
- @rm = {} # registration mark centers
179
- @rmsa = {} # registration mark search area
180
199
  @rm[:tl] = reg_centroid_on(:tl)
181
200
  @rm[:tr] = reg_centroid_on(:tr)
182
201
  @rm[:br] = reg_centroid_on(:br)
@@ -192,7 +211,7 @@ module Mork
192
211
  @rmsa[corner] = @grom.rm_search_area(corner, i)
193
212
  cx, cy = raw_pixels.dark_centroid @rmsa[corner]
194
213
  if cx.nil?
195
- status = :insufficient_contrast
214
+ status = :no_contrast
196
215
  elsif (cx < @grom.rm_edgy_x) or
197
216
  (cy < @grom.rm_edgy_y) or
198
217
  (cy > @rmsa[corner][:h] - @grom.rm_edgy_y) or
@@ -77,6 +77,17 @@ module Mork
77
77
  @mim.outline cells
78
78
  end
79
79
 
80
+ def cross(cells)
81
+ return if not_registered
82
+ raise "Invalid ‘cells’ argument" unless cells.kind_of? Array
83
+ @mim.cross cells
84
+ end
85
+
86
+ def cross_marked
87
+ return if not_registered
88
+ @mim.cross mark_array
89
+ end
90
+
80
91
  def highlight_marked
81
92
  return if not_registered
82
93
  @mim.highlight_cells mark_array
@@ -96,12 +107,24 @@ module Mork
96
107
  @mim.highlight_reg_area
97
108
  end
98
109
 
99
- def write(fname)
110
+ # write(output_path_file_name)
111
+ #
112
+ # writes out a copy of the source image after registration;
113
+ # the output image will also contain any previously applied overlays;
114
+ # if the argument is omitted, the image is created in-place,
115
+ # i.e. the original source image is overwritten.
116
+ def write(fname=nil)
100
117
  return if not_registered
101
118
  @mim.write(fname)
102
119
  end
103
120
 
104
- def write_raw(fname)
121
+ # write_raw(output_path_file_name)
122
+ #
123
+ # writes out a copy of the source image before registration;
124
+ # the output image will also contain any previously applied overlays
125
+ # if the argument is omitted, the image is created in-place,
126
+ # i.e. the original source image is overwritten.
127
+ def write_raw(fname=nil)
105
128
  @mim.write(fname, false)
106
129
  end
107
130
 
data/lib/mork/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mork
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/mork.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
 
20
20
  # dependencies:
21
21
  s.add_dependency 'narray', '~> 0.6'
22
- s.add_dependency 'mini_magick', '~> 3.8'
22
+ s.add_dependency 'mini_magick', '~> 4.0'
23
23
  s.add_dependency 'prawn', '~> 1.3'
24
24
  s.add_development_dependency 'rake', '~> 10.3'
25
25
  s.add_development_dependency 'rspec', '~> 3.1'
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'fileutils'
2
3
 
3
4
  module Mork
4
5
  describe SheetOMR do
@@ -24,12 +25,29 @@ module Mork
24
25
  sheet.write 'spec/out/marked_highlights.jpg'
25
26
  end
26
27
 
28
+ it 'crossed marked cells' do
29
+ sheet.cross_marked
30
+ sheet.write 'spec/out/marked_crosses.jpg'
31
+ end
32
+
27
33
  it 'outlines some responses' do
28
34
  sheet.outline [[1],[1],[2],[2],[3,4],[],[0,1,2,3,4], [],[1],[2],[2],[3,4],[],[0,1,2,3,4]]
29
35
  sheet.write 'spec/out/outlines.jpg'
30
36
  end
31
37
 
32
- it 'highlights marked cells and outline correct responses', focus: true do
38
+ it 'cross some responses' do
39
+ sheet.cross [[1],[1],[2],[2],[3,4],[],[0,1,2,3,4], [],[1],[2],[2],[3,4],[],[0,1,2,3,4]]
40
+ sheet.write 'spec/out/crosses.jpg'
41
+ end
42
+
43
+ it 'outlines some responses in-place (rewriting the source image)' do
44
+ FileUtils.cp shinfo.filename, 'spec/out/inplace.jpg'
45
+ tsheet = SheetOMR.new 'spec/out/inplace.jpg', shinfo.grid_file
46
+ tsheet.outline [[1],[1],[2],[2],[3,4],[],[0,1,2,3,4], [],[1],[2],[2],[3,4],[],[0,1,2,3,4]]
47
+ tsheet.write
48
+ end
49
+
50
+ it 'highlights marked cells and outline correct responses' do
33
51
  sheet.highlight_marked
34
52
  sheet.outline [[1],[1],[2],[2],[3,4],[],[0,1,2,3,4], [],[1],[2],[2],[3,4],[],[0,1,2,3,4]]
35
53
  sheet.write 'spec/out/marks_and_outs.jpg'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giuseppe Bertini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-07 00:00:00.000000000 Z
11
+ date: 2014-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: narray
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.8'
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.8'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: prawn
43
43
  requirement: !ruby/object:Gem::Requirement