Class: Ocarina::CharacterGenerator

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ocarina/character_generator.rb

Overview

creates bitmap images for characters using RMagick’s drawing canvas

Direct Known Subclasses

LetterpressCharacterGenerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#char_to_binary_string, #filename_for_noise_image, #filename_for_quantized_image, #filename_for_training_image, #int_to_binary_string, #is_lower?, #is_upper?, #noise_image_for_char, #pixel_number_to_col, #pixel_number_to_row, #pixel_to_bit, #quantize_image, #reference_image_for_char, #sigma

Constructor Details

#initialize(config) ⇒ CharacterGenerator

Returns a new instance of CharacterGenerator.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ocarina/character_generator.rb', line 12

def initialize(config)
  @config = config

  # generate reference images
  #
  @reference_image_hash = { }

  @config.char_set.each_char do |char|
    @reference_image_hash[char] = generate_reference_gif_for_char char
  end

  # generate noise images
  #
  @noise_image_hash = { }

  @config.char_set.each_char do |char|
    @noise_image_hash[char] = generate_noise_gif_for_char char
  end
end

Instance Attribute Details

#noise_image_hashObject

Returns the value of attribute noise_image_hash.



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

def noise_image_hash
  @noise_image_hash
end

#reference_image_hashObject

Returns the value of attribute reference_image_hash.



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

def reference_image_hash
  @reference_image_hash
end

Instance Method Details

#draw_image_for_char(char) ⇒ Object

draws the image, but does not save it



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ocarina/character_generator.rb', line 48

def draw_image_for_char(char)
  canvas       = Magick::Image.new(@config.char_width, @config.char_height)
  gc           = Magick::Draw.new
  gc.pointsize = 20.0
  #gc.font_family = "Helvetica"
  #gc.font_weight = Magick::BoldWeight

  gc.stroke('transparent')
  gc.fill('black')
  gc.gravity = Magick::CenterGravity
  gc.annotate(canvas, 0, 0, 0, 0, char)

  gc.draw(canvas)

  canvas
end

#generate_noise_gif_for_char(char) ⇒ Object

generate the gif image for the given character, but with added noise



74
75
76
77
78
79
80
81
82
83
# File 'lib/ocarina/character_generator.rb', line 74

def generate_noise_gif_for_char(char)
  image = @reference_image_hash[char]

  #image = image.wave(10, 100)
  image = image.add_noise(Magick::PoissonNoise)
  #image = image.rotate(5)
  #image = image.resize_to_fit(@config.char_width, @config.char_height)

  image
end

#generate_reference_gif_for_char(char) ⇒ Object

generate the gif image for the given character



68
69
70
# File 'lib/ocarina/character_generator.rb', line 68

def generate_reference_gif_for_char(char)
  draw_image_for_char char
end

#persist_tilesObject

save the reference and noise images to disk



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ocarina/character_generator.rb', line 34

def persist_tiles
  reference_image_hash.each_pair do |char, image|
    puts "saving to: #{filename_for_training_image(char, 'gif')}"

    image.write(filename_for_training_image(char, 'gif'))
  end

  noise_image_hash.each_pair do |char, image|
    image.write(filename_for_noise_image(char, 'gif'))
  end
end