Module: Ocarina::Util

Included in:
CharacterGenerator, Config, LetterpressCropper, Network
Defined in:
lib/ocarina/util.rb

Instance Method Summary collapse

Instance Method Details

#char_to_binary_string(target_char) ⇒ Object



14
15
16
# File 'lib/ocarina/util.rb', line 14

def char_to_binary_string(target_char)
  int_to_binary_string target_char.ord
end

#filename_for_noise_image(c, suffix) ⇒ Object

returns filepath for the “noise” image for a given character



62
63
64
65
66
67
68
69
# File 'lib/ocarina/util.rb', line 62

def filename_for_noise_image(c, suffix)

  # the default OSX file system is case preserving, meaning that you cannot have
  # both "A.bitmap" and "a.bitmap" at the same time. So we map lowercase letters to
  # a special name.
  #
  is_lower?(c) ? "#{IMAGES_DIR}/noise/#{c}_lower.#{suffix}" : "#{IMAGES_DIR}/noise/#{c}.#{suffix}"
end

#filename_for_quantized_image(c, suffix) ⇒ Object



71
72
73
74
# File 'lib/ocarina/util.rb', line 71

def filename_for_quantized_image(c, suffix)

  is_lower?(c) ? "#{IMAGES_DIR}/quantized/#{c}_lower.#{suffix}" : "#{IMAGES_DIR}/quantized/#{c}.#{suffix}"
end

#filename_for_training_image(c, suffix) ⇒ Object

returns filepath for the image for a given character



51
52
53
54
55
56
57
58
# File 'lib/ocarina/util.rb', line 51

def filename_for_training_image(c, suffix)

  # the default OSX file system is case preserving, meaning that you cannot have
  # both "A.bitmap" and "a.bitmap" at the same time. So we map lowercase letters to
  # a special name.
  #
  is_lower?(c) ? "#{IMAGES_DIR}/reference/#{c}_lower.#{suffix}" : "#{IMAGES_DIR}/reference/#{c}.#{suffix}"
end

#int_to_binary_string(i) ⇒ Object



10
11
12
# File 'lib/ocarina/util.rb', line 10

def int_to_binary_string(i)
  i.to_s(2).rjust(config.num_outputs, '0')
end

#is_lower?(c) ⇒ Boolean

kind of sad that this is not built into Ruby

Returns:

  • (Boolean)


77
78
79
# File 'lib/ocarina/util.rb', line 77

def is_lower?(c)
  !! /[[:lower:]]/.match(c)
end

#is_upper?(c) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ocarina/util.rb', line 81

def is_upper?(c)
  !! /[[:upper:]]/.match(c)
end

#noise_image_for_char(char) ⇒ Object

returns a Magick::Image, constructed from the noise gif file for the given character



45
46
47
# File 'lib/ocarina/util.rb', line 45

def noise_image_for_char(char)
  Magick::Image.read(filename_for_noise_image char, "gif").first
end

#pixel_number_to_col(n, image) ⇒ Object



24
25
26
# File 'lib/ocarina/util.rb', line 24

def pixel_number_to_col(n, image)
  n % image.columns
end

#pixel_number_to_row(n, image) ⇒ Object



28
29
30
# File 'lib/ocarina/util.rb', line 28

def pixel_number_to_row(n, image)
  n / image.columns
end

#pixel_to_bit(pixel) ⇒ Object



32
33
34
# File 'lib/ocarina/util.rb', line 32

def pixel_to_bit(pixel)
  pixel.red | pixel.green | pixel.black > 0 ? 1 : 0
end

#quantize_image(image) ⇒ Object



85
86
87
# File 'lib/ocarina/util.rb', line 85

def quantize_image(image)
  image.white_threshold(Magick::MaxRGB * 0.35).quantize(2, Magick::GRAYColorspace)
end

#reference_image_for_char(char) ⇒ Object

returns a Magick::Image, constructed from the reference gif file for the given character



39
40
41
# File 'lib/ocarina/util.rb', line 39

def reference_image_for_char(char)
  Magick::Image.read(filename_for_training_image char, "gif").first
end

#sigma(x) ⇒ Object

function that maps its input to a range between 0..1 mathematically it’s supposed to be asymptotic, but large values of x will round up to 1



20
21
22
# File 'lib/ocarina/util.rb', line 20

def sigma(x)
  1.0/(1.0+Math.exp(-x))
end