-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.rb
More file actions
301 lines (266 loc) · 7.54 KB
/
image.rb
File metadata and controls
301 lines (266 loc) · 7.54 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
module Processing
# Image object.
#
# @see https://processing.org/reference/PImage.html
# @see https://p5js.org/reference/p5/p5.Image/
#
class Image
include Xot::Inspectable
# @private
def initialize(image)
@image = image
@pixels, @error = nil, false
end
# Gets width of image.
#
# @return [Numeric] width of image
#
# @see https://processing.org/reference/PImage_width.html
# @see https://p5js.org/reference/p5.Image/width/
#
def width()
@image&.width || (@error ? -1 : 0)
end
# Gets height of image.
#
# @return [Numeric] height of image
#
# @see https://processing.org/reference/PImage_height.html
# @see https://p5js.org/reference/p5.Image/height/
#
def height()
@image&.height || (@error ? -1 : 0)
end
alias w width
alias h height
# Returns the width and height of image.
#
# @return [Array<Numeric>] [width, height]
#
def size()
[width, height]
end
# Sets the color of the pixel.
#
# @param x [Integer] x position of the pixel
# @param y [Integer] y position of the pixel
# @param c [Integer] color value
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_set_.html
# @see https://p5js.org/reference/p5.Image/set/
#
def set(x, y, c)
getInternal__.bitmap(true)[x, y] = self.class.fromColor__(c).map {|n| n / 255.0}
nil
end
# Returns the color of the pixel.
#
# @return [Integer] color value (0xAARRGGBB)
#
# @see https://processing.org/reference/PImage_get_.html
# @see https://p5js.org/reference/p5.Image/get/
#
def get(x, y)
getInternal__.bitmap[x, y]
.map {|n| (n * 255).to_i.clamp 0, 255}
.then {|r, g, b, a| self.class.toColor__ r, g, b, a}
end
# Loads all pixels to the 'pixels' array.
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_loadPixels_.html
# @see https://p5js.org/reference/p5.Image/loadPixels/
#
def loadPixels()
@pixels = getInternal__.pixels
end
# Update the image pixels with the 'pixels' array.
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_updatePixels_.html
# @see https://p5js.org/reference/p5.Image/updatePixels/
#
def updatePixels()
return unless @pixels
getInternal__.pixels = @pixels
@pixels = nil
end
# An array of all pixels.
# Call loadPixels() before accessing the array.
#
# @return [Array] color array
#
# @see https://processing.org/reference/PImage_pixels.html
# @see https://p5js.org/reference/p5.Image/pixels/
#
attr_reader :pixels
# Applies an image filter.
#
# overload filter(shader)
# overload filter(type)
# overload filter(type, param)
#
# @param shader [Shader] a fragment shader to apply
# @param type [THRESHOLD, GRAY, INVERT, BLUR] filter type
# @param param [Numeric] a parameter for each filter
#
# @see https://processing.org/reference/PImage_filter_.html
# @see https://p5js.org/reference/p5.Image/filter/
#
def filter(*args)
@filter = Shader.createFilter__(*args)
end
# Resizes image.
#
# @param width [Numeric] width for resized image
# @param height [Numeric] height for resized image
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_resize_.html
# @see https://p5js.org/reference/p5.Image/resize/
#
def resize(width, height)
return nil if width == @image.width && height == @image.height
@image = Rays::Image.new(width, height).paint do |painter|
painter.image getInternal__, 0, 0, width, height
end
nil
end
# Copies image.
#
# @overload copy(sx, sy, sw, sh, dx, dy, dw, dh)
# @overload copy(img, sx, sy, sw, sh, dx, dy, dw, dh)
#
# @param img [Image] image for copy source
# @param sx [Numrtic] x position of source region
# @param sy [Numrtic] y position of source region
# @param sw [Numrtic] width of source region
# @param sh [Numrtic] height of source region
# @param dx [Numrtic] x position of destination region
# @param dy [Numrtic] y position of destination region
# @param dw [Numrtic] width of destination region
# @param dh [Numrtic] height of destination region
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_copy_.html
# @see https://p5js.org/reference/p5.Image/copy/
#
def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
blend img, sx, sy, sw, sh, dx, dy, dw, dh, :normal
end
# @private
def mask__()
raise NotImplementedError
end
# Blends image.
#
# @overload blend(sx, sy, sw, sh, dx, dy, dw, dh, mode)
# @overload blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode)
#
# @param img [Image] image for blend source
# @param sx [Numeric] x position of source region
# @param sy [Numeric] y position of source region
# @param sw [Numeric] width of source region
# @param sh [Numeric] height of source region
# @param dx [Numeric] x position of destination region
# @param dy [Numeric] y position of destination region
# @param dw [Numeric] width of destination region
# @param dh [Numeric] height of destination region
# @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE] blend mode
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_blend_.html
# @see https://p5js.org/reference/p5.Image/blend/
#
def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
img ||= self
getInternal__.paint do |painter|
img.drawImage__ painter, sx, sy, sw, sh, dx, dy, dw, dh, blend_mode: mode
end
nil
end
# @private
def blendColor__()
raise NotImplementedError
end
# @private
def reset__()
raise NotImplementedError
end
# @private
def getCurrentFrame__()
raise NotImplementedError
end
# @private
def setFrame__()
raise NotImplementedError
end
# @private
def numFrames__()
raise NotImplementedError
end
# @private
def play__()
raise NotImplementedError
end
# @private
def pause__()
raise NotImplementedError
end
# @private
def delay__()
raise NotImplementedError
end
# Saves image to file.
#
# @param filename [String] file name to save image
#
# @return [nil] nil
#
# @see https://processing.org/reference/PImage_save_.html
# @see https://p5js.org/reference/p5.Image/save/
#
def save(filename)
getInternal__.save filename
nil
end
# @private
def getInternal__()
@image or raise 'Invalid image object'
end
# @private
def setInternal__(image, error = false)
@image, @error = image, error
end
# @private
def drawImage__(painter, *args, **states)
shader = painter.shader || @filter&.getInternal__
painter.push shader: shader, **states do |_|
painter.image getInternal__, *args
end
end
# @private
def self.fromColor__(color)
[
color >> 16 & 0xff,
color >> 8 & 0xff,
color & 0xff,
color >> 24 & 0xff
]
end
# @private
def self.toColor__(r, g, b, a)
(r & 0xff) << 16 |
(g & 0xff) << 8 |
(b & 0xff) |
(a & 0xff) << 24
end
end# Image
end# Processing