-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtextures_image_kernel.bas
More file actions
93 lines (78 loc) · 3.02 KB
/
textures_image_kernel.bas
File metadata and controls
93 lines (78 loc) · 3.02 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
REM /*******************************************************************************************
REM *
REM * raylib [textures] example - Image loading and texture creation
REM *
REM * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
REM *
REM * Example originally created with raylib 1.3, last time updated with raylib 1.3
REM *
REM * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
REM * BSD-like license that allows static linking with closed source software
REM *
REM * Copyright (c) 2015-2023 Karim Salem (@kimo-s)
REM *
REM ********************************************************************************************/
import raylib as rl
import raylibc as c
sub normalizeKernel(byref kernel, size)
local _sum = 0
local i
for i = 0 to size - 1
_sum += kernel[i]
next
if (_sum != 0) then
for i = 0 to size - 1
kernel[i] /= _sum
next
endif
end
const resources = CWD + "raylib/examples/textures/resources/"
const _image = rl.LoadImage(resources + "cat.png")
const screenWidth = 800
const screenHeight = 450
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution")
gaussiankernel = [1.0, 2.0, 1.0, 2.0, 4.0, 2.0, 1.0, 2.0, 1.0]
sobelkernel = [1.0, 0.0, -1.0, 2.0, 0.0, -2.0,1.0, 0.0, -1.0]
sharpenkernel = [0.0, -1.0, 0.0, -1.0, 5.0, -1.0, 0.0, -1.0, 0.0]
normalizeKernel(gaussiankernel, 9)
normalizeKernel(sharpenkernel, 9)
normalizeKernel(sobelkernel, 9)
catSharpend = rl.ImageCopy(_image)
rl.ImageKernelConvolution(catSharpend, sharpenkernel)
catSobel = rl.ImageCopy(_image)
rl.ImageKernelConvolution(catSobel, sobelkernel)
catGaussian = rl.ImageCopy(_image)
for i = 0 to 6
rl.ImageKernelConvolution(catGaussian, gaussiankernel)
next
rl.ImageCrop(_image, [0, 0, 200, 450])
rl.ImageCrop(catGaussian, [0, 0, 200, 450])
rl.ImageCrop(catSobel, [0, 0, 200, 450])
rl.ImageCrop(catSharpend, [0, 0, 200, 450])
texture = rl.LoadTextureFromImage(_image) ' Image converted to texture, GPU memory (VRAM)
catSharpendTexture = rl.LoadTextureFromImage(catSharpend)
catSobelTexture = rl.LoadTextureFromImage(catSobel)
catGaussianTexture = rl.LoadTextureFromImage(catGaussian)
rl.UnloadImage(_image) ' Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
rl.UnloadImage(catGaussian)
rl.UnloadImage(catSobel)
rl.UnloadImage(catSharpend)
rl.SetTargetFPS(60)
while (!rl.WindowShouldClose())
rl.BeginDrawing()
rl.ClearBackground(c.RAYWHITE)
rl.DrawTexture(catSharpendTexture, 0, 0, c.WHITE)
rl.DrawText("Sharpend", 0, 0, 20, c.RED)
rl.DrawTexture(catSobelTexture, 200, 0, c.WHITE)
rl.DrawText("Sobel", 200, 0, 20, c.RED)
rl.DrawTexture(catGaussianTexture, 400, 0, c.WHITE)
rl.DrawText("Gaussian", 400, 0, 20, c.RED)
rl.DrawTexture(texture, 600, 0, c.WHITE)
rl.DrawText("Original", 600, 0, 20, c.RED)
rl.EndDrawing()
wend
rl.UnloadTexture(texture)
rl.UnloadTexture(catGaussianTexture)
rl.UnloadTexture(catSobelTexture)
rl.UnloadTexture(catSharpendTexture)
rl.CloseWindow()