Class: Contrek::Bitmaps::PngBitmap

Inherits:
Bitmap
  • Object
show all
Defined in:
lib/contrek/bitmaps/png_bitmap.rb

Direct Known Subclasses

CustomBitmap

Instance Method Summary collapse

Methods inherited from Bitmap

#clear!, #copy_rect, #scan

Methods included from Painting

#bitmap_colors, direct_draw_polygons

Constructor Details

#initialize(file_path) ⇒ PngBitmap

Returns a new instance of PngBitmap.



6
7
8
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 6

def initialize(file_path)
  @image = ChunkyPNG::Image.from_file(file_path)
end

Instance Method Details

#clearObject



61
62
63
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 61

def clear
  @image = ChunkyPNG::Image.new(w, h, ChunkyPNG::Color.rgba(255, 255, 255, 255))
end

#draw_line(start_x, start_y, end_x, end_y, value) ⇒ Object



18
19
20
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 18

def draw_line(start_x, start_y, end_x, end_y, value)
  @image.line_xiaolin_wu(start_x, start_y, end_x, end_y, value)
end

#hObject



14
15
16
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 14

def h
  @image.dimension.height
end

#hsv_at(x, y) ⇒ Object



30
31
32
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 30

def hsv_at(x, y)
  @image.get_pixel(x, y).to_hsv
end

#inspectObject



57
58
59
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 57

def inspect
  "PngBitMap"
end

#resize_h(new_h) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 49

def resize_h(new_h)
  old_w = w
  old_h = h

  new_w = (old_w * new_h) / old_h
  @image = @image.resize(new_w, new_h)
end

#rgb_value_at(x, y) ⇒ Object



26
27
28
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 26

def rgb_value_at(x, y)
  value_at(x, y)
end

#save(filename) ⇒ Object



38
39
40
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 38

def save(filename)
  @image.save(filename, interlace: true, compression: Zlib::NO_COMPRESSION)
end

#to_tmp_fileObject



42
43
44
45
46
47
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 42

def to_tmp_file
  tmp_png_file = Tempfile.new
  tmp_png_file.binmode
  tmp_png_file.write(@image.to_blob)
  tmp_png_file
end

#value_at(x, y) ⇒ Object



22
23
24
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 22

def value_at(x, y)
  @image[x, y]
end

#value_set(x, y, value) ⇒ Object



34
35
36
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 34

def value_set(x, y, value)
  @image[x, y] = value
end

#wObject



10
11
12
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 10

def w
  @image.dimension.width
end