Class: Contrek::Bitmaps::PngBitmap

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

Direct Known Subclasses

RawBitmap

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rendering

included

Methods inherited from Bitmap

#clear!, #copy_rect, #scan

Methods included from Painting

#bitmap_colors, direct_draw_polygons, draw_polygon

Constructor Details

#initialize(file_path) ⇒ PngBitmap

Returns a new instance of PngBitmap.



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

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

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



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

def image
  @image
end

Instance Method Details

#clearObject



66
67
68
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 66

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



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

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



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

def h
  @image.dimension.height
end

#hsv_at(x, y) ⇒ Object



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

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

#inspectObject



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

def inspect
  "PngBitMap"
end

#resize_h(new_h) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 54

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



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

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

#save(filename) ⇒ Object



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

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

#to_tmp_fileObject



47
48
49
50
51
52
# File 'lib/contrek/bitmaps/png_bitmap.rb', line 47

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



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

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

#value_set(x, y, value) ⇒ Object



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

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

#wObject



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

def w
  @image.dimension.width
end