Skip to content

Commit 5ab2915

Browse files
committed
Implements ImageKernelConvolution
1 parent 1f39850 commit 5ab2915

9 files changed

Lines changed: 139 additions & 38 deletions

File tree

raylib/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*Raylib* _MAJOR 5 _MINOR 0 _PATCH 0 5.0
1+
*Raylib* _MAJOR 5 _MINOR 1 _PATCH 0 5.1-dev
22
=======
33
raylib is a simple and easy-to-use library to enjoy videogames programming.
44

@@ -353,6 +353,7 @@ Implemented APIs (604)
353353
| sub ImageFlipVertical(image) | Flip image vertically |
354354
| sub ImageFormat(image, newFormat) | Convert image data to desired format |
355355
| func ImageFromImage(image, rec) | Create an image from another image piece |
356+
| sub ImageKernelConvolution(image, kernel, kernelSize) | Apply Custom Square image convolution kernel |
356357
| sub ImageMipmaps(image) | Compute all mipmap levels for a provided image |
357358
| sub ImageResize(image, newWidth, newHeight) | Resize image (Bicubic scaling algorithm) |
358359
| sub ImageResizeCanvas(image, newWidth, newHeight, offsetX, offsetY, fill) | Resize canvas and fill with color |
@@ -575,7 +576,6 @@ Implemented APIs (604)
575576
| sub UnloadAutomationEventList(list) | Unload automation events list from file |
576577
| sub UnloadCodepoints(codepoints) | Unload codepoints data from memory |
577578
| sub UnloadDirectoryFiles(files) | Unload filepaths |
578-
| sub UnloadDroppedFiles(files) | Unload dropped filepaths |
579579
| sub UnloadFileData(data) | Unload file data allocated by LoadFileData() |
580580
| sub UnloadFileText(text) | Unload file text data allocated by LoadFileText() |
581581
| sub UnloadFont(font) | Unload font from GPU memory (VRAM) |

raylib/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,28 @@ static int cmd_updateautomationeventlist(int argc, slib_par_t *params, var_t *re
876876
return result;
877877
}
878878

879+
//
880+
// Apply Custom Square image convolution kernel
881+
//
882+
static int cmd_imagekernelconvolution(int argc, slib_par_t *params, var_t *retval) {
883+
int result;
884+
int image_id = get_image_id(argc, params, 0, retval);
885+
if (image_id != -1 && is_param_array(argc, params, 1)) {
886+
var_p_t array = params[1].var_p;
887+
int kernelSize = v_asize(array);
888+
float *kernel = new float[kernelSize];
889+
for (int i = 0; i < kernelSize; i++) {
890+
kernel[i] = get_num(v_elem(array, i));
891+
}
892+
ImageKernelConvolution(&_imageMap.at(image_id), kernel, kernelSize);
893+
delete [] kernel;
894+
result = 1;
895+
} else {
896+
result = 0;
897+
}
898+
return result;
899+
}
900+
879901
#include "proc.h"
880902
#include "func.h"
881903

@@ -1793,6 +1815,7 @@ static FUNC_SIG lib_func[] = {
17931815

17941816
static FUNC_SIG lib_proc[] = {
17951817
#include "proc-def.h"
1818+
{2, 2, "IMAGEKERNELCONVOLUTION", cmd_imagekernelconvolution},
17961819
{4, 5, "SETSHADERVALUE", cmd_setshadervalue},
17971820
{2, 2, "SETMODELDIFFUSETEXTURE", cmd_setmodeldiffusetexture},
17981821
{4, 4, "SETMODELTRANSFORM", cmd_setmodeltransform},

raylib/mkraylib.bas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ func is_internal(name)
293293
name == "LoadRenderTexture" || &
294294
name == "LoadShader" || &
295295
name == "SetShaderValue" || &
296+
name == "ImageKernelConvolution" || &
296297
name == "TextFormat" || &
297298
name == "TraceLog" || &
299+
name == "UnloadDroppedFiles" || &
298300
name == "UpdateCameraPro" || &
299301
name == "UpdateCamera" || &
300302
name == "UpdateTexture")

raylib/proc-def.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@
229229
{1, 1, "UNLOADAUTOMATIONEVENTLIST", cmd_unloadautomationeventlist},
230230
{0, 0, "UNLOADCODEPOINTS", cmd_unloadcodepoints},
231231
{1, 1, "UNLOADDIRECTORYFILES", cmd_unloaddirectoryfiles},
232-
{1, 1, "UNLOADDROPPEDFILES", cmd_unloaddroppedfiles},
233232
{1, 1, "UNLOADFILEDATA", cmd_unloadfiledata},
234233
{1, 1, "UNLOADFILETEXT", cmd_unloadfiletext},
235234
{1, 1, "UNLOADFONT", cmd_unloadfont},

raylib/proc.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,15 +3023,6 @@ static int cmd_unloaddirectoryfiles(int argc, slib_par_t *params, var_t *retval)
30233023
return 1;
30243024
}
30253025

