-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.rb
73 lines (58 loc) · 1.91 KB
/
Image.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# encoding: ascii-8bit
class Image
attr_reader :width, :height
#-----------------------------------------------
# Initialize
#-----------------------------------------------
def initialize(width, height)
@width = width
@height = height
@pixels = "\0" * (width * height << 2)
end
#-----------------------------------------------
# Initialize copy
#-----------------------------------------------
def initialize_copy(other)
super
@pixels = @pixels.dup
end
#-----------------------------------------------
# Get pixel
#-----------------------------------------------
def get_pixel(x, y)
index = x + y * @width << 2
[@pixels.getbyte(index + 2), @pixels.getbyte(index + 1), @pixels.getbyte(index), @pixels.getbyte(index + 3)]
end
#-----------------------------------------------
# Set pixel
#-----------------------------------------------
def set_pixel(x, y, r, g, b, a)
index = x + y * @width << 2
@pixels.setbyte(index, b)
@pixels.setbyte(index + 1, g)
@pixels.setbyte(index + 2, r)
@pixels.setbyte(index + 3, a)
self
end
#-----------------------------------------------
# Match pixel
#-----------------------------------------------
def match_pixel?(x, y, r, g, b, a)
index = x + y * @width << 2
@pixels.getbyte(index) == b and @pixels.getbyte(index + 1) == g and @pixels.getbyte(index + 2) == r and @pixels.getbyte(index + 3) == a
end
#-----------------------------------------------
# Read
#-----------------------------------------------
def read(pixel_index, data_index, data, n_elem)
data[data_index, n_elem] = @pixels.byteslice(pixel_index, n_elem)
self
end
#-----------------------------------------------
# Write
#-----------------------------------------------
def write(pixel_index, data_index, data, n_elem)
@pixels[pixel_index, n_elem] = data.byteslice(data_index, n_elem)
self
end
end