raylib/examples/textures/textures_image_processing.c

169 lines
6.4 KiB
C
Raw Normal View History

2016-02-11 14:26:45 +03:00
/*******************************************************************************************
*
* raylib [textures] example - Image processing
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 3.5 (www.raylib.com)
2016-02-11 14:26:45 +03:00
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2019-05-20 17:36:42 +03:00
#include <stdlib.h> // Required for: free()
2016-02-11 14:26:45 +03:00
#define NUM_PROCESSES 8
2019-05-20 17:36:42 +03:00
typedef enum {
NONE = 0,
COLOR_GRAYSCALE,
COLOR_TINT,
COLOR_INVERT,
COLOR_CONTRAST,
COLOR_BRIGHTNESS,
FLIP_VERTICAL,
FLIP_HORIZONTAL
2016-02-11 14:26:45 +03:00
} ImageProcess;
static const char *processText[] = {
"NO PROCESSING",
"COLOR GRAYSCALE",
"COLOR TINT",
"COLOR INVERT",
"COLOR CONTRAST",
"COLOR BRIGHTNESS",
"FLIP VERTICAL",
"FLIP HORIZONTAL"
};
2019-05-20 17:36:42 +03:00
int main(void)
2016-02-11 14:26:45 +03:00
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
const int screenWidth = 800;
const int screenHeight = 450;
2016-02-11 14:26:45 +03:00
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image imOrigin = LoadImage("resources/parrots.png"); // Loaded in CPU memory (RAM)
ImageFormat(&imOrigin, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8); // Format image to RGBA 32bit (required for texture update) <-- ISSUE
Texture2D texture = LoadTextureFromImage(imOrigin); // Image converted to texture, GPU memory (VRAM)
Image imCopy = ImageCopy(imOrigin);
2016-02-11 14:26:45 +03:00
int currentProcess = NONE;
bool textureReload = false;
2020-12-18 20:36:04 +03:00
Rectangle toggleRecs[NUM_PROCESSES] = { 0 };
int mouseHoverRec = -1;
2019-05-20 17:36:42 +03:00
2020-12-18 20:36:04 +03:00
for (int i = 0; i < NUM_PROCESSES; i++) toggleRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f };
2019-05-20 17:36:42 +03:00
2016-02-11 14:26:45 +03:00
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
2020-12-18 20:36:04 +03:00
// Mouse toggle group logic
for (int i = 0; i < NUM_PROCESSES; i++)
{
if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i]))
{
mouseHoverRec = i;
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
{
currentProcess = i;
textureReload = true;
}
break;
}
else mouseHoverRec = -1;
}
// Keyboard toggle group logic
2016-02-11 14:26:45 +03:00
if (IsKeyPressed(KEY_DOWN))
{
currentProcess++;
if (currentProcess > (NUM_PROCESSES - 1)) currentProcess = 0;
2016-02-11 14:26:45 +03:00
textureReload = true;
}
else if (IsKeyPressed(KEY_UP))
{
currentProcess--;
if (currentProcess < 0) currentProcess = 7;
textureReload = true;
}
2019-05-20 17:36:42 +03:00
2020-12-18 20:36:04 +03:00
// Reload texture when required
2016-02-11 14:26:45 +03:00
if (textureReload)
{
UnloadImage(imCopy); // Unload image-copy data
imCopy = ImageCopy(imOrigin); // Restore image-copy from image-origin
2019-05-20 17:36:42 +03:00
// 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
2016-02-11 14:26:45 +03:00
// with a texture and by shaders
switch (currentProcess)
{
case COLOR_GRAYSCALE: ImageColorGrayscale(&imCopy); break;
case COLOR_TINT: ImageColorTint(&imCopy, GREEN); break;
case COLOR_INVERT: ImageColorInvert(&imCopy); break;
case COLOR_CONTRAST: ImageColorContrast(&imCopy, -40); break;
case COLOR_BRIGHTNESS: ImageColorBrightness(&imCopy, -80); break;
case FLIP_VERTICAL: ImageFlipVertical(&imCopy); break;
case FLIP_HORIZONTAL: ImageFlipHorizontal(&imCopy); break;
2016-02-11 14:26:45 +03:00
default: break;
}
2019-05-20 17:36:42 +03:00
Color *pixels = LoadImageColors(imCopy); // Load pixel data from image (RGBA 32bit)
2016-02-11 14:26:45 +03:00
UpdateTexture(texture, pixels); // Update texture with new image data
UnloadImageColors(pixels); // Unload pixels data from RAM
2019-05-20 17:36:42 +03:00
2016-02-11 14:26:45 +03:00
textureReload = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
2019-05-20 17:36:42 +03:00
2016-02-11 14:26:45 +03:00
DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY);
2019-05-20 17:36:42 +03:00
2016-02-11 14:26:45 +03:00
// Draw rectangles
for (int i = 0; i < NUM_PROCESSES; i++)
{
2020-12-18 20:36:04 +03:00
DrawRectangleRec(toggleRecs[i], ((i == currentProcess) || (i == mouseHoverRec)) ? SKYBLUE : LIGHTGRAY);
DrawRectangleLines((int)toggleRecs[i].x, (int) toggleRecs[i].y, (int) toggleRecs[i].width, (int) toggleRecs[i].height, ((i == currentProcess) || (i == mouseHoverRec)) ? BLUE : GRAY);
DrawText( processText[i], (int)( toggleRecs[i].x + toggleRecs[i].width/2 - MeasureText(processText[i], 10)/2), (int) toggleRecs[i].y + 11, 10, ((i == currentProcess) || (i == mouseHoverRec)) ? DARKBLUE : DARKGRAY);
2016-02-11 14:26:45 +03:00
}
DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE);
DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK);
2019-05-20 17:36:42 +03:00
2016-02-11 14:26:45 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture from VRAM
UnloadImage(imOrigin); // Unload image-origin from RAM
UnloadImage(imCopy); // Unload image-copy from RAM
2016-02-11 14:26:45 +03:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}