class Mork::Mimage

@private

Attributes

choxq[R]
rm[R]
status[R]

Public Class Methods

new(path, grom) click to toggle source
# File lib/mork/mimage.rb, line 13
def initialize(path, grom)
  @mack  = Magicko.new path

  @grom  = grom.set_page_size @mack.width, @mack.height
  @choxq = [(0...@grom.max_choices_per_question).to_a] * grom.max_questions
  @rm    = {} # registration mark centers
  @valid = register
end

Public Instance Methods

barcode_bits() click to toggle source
# File lib/mork/mimage.rb, line 63
def barcode_bits
  @barcode_bits ||= begin
    @grom.barcode_bits.times.map do |b|
      reg_pixels.average(@grom.barcode_bit_area b+1) < barcode_threshold
    end
  end
end
choice_mean_darkness() click to toggle source
# File lib/mork/mimage.rb, line 45
def choice_mean_darkness
  @choice_mean_darkness ||= begin
    itemator(@choxq) { |q,c| reg_pixels.average @grom.choice_cell_area(q, c) }
  end
end
low_contrast?() click to toggle source
# File lib/mork/mimage.rb, line 26
def low_contrast?
  @rm.any? { |k,v| v < @grom.reg_min_contrast }
end
marked() click to toggle source
# File lib/mork/mimage.rb, line 51
def marked
  @marked_choices ||= begin
    choice_mean_darkness.map do |cho|
      [].tap do |choices|
        cho.map.with_index do |drk, c|
          choices << c if drk < choice_threshold
        end
      end
    end
  end
end
overlay(what, where) click to toggle source
# File lib/mork/mimage.rb, line 71
def overlay(what, where)
  areas = case where
          when :barcode
            @grom.barcode_areas barcode_bits
          when :cal
            @grom.calibration_cell_areas
          when :marked
            choice_cell_areas marked
          when :all
            choice_cell_areas @choxq
          when :max
            @grom.choice_cell_areas.flatten
          when Array
            choice_cell_areas where
          else
            fail ArgumentError, 'Invalid overlay argument “where”'
          end
  round = where != :barcode
  @mack.send what, areas, round
end
save(fname=nil, reg=true) click to toggle source

write the underlying MiniMagick::Image to disk; if no file name is given, image is processed in-place; if the 2nd arg is false, then stretching is not applied

# File lib/mork/mimage.rb, line 95
def save(fname=nil, reg=true)
  pp = reg ? @rm : nil
  @mack.save fname, pp
end
save_registration(fname) click to toggle source
# File lib/mork/mimage.rb, line 100
def save_registration(fname)
  each_corner { |c| @mack.plus @rm[c][:x], @rm[c][:y], 30 }
  each_corner { |c| @mack.outline [@grom.rm_crop_area(c)], false }
  @mack.save fname, nil
end
set_ch(choxq) click to toggle source
# File lib/mork/mimage.rb, line 39
def set_ch(choxq)
  @choxq =  choxq.map { |ncho| (0...ncho).to_a }
  # if set_ch is called more than once, discard memoization
  @marked_choices = @choice_mean_darkness = nil
end
valid?() click to toggle source
# File lib/mork/mimage.rb, line 22
def valid?
  @valid
end