3026-
//
3027-
// Unload dropped filepaths
3028-
//
3029-
static int cmd_unloaddroppedfiles(int argc, slib_par_t *params, var_t *retval) {
3030-
auto files = get_param_filepathlist(argc, params, 0);
3031-
UnloadDroppedFiles(files);
3032-
return 1;
3033-
}
3034-
30353026
//
30363027
// Unload file data allocated by LoadFileData()
30373028
//

raylib/samples/core_automation_events.bas

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,29 @@ while (!rl.WindowShouldClose())
141141

142142
if (rl.IsKeyPressed(c.KEY_R)) then
143143
REM Reset game state
144-
player.position.x = 400
145-
player.position.y = 280
146-
player.speed = 0
147-
player.canJump = false
148-
camera.target = player.position
149-
camera.offset.x = screenWidth/2.0
150-
camera.offset.y = screenHeight/2.0
151-
camera.rotation = 0.0
152-
camera.zoom = 1.0
144+
reset_player()
145+
reset_camera()
153146
endif
154147

155148
REM Update camera
156149
camera.target = player.position
157150
camera.offset.x = screenWidth/2.0
158151
camera.offset.y = screenHeight/2.0
159-
minX = 1000
160-
minY = 1000
161-
maxX = -1000
162-
maxY = -1000
163-
164-
for i = 0 to MAX_ENVIRONMENT_ELEMENTS - 1
165-
minX = min(element._rect.x, minX)
166-
maxX = max(element._rect.x + element._rect.width, maxX)
167-
minY = min(element._rect.y, minY)
168-
maxY = max(element._rect.y + element._rect.height, maxY)
169-
next
170-
171-
_max = rl.GetWorldToScreen2D([maxX, maxY], camera)
172-
_min = rl.GetWorldToScreen2D([minX, minY], camera)
173-
152+
' minX = 1000
153+
' minY = 1000
154+
' maxX = -1000
155+
' maxY = -1000
156+
'
157+
' for i = 0 to MAX_ENVIRONMENT_ELEMENTS - 1
158+
' minX = min(element._rect.x, minX)
159+
' maxX = max(element._rect.x + element._rect.width, maxX)
160+
' minY = min(element._rect.y, minY)
161+
' maxY = max(element._rect.y + element._rect.height, maxY)
162+
' next
163+
'
164+
' _max = rl.GetWorldToScreen2D([maxX, maxY], camera)
165+
' _min = rl.GetWorldToScreen2D([minX, minY], camera)
166+
'
174167
' if (_max.x < screenWidth) then camera.offset.x = screenWidth - (_max.x - screenWidth/2)
175168
' if (_max.y < screenHeight) then camera.offset.y = screenHeight - (_max.y - screenHeight/2)
176169
' if (_min.x > 0) then camera.offset.x = screenWidth/2 - _min.x
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
REM /*******************************************************************************************
2+
REM *
3+
REM * raylib [textures] example - Image loading and texture creation
4+
REM *
5+
REM * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
6+
REM *
7+
REM * Example originally created with raylib 1.3, last time updated with raylib 1.3
8+
REM *
9+
REM * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
10+
REM * BSD-like license that allows static linking with closed source software
11+
REM *
12+
REM * Copyright (c) 2015-2023 Karim Salem (@kimo-s)
13+
REM *
14+
REM ********************************************************************************************/
15+
16+
import raylib as rl
17+
import raylibc as c
18+
19+
sub normalizeKernel(byref kernel, size)
20+
local _sum = 0
21+
local i
22+
for i = 0 to size - 1
23+
_sum += kernel[i]
24+
next
25+
26+
if (_sum != 0) then
27+
for i = 0 to size - 1
28+
kernel[i] /= _sum
29+
next
30+
endif
31+
end
32+
33+
const resources = CWD + "raylib/examples/textures/resources/"
34+
const _image = rl.LoadImage(resources + "cat.png")
35+
const screenWidth = 800
36+
const screenHeight = 450
37+
38+
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution")
39+
40+
gaussiankernel = [1.0, 2.0, 1.0, 2.0, 4.0, 2.0, 1.0, 2.0, 1.0]
41+
sobelkernel = [1.0, 0.0, -1.0, 2.0, 0.0, -2.0,1.0, 0.0, -1.0]
42+
sharpenkernel = [0.0, -1.0, 0.0, -1.0, 5.0, -1.0, 0.0, -1.0, 0.0]
43+
44+
normalizeKernel(gaussiankernel, 9)
45+
normalizeKernel(sharpenkernel, 9)
46+
normalizeKernel(sobelkernel, 9)
47+
48+
catSharpend = rl.ImageCopy(_image)
49+
rl.ImageKernelConvolution(catSharpend, sharpenkernel)
50+
51+
catSobel = rl.ImageCopy(_image)
52+
rl.ImageKernelConvolution(catSobel, sobelkernel)
53+
54+
catGaussian = rl.ImageCopy(_image)
55+
for i = 0 to 6
56+
rl.ImageKernelConvolution(catGaussian, gaussiankernel)
57+
next
58+
59+
rl.ImageCrop(_image, [0, 0, 200, 450])
60+
rl.ImageCrop(catGaussian, [0, 0, 200, 450])
61+
rl.ImageCrop(catSobel, [0, 0, 200, 450])
62+
rl.ImageCrop(catSharpend, [0, 0, 200, 450])
63+
texture = rl.LoadTextureFromImage(_image) ' Image converted to texture, GPU memory (VRAM)
64+
catSharpendTexture = rl.LoadTextureFromImage(catSharpend)
65+
catSobelTexture = rl.LoadTextureFromImage(catSobel)
66+
catGaussianTexture = rl.LoadTextureFromImage(catGaussian)
67+
68+
rl.UnloadImage(_image) ' Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
69+
rl.UnloadImage(catGaussian)
70+
rl.UnloadImage(catSobel)
71+
rl.UnloadImage(catSharpend)
72+
73+
rl.SetTargetFPS(60)
74+
while (!rl.WindowShouldClose())
75+
rl.BeginDrawing()
76+
rl.ClearBackground(c.RAYWHITE)
77+
rl.DrawTexture(catSharpendTexture, 0, 0, c.WHITE)
78+
rl.DrawText("Sharpend", 0, 0, 20, c.RED)
79+
rl.DrawTexture(catSobelTexture, 200, 0, c.WHITE)
80+
rl.DrawText("Sobel", 200, 0, 20, c.RED)
81+
rl.DrawTexture(catGaussianTexture, 400, 0, c.WHITE)
82+
rl.DrawText("Gaussian", 400, 0, 20, c.RED)
83+
rl.DrawTexture(texture, 600, 0, c.WHITE)
84+
rl.DrawText("Original", 600, 0, 20, c.RED)
85+
rl.EndDrawing()
86+
wend
87+
88+
rl.UnloadTexture(texture)
89+
rl.UnloadTexture(catGaussianTexture)
90+
rl.UnloadTexture(catSobelTexture)
91+
rl.UnloadTexture(catSharpendTexture)
92+
rl.CloseWindow()
93+

0 commit comments

Comments
 (0)