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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*******************************************************************************************
*
* raylib [textures] example - Image processing
*
* 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-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <stdlib.h> // Required for: free()
#define NUM_PROCESSES 9
typedef enum {
NONE = 0,
COLOR_GRAYSCALE,
COLOR_TINT,
COLOR_INVERT,
COLOR_CONTRAST,
COLOR_BRIGHTNESS,
GAUSSIAN_BLUR,
FLIP_VERTICAL,
FLIP_HORIZONTAL
} ImageProcess;
static const char *processText[] = {
"NO PROCESSING",
"COLOR GRAYSCALE",
"COLOR TINT",
"COLOR INVERT",
"COLOR CONTRAST",
"COLOR BRIGHTNESS",
"GAUSSIAN BLUR",
"FLIP VERTICAL",
"FLIP HORIZONTAL"
};
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
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);
int currentProcess = NONE;
bool textureReload = false;
Rectangle toggleRecs[NUM_PROCESSES] = { 0 };
int mouseHoverRec = -1;
for (int i = 0; i < NUM_PROCESSES; i++) toggleRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f };
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Mouse toggle group logic
for (int i = 0; i < NUM_PROCESSES; i++)
{
if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i]))
{
mouseHoverRec = i;
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
currentProcess = i;
textureReload = true;
}
break;
}
else mouseHoverRec = -1;
}
// Keyboard toggle group logic
if (IsKeyPressed(KEY_DOWN))
{
currentProcess++;
if (currentProcess > (NUM_PROCESSES - 1)) currentProcess = 0;
textureReload = true;
}
else if (IsKeyPressed(KEY_UP))
{
currentProcess--;
if (currentProcess < 0) currentProcess = 7;
textureReload = true;
}
// Reload texture when required
if (textureReload)
{
UnloadImage(imCopy); // Unload image-copy data
imCopy = ImageCopy(imOrigin); // 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 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 GAUSSIAN_BLUR: ImageBlurGaussian(&imCopy, 10); break;
case FLIP_VERTICAL: ImageFlipVertical(&imCopy); break;
case FLIP_HORIZONTAL: ImageFlipHorizontal(&imCopy); break;
default: break;
}
Color *pixels = LoadImageColors(imCopy); // 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(RAYWHITE);
DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY);
// Draw rectangles
for (int i = 0; i < NUM_PROCESSES; i++)
{
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);
}
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);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture from VRAM
UnloadImage(imOrigin); // Unload image-origin from RAM
UnloadImage(imCopy); // Unload image-copy from RAM
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
|