Class: RubyMarks::ImageUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_marks/image_utils.rb

Class Method Summary collapse

Class Method Details

.calc_height(y1, y2) ⇒ Object



11
12
13
# File 'lib/ruby_marks/image_utils.rb', line 11

def self.calc_height(y1, y2)
  y2.to_i - y1.to_i + 1
end

.calc_middle_horizontal(x, width) ⇒ Object



16
17
18
# File 'lib/ruby_marks/image_utils.rb', line 16

def self.calc_middle_horizontal(x, width)
  x.to_i + width.to_i / 2
end

.calc_middle_vertical(y, height) ⇒ Object



21
22
23
# File 'lib/ruby_marks/image_utils.rb', line 21

def self.calc_middle_vertical(y, height)
  y.to_i + height.to_i / 2
end

.calc_width(x1, x2) ⇒ Object



6
7
8
# File 'lib/ruby_marks/image_utils.rb', line 6

def self.calc_width(x1, x2)
  x2.to_i - x1.to_i + 1
end

.export_file_to_str(file) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/ruby_marks/image_utils.rb', line 129

def self.export_file_to_str(file)
  image = file.export_pixels_to_str
  image = image.gsub!(Regexp.new('\xFF\xFF\xFF', nil, 'n'), " ,") if image
  image = image.gsub!(Regexp.new('\x00\x00\x00', nil, 'n'), ".,") if image
  image = image.split(',') if image
  image = image.each_slice(file.page.width).to_a  if image 
end

.flood_scan(image, x, y, character = " ", coordinates = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_marks/image_utils.rb', line 36

def self.flood_scan(image, x, y, character=" ", coordinates={})
  coordinates = {x1: 0, y1: 0, x2: image[0].size, y2: image.size} unless coordinates.any?
  result_mask =  Hash.new { |hash, key| hash[key] = [] }
  result_mask.tap do |result_mask|
    process_queue =  Hash.new { |hash, key| hash[key] = [] }
    process_line = true
    
    loop do

      break if y > coordinates[:y2] - 1 || y < coordinates[:y1]
      reset_process = false

      if process_line
        current_x = x
        loop do
          position = image[y][current_x]
          
          break if position != character || current_x - 1 <= coordinates[:x1]         
          process_queue[y] << current_x unless process_queue[y].include?(current_x) || result_mask[y].include?(current_x) 
          result_mask[y] << current_x unless result_mask[y].include?(current_x)            
          current_x = current_x - 1
        end

        current_x = x.to_i
        loop do
          position = image[y][current_x]

          break if position != character || current_x + 1 >= coordinates[:x2]       
          process_queue[y] << current_x unless process_queue[y].include?(current_x) || result_mask[y].include?(current_x)              
          result_mask[y] << current_x unless result_mask[y].include?(current_x)
          current_x = current_x + 1
        end

        result_mask[y] = result_mask[y].sort
        process_queue[y] = process_queue[y].sort
      end

      process_line = true

      process_queue[y].each do |element|
        if y - 1 >= coordinates[:y1]
          position = image[y-1][element]

          if position == character && !result_mask[y-1].include?(element)
            x = element
            y = y - 1
            reset_process = true
            break
          end
        end
      end

      next if reset_process

      process_queue[y].each do |element|         
        if y + 1 <= coordinates[:y2]
          position = image[y+1] && image[y+1][element]

          if position && position == character && !result_mask[y+1].include?(element)
            x = element
            y = y + 1
            reset_process = true
            break
          else
            process_queue[y].delete(element)
          end
                
        end
      end

      next if reset_process

      process_queue.each do |k,v|
        process_queue.delete(k) if v.empty?
      end

      break if process_queue.empty?

      process_line = false
      y = process_queue.first[0] if process_queue.first.is_a?(Array)
    end
  end
end

.image_center(coordinates) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ruby_marks/image_utils.rb', line 26

def self.image_center(coordinates)
  width  = self.calc_width(coordinates[:x1], coordinates[:x2])
  height = self.calc_height(coordinates[:y1], coordinates[:y2])
  
  x = self.calc_middle_horizontal(coordinates[:x1], width)
  y = self.calc_middle_vertical(coordinates[:y1], height)
  return {x: x, y: y}
end

.to_hex(red, green, blue) ⇒ Object



121
122
123
124
125
126
# File 'lib/ruby_marks/image_utils.rb', line 121

def self.to_hex(red, green, blue)
  red = get_hex_from_color(red)
  green = get_hex_from_color(green)
  blue = get_hex_from_color(blue)
  "##{red}#{green}#{blue}".upcase
end