Watch
2
0
Fork
You've already forked raylib-cs
0
raylib-cs/Examples/Textures/ImageProcessing.cs
tiger tiger tiger 8c22e68c2a
WASM examples (+ backports of new official examples) (#344)
* chg: New build system that uses the officially distributed binaries, bumped version to 8.0.0, simplified git workflow, removed deprecated OpenGL 1.1 functionality.

* chg: Modernize CI workflow, enable SourceLink

- Bump workflow actions to latest majors (Node 24); drop deprecated softprops/action-gh-release@v1
- Trigger push builds on main instead of master
- Create local nuget feed dir before pack (fixes NU1301)
- Enable Microsoft.SourceLink.GitHub for debugging symbols (ref PR #340)

* fix: centralized version data in Directory.build.props, and fixed various interop details that had incorrect function signatures

* chore: updated readme

* fix: version the native extract marker and chain download via DependsOnTargets

The .extracted marker now includes the raylib package name, so bumping
TargetRaylibTag re-extracts the new archive instead of silently keeping
(and packing/copying) the previous version's files.

_PrepareNativeLibrary and _StageWasmNative now depend directly on
_DownloadAndExtractInternal instead of CallTarget-ing it; dependency
targets run in the same project instance, so the resolved properties
(RaylibPackageName etc.) propagate naturally.

* fix: let the binding build for browser-wasm on both net8.0 and net10.0

The net8-era wasm workload (Microsoft.NET.Runtime.WebAssembly.Sdk 8.0.x,
auto-imported for RID browser-wasm) treats every browser-wasm project as
a wasm app: it forces OutputType=Exe after project evaluation (CS5001
for a classlib) and hooks its app-bundle build after Build, which errors
because a library has no assemblies to bundle. Opt Raylib-cs out via
DisableAutoWasmBuildApp (props time, before the workload defaults its
trigger) and pin OutputType back to Library in Directory.Build.targets
(evaluated after the workload props, so the assignment wins). net10's
wasm SDK needs neither workaround.

* chore: readme updated

* chg: simplifying build logic - a simple line in the documentation should save us the code here

* fix: Wrong signature of FrameBufferComplete

* chore: readme update

* feat: samples default to local project reference, and can optionally use the nuget package

* feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style

* chore: readme, gitignore, and targets backport.

* fix: Examples.csproj runs the download task when building locally

* feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style

* chore: readme, gitignore, and targets backport.

* chore: clean up linter warnings

* feat: html harness focuses the example and allows quick navigation with J/K instead.

* chore: readme mentions the property to use nuget vs. the local project reference

* feat: replaced the J/K navigation with good old HTML buttons

* chore: run dotnet format scoped default (was previously scoped to just 'style')
2026-07-30 18:34:34 +01:00

252 lines
8.4 KiB
C#

/*******************************************************************************************
*
* raylib [textures] example - image processing
*
* Example complexity rating: [★★★☆] 3/4
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* Example originally created with raylib 1.4, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2016-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
namespace Examples.Textures;
public partial class ImageProcessing : IExample
{
private const int screenWidth = 800;
private const int screenHeight = 450;
public const int NumProcesses = 9;
public string Name => "Textures / Image Processing";
public string Title => "raylib [textures] example - image processing";
private enum ImageProcess
{
None = 0,
ColorGrayScale,
ColorTint,
ColorInvert,
ColorContrast,
ColorBrightness,
GaussianBlur,
FlipVertical,
FlipHorizontal
}
private string[] processText = {
"NO PROCESSING",
"COLOR GRAYSCALE",
"COLOR TINT",
"COLOR INVERT",
"COLOR CONTRAST",
"COLOR BRIGHTNESS",
"GAUSSIAN BLUR",
"FLIP VERTICAL",
"FLIP HORIZONTAL"
};
private Image imageOrigin;
private Image imageCopy;
private Texture2D texture;
private ImageProcess currentProcess;
private bool textureReload;
private Rectangle[] toggleRecs;
private int mouseHoverRec;
public void Init()
{
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
imageOrigin = LoadImage("resources/parrots.png"); // Loaded in CPU memory (RAM)
ImageFormat(ref imageOrigin, PixelFormat.UncompressedR8G8B8A8); // Format image to RGBA 32bit (required for texture update) <-- ISSUE
texture = LoadTextureFromImage(imageOrigin); // Image converted to texture, GPU memory (VRAM)
imageCopy = ImageCopy(imageOrigin);
currentProcess = ImageProcess.None;
textureReload = false;
toggleRecs = new Rectangle[NumProcesses];
mouseHoverRec = -1;
for (var i = 0; i < NumProcesses; i++)
{
toggleRecs[i] = new Rectangle(40.0f, (float)(50 + 32 * i), 150.0f, 30.0f);
}
}
public unsafe void Update()
{
// Update
//----------------------------------------------------------------------------------
// Mouse toggle group logic
for (var i = 0; i < NumProcesses; i++)
{
if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i]))
{
mouseHoverRec = i;
if (IsMouseButtonReleased(MouseButton.Left))
{
currentProcess = (ImageProcess)i;
textureReload = true;
}
break;
}
else
{
mouseHoverRec = -1;
}
}
// Keyboard toggle group logic
if (IsKeyPressed(KeyboardKey.Down))
{
currentProcess++;
if ((int)currentProcess > (NumProcesses - 1))
{
currentProcess = 0;
}
textureReload = true;
}
else if (IsKeyPressed(KeyboardKey.Up))
{
currentProcess--;
if (currentProcess < 0)
{
currentProcess = ImageProcess.FlipHorizontal;
}
textureReload = true;
}
// Reload texture when required
if (textureReload)
{
UnloadImage(imageCopy); // Unload image-copy data
imageCopy = ImageCopy(imageOrigin); // Restore image-copy from image-origin
// NOTE: Image processing is a costly CPU process to be done every frame,
// If image processing is required in a frame-basis, it should be done
// with a texture and by shaders
switch (currentProcess)
{
case ImageProcess.ColorGrayScale:
ImageColorGrayscale(ref imageCopy);
break;
case ImageProcess.ColorTint:
ImageColorTint(ref imageCopy, Color.Green);
break;
case ImageProcess.ColorInvert:
ImageColorInvert(ref imageCopy);
break;
case ImageProcess.ColorContrast:
ImageColorContrast(ref imageCopy, -40);
break;
case ImageProcess.ColorBrightness:
ImageColorBrightness(ref imageCopy, -80);
break;
case ImageProcess.GaussianBlur:
ImageBlurGaussian(ref imageCopy, 10);
break;
case ImageProcess.FlipVertical:
ImageFlipVertical(ref imageCopy);
break;
case ImageProcess.FlipHorizontal:
ImageFlipHorizontal(ref imageCopy);
break;
default:
break;
}
var pixels = LoadImageColors(imageCopy); // Load pixel data from image (RGBA 32bit)
UpdateTexture(texture, pixels); // Update texture with new image data
UnloadImageColors(pixels); // Unload pixels data from RAM
textureReload = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
DrawText("IMAGE PROCESSING:", 40, 30, 10, Color.DarkGray);
// Draw rectangles
for (var i = 0; i < NumProcesses; i++)
{
DrawRectangleRec(toggleRecs[i], ((i == (int)currentProcess) || (i == mouseHoverRec)) ? Color.SkyBlue : Color.LightGray);
DrawRectangleLines(
(int)toggleRecs[i].X,
(int)toggleRecs[i].Y,
(int)toggleRecs[i].Width,
(int)toggleRecs[i].Height,
((i == (int)currentProcess) || (i == mouseHoverRec)) ? Color.Blue : Color.Gray
);
var labelX = (int)(toggleRecs[i].X + toggleRecs[i].Width / 2);
DrawText(
processText[i],
(int)(labelX - MeasureText(processText[i], 10) / 2),
(int)toggleRecs[i].Y + 11,
10,
((i == (int)currentProcess) || (i == mouseHoverRec)) ? Color.DarkBlue : Color.DarkGray
);
}
var x = screenWidth - texture.Width - 60;
var y = screenHeight / 2 - texture.Height / 2;
DrawTexture(texture, x, y, Color.White);
DrawRectangleLines(x, y, texture.Width, texture.Height, Color.Black);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(texture); // Unload texture from VRAM
UnloadImage(imageOrigin); // Unload image-origin from RAM
UnloadImage(imageCopy); // Unload image-copy from RAM
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing");
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
var game = new ImageProcessing();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}