-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea_draw_image.cr
More file actions
72 lines (60 loc) · 1.75 KB
/
area_draw_image.cr
File metadata and controls
72 lines (60 loc) · 1.75 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
require "../../src/uing"
require "stumpy_png"
fname = File.join(__DIR__, "crys.png")
canvas = StumpyPNG.read(fname)
width = canvas.width.to_i32
height = canvas.height.to_i32
# Create pixel buffer for premultiplied RGBA
pixels = Bytes.new(width * height * 4)
(0...height).each do |y|
(0...width).each do |x|
offset = (y * width + x) * 4
r, g, b, a = canvas[x, y].to_rgba
# Handle alpha properly - default to 255 if nil
alpha = a || 255_u8
# For premultiplied alpha, multiply RGB by alpha/255
if alpha < 255
alpha_factor = alpha.to_f / 255.0
r = (r.to_f * alpha_factor).to_u8
g = (g.to_f * alpha_factor).to_u8
b = (b.to_f * alpha_factor).to_u8
end
pixels[offset] = r
pixels[offset + 1] = g
pixels[offset + 2] = b
pixels[offset + 3] = alpha
end
end
UIng.init
image = UIng::Image.new(width, height)
image.append(pixels, width, height, width * 4)
window = UIng::Window.new("Draw Image Example", 400, 400, margined: true)
window.on_closing do
UIng.quit
true
end
area_handler = UIng::Area::Handler.new do |header|
draw do |area, params|
ctx = params.context
white_brush = UIng::Area::Draw::Brush.new(:solid, 1.0, 1.0, 1.0, 1.0)
ctx.fill_path(white_brush) do |path|
path.add_rectangle(0, 0, params.area_width, params.area_height)
end
begin
ctx.draw_image(image, 10, 10, 100, 100)
ctx.draw_image(image, 160, 10, 200, 100)
ctx.draw_image(image, 10, 160, 100, 200)
ctx.draw_image(image, 160, 160, 200, 200)
rescue ex
UIng.handle_callback_error(ex, "draw_image")
end
end
end
area = UIng::Area.new(area_handler)
box = UIng::Box.new(:horizontal)
box.append(area, stretchy: true)
window.set_child(box)
window.show
UIng.main
image.free
UIng.uninit