|
| 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