feat: Merging into Menu
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
/build
|
/build
|
||||||
|
/res_images
|
||||||
.idea/
|
.idea/
|
||||||
.vs/
|
.vs/
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
2
Makefile
2
Makefile
@ -15,7 +15,7 @@ INC_DIRS := $(shell find $(SRC_DIRS) -type d)
|
|||||||
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
||||||
|
|
||||||
CPPFLAGS := $(INC_FLAGS) -MMD -MP `sdl2-config --cflags` -Wall -Wextra -Werror -g -pedantic -std=c99
|
CPPFLAGS := $(INC_FLAGS) -MMD -MP `sdl2-config --cflags` -Wall -Wextra -Werror -g -pedantic -std=c99
|
||||||
LDFLAGS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image
|
LDFLAGS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image -lm
|
||||||
|
|
||||||
# Default rule: build both main and solver
|
# Default rule: build both main and solver
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
|
13
src/main.c
13
src/main.c
@ -8,6 +8,7 @@
|
|||||||
#include "utils/EZ_UI/EZ_utils.h"
|
#include "utils/EZ_UI/EZ_utils.h"
|
||||||
#include "utils/EZ_UI/EZ_manager.h"
|
#include "utils/EZ_UI/EZ_manager.h"
|
||||||
#include "utils/Rendering/RenderingUtils.h"
|
#include "utils/Rendering/RenderingUtils.h"
|
||||||
|
#include "utils/Application/ApplicationUtils.h"
|
||||||
|
|
||||||
char flags = 0;
|
char flags = 0;
|
||||||
|
|
||||||
@ -16,8 +17,14 @@ SDL_Renderer* main_renderer;
|
|||||||
|
|
||||||
TTF_Font* main_font;
|
TTF_Font* main_font;
|
||||||
|
|
||||||
int main(void)
|
SDL_Renderer* main_renderer;
|
||||||
|
|
||||||
|
char* application_directory;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
application_directory = path_get_directory(argv[0]);
|
||||||
|
|
||||||
//SDL Init
|
//SDL Init
|
||||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
||||||
SDL_Window* window = SDL_CreateWindow("OCRudoku", 0, 0, 1280, 720, SDL_WINDOW_RESIZABLE);
|
SDL_Window* window = SDL_CreateWindow("OCRudoku", 0, 0, 1280, 720, SDL_WINDOW_RESIZABLE);
|
||||||
@ -33,6 +40,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
main_renderer = renderer;
|
main_renderer = renderer;
|
||||||
|
|
||||||
|
main_renderer = renderer;
|
||||||
|
|
||||||
//TTF init
|
//TTF init
|
||||||
|
|
||||||
if (TTF_Init() == -1) {
|
if (TTF_Init() == -1) {
|
||||||
@ -72,4 +81,6 @@ int main(void)
|
|||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
|
(void)argc, (void) argv;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <SDL_ttf.h>
|
#include <SDL_ttf.h>
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
#ifndef GLOBAL_H
|
#ifndef GLOBAL_H
|
||||||
#define GLOBAL_H
|
#define GLOBAL_H
|
||||||
@ -6,6 +7,9 @@
|
|||||||
extern TTF_Font* main_font;
|
extern TTF_Font* main_font;
|
||||||
|
|
||||||
extern SDL_Window* main_window;
|
extern SDL_Window* main_window;
|
||||||
|
|
||||||
extern SDL_Renderer* main_renderer;
|
extern SDL_Renderer* main_renderer;
|
||||||
|
|
||||||
|
extern char* application_directory;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,9 +6,13 @@
|
|||||||
#include "../utils/EZ_UI/elements/EZ_button.h"
|
#include "../utils/EZ_UI/elements/EZ_button.h"
|
||||||
#include "../utils/EZ_UI/elements/EZ_text.h"
|
#include "../utils/EZ_UI/elements/EZ_text.h"
|
||||||
#include "../utils/EZ_UI/elements/EZ_drag_select.h"
|
#include "../utils/EZ_UI/elements/EZ_drag_select.h"
|
||||||
|
#include "../utils/Spliting/Spliting.h"
|
||||||
|
#include "../utils/Image/ImageUtils.h"
|
||||||
|
#include "../utils/Application/ApplicationUtils.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_ttf.h>
|
#include <SDL_ttf.h>
|
||||||
|
#include <SDL_image.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -107,6 +111,7 @@ void button_action_switch(EZ_button* tmp)
|
|||||||
(void)tmp;
|
(void)tmp;
|
||||||
EZ_select_menu(1);
|
EZ_select_menu(1);
|
||||||
//switch to next page
|
//switch to next page
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_main_menu(SDL_Renderer* renderer)
|
void load_main_menu(SDL_Renderer* renderer)
|
||||||
@ -120,7 +125,6 @@ void load_main_menu(SDL_Renderer* renderer)
|
|||||||
main_menu.process_event = &main_menu_process_events;
|
main_menu.process_event = &main_menu_process_events;
|
||||||
main_menu.unload_menu = &unload_main_menu;
|
main_menu.unload_menu = &unload_main_menu;
|
||||||
|
|
||||||
|
|
||||||
//Image
|
//Image
|
||||||
|
|
||||||
anchore_point main_img_anchore = {
|
anchore_point main_img_anchore = {
|
||||||
|
64
src/utils/Application/ApplicationUtils.c
Normal file
64
src/utils/Application/ApplicationUtils.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
|
char* path_get_directory(char* path)
|
||||||
|
{
|
||||||
|
long int last_index = -1;
|
||||||
|
long int i = 0;
|
||||||
|
|
||||||
|
while (path[i] != '\0')
|
||||||
|
{
|
||||||
|
if (path[i] == '/' || path[i] == '\\')
|
||||||
|
last_index = i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* n_str = (char*)calloc(last_index + 2, sizeof(char));
|
||||||
|
|
||||||
|
if (n_str == NULL)
|
||||||
|
errx(EXIT_FAILURE, "unable to allocate");
|
||||||
|
|
||||||
|
n_str[last_index+1] = '\0';
|
||||||
|
|
||||||
|
if (last_index == -1)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
memcpy(n_str, path, last_index+1);
|
||||||
|
|
||||||
|
return n_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* combine_path(char* first_path, char* second_path)
|
||||||
|
{
|
||||||
|
size_t f_len = strlen(first_path);
|
||||||
|
|
||||||
|
char* res;
|
||||||
|
|
||||||
|
if ((first_path[f_len-1] == '/' || first_path[f_len-1] == '\\') || f_len == 0)
|
||||||
|
{
|
||||||
|
res = (char*)calloc(f_len + strlen(second_path) + 1, sizeof(char));
|
||||||
|
|
||||||
|
if (res == NULL)
|
||||||
|
errx(EXIT_FAILURE, "unable to allocate");
|
||||||
|
|
||||||
|
memcpy(res, first_path, f_len+1);
|
||||||
|
|
||||||
|
strcat(res, second_path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = (char*)calloc(f_len + strlen(second_path) + 2, sizeof(char));
|
||||||
|
|
||||||
|
if (res == NULL)
|
||||||
|
errx(EXIT_FAILURE, "unable to allocate");
|
||||||
|
|
||||||
|
memcpy(res, first_path, f_len+1);
|
||||||
|
|
||||||
|
strcat(res, "/");
|
||||||
|
|
||||||
|
strcat(res, second_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
8
src/utils/Application/ApplicationUtils.h
Normal file
8
src/utils/Application/ApplicationUtils.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef APLLICATION_UTILS_H
|
||||||
|
#define APLLICATION_UTILS_H
|
||||||
|
|
||||||
|
char* path_get_directory(char* path);
|
||||||
|
|
||||||
|
char* combine_path(char* first_path, char* second_path);
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
|
#include <math.h>
|
||||||
#include "ImageUtils.h"
|
#include "ImageUtils.h"
|
||||||
|
|
||||||
void render_image_file(const char* file, SDL_Renderer* renderer, const SDL_Rect* srcrect, const SDL_Rect* dstrect)
|
void render_image_file(const char* file, SDL_Renderer* renderer, const SDL_Rect* srcrect, const SDL_Rect* dstrect)
|
||||||
@ -37,6 +38,77 @@ void render_surface(SDL_Surface* surface, SDL_Renderer* renderer, const SDL_Rect
|
|||||||
errx(1, "render_surface: fail to render texture");
|
errx(1, "render_surface: fail to render texture");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* get_pixel_pt(SDL_Surface* surface, size_t x, size_t y)
|
||||||
|
{
|
||||||
|
size_t bpp = (size_t)surface->format->BytesPerPixel;
|
||||||
|
return (void*)((size_t)surface->pixels + (y * (size_t)surface->pitch + x * bpp));
|
||||||
|
}
|
||||||
|
|
||||||
|
Uint32 get_pixel_val(void* pixel, SDL_PixelFormat* format)
|
||||||
|
{
|
||||||
|
Uint32 pixel32;
|
||||||
|
|
||||||
|
int bpp = format->BytesPerPixel;
|
||||||
|
|
||||||
|
switch (bpp)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
pixel32 = *(Uint8*)pixel;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
pixel32 = *(Uint16*)pixel;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
||||||
|
pixel32 = ((Uint8*)pixel)[0] << 16 | ((Uint8*)pixel)[1] << 8 | ((Uint8*)pixel)[2];
|
||||||
|
else
|
||||||
|
pixel32 = ((Uint8*)pixel)[0] | ((Uint8*)pixel)[1] << 8 | ((Uint8*)pixel)[2] << 16;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
pixel32 = *(Uint32*)pixel;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
pixel32 = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pixel32;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_pixel_val(void* pixel, SDL_PixelFormat* format, Uint32 val)
|
||||||
|
{
|
||||||
|
int bpp = format->BytesPerPixel;
|
||||||
|
|
||||||
|
switch (bpp)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
*(Uint8*)pixel = (Uint8)val;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
*(Uint16*)pixel = (Uint16)val;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
||||||
|
{
|
||||||
|
((Uint8*)pixel)[0] = (Uint8)(val >> 16);
|
||||||
|
((Uint8*)pixel)[1] = (Uint8)(val >> 8);
|
||||||
|
((Uint8*)pixel)[2] = (Uint8)(val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((Uint8*)pixel)[0] = (Uint8)(val);
|
||||||
|
((Uint8*)pixel)[1] = (Uint8)(val >> 8);
|
||||||
|
((Uint8*)pixel)[2] = (Uint8)(val >> 16);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
*(Uint32*)pixel = val;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void pixel_to_grayscale(void* pixel, SDL_PixelFormat* format)
|
void pixel_to_grayscale(void* pixel, SDL_PixelFormat* format)
|
||||||
{
|
{
|
||||||
Uint32* pixel32 = (Uint32*)pixel;
|
Uint32* pixel32 = (Uint32*)pixel;
|
||||||
@ -54,13 +126,13 @@ void pixel_to_grayscale(void* pixel, SDL_PixelFormat* format)
|
|||||||
|
|
||||||
void pixel_to_black_white(void* pixel, SDL_PixelFormat* format, Uint8 threshold)
|
void pixel_to_black_white(void* pixel, SDL_PixelFormat* format, Uint8 threshold)
|
||||||
{
|
{
|
||||||
Uint32* pixel32 = (Uint32*)pixel;
|
|
||||||
|
|
||||||
Uint8 r;
|
Uint8 r;
|
||||||
Uint8 g;
|
Uint8 g;
|
||||||
Uint8 b;
|
Uint8 b;
|
||||||
|
|
||||||
SDL_GetRGB(*pixel32, format, &r, &g, &b);
|
Uint32 val = get_pixel_val(pixel, format);
|
||||||
|
|
||||||
|
SDL_GetRGB(val, format, &r, &g, &b);
|
||||||
|
|
||||||
Uint8 gray = (Uint8)(((Uint32)r + (Uint32)g + (Uint32)b) / 3);
|
Uint8 gray = (Uint8)(((Uint32)r + (Uint32)g + (Uint32)b) / 3);
|
||||||
|
|
||||||
@ -73,48 +145,159 @@ void pixel_to_black_white(void* pixel, SDL_PixelFormat* format, Uint8 threshold)
|
|||||||
gray = 0;
|
gray = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*pixel32 = SDL_MapRGB(format, gray, gray, gray);
|
Uint32 n_val = SDL_MapRGB(format, gray, gray, gray);
|
||||||
|
|
||||||
|
set_pixel_val(pixel, format, n_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_to_grayscale(SDL_Surface* surface)
|
void image_to_black_white(SDL_Surface* surface)
|
||||||
{
|
{
|
||||||
if (SDL_LockSurface(surface) < 0)
|
if (SDL_LockSurface(surface) < 0)
|
||||||
errx(1, "image_to_grayscale: fail to lock surface!");
|
errx(1, "image_to_black_white: fail to lock surface!");
|
||||||
int pixel_size = surface->pitch / surface->w;
|
//int pixel_size = surface->pitch / surface->w;
|
||||||
for (ssize_t x = 0; x < surface->w; x++)
|
for (ssize_t x = 0; x < surface->w; x++)
|
||||||
{
|
{
|
||||||
for (ssize_t y = 0; y < surface->h; y++)
|
for (ssize_t y = 0; y < surface->h; y++)
|
||||||
{
|
{
|
||||||
// Cast surface->pixels to uint8_t* for pointer arithmetic
|
// Cast surface->pixels to uint8_t* for pointer arithmetic
|
||||||
uint8_t* pixel = (uint8_t*)(surface->pixels) + ((x + y * surface->w) * pixel_size);
|
|
||||||
pixel_to_black_white(pixel, surface->format, 127);
|
//uint8_t* pixel = (uint8_t*)(surface->pixels) + ((x + y * surface->w) * pixel_size);
|
||||||
|
pixel_to_black_white(get_pixel_pt(surface, x, y), surface->format, 127);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SDL_UnlockSurface(surface);
|
SDL_UnlockSurface(surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*int test()
|
SDL_Surface* create_sub_surface(SDL_Surface* surface, SDL_Rect rect)
|
||||||
{
|
{
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Surface* sub = SDL_CreateRGBSurface(0,rect.w,rect.h,32,0,0,0,0);
|
||||||
SDL_Window* test_window = SDL_CreateWindow("Test", 0, 0, 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_INPUT_GRABBED);
|
|
||||||
SDL_Renderer* renderer = SDL_CreateRenderer(test_window, -1, 0);
|
|
||||||
|
|
||||||
char* image_path = "HandWriting.png";
|
if (sub == NULL)
|
||||||
|
errx(EXIT_FAILURE, "failed to create sub-surface");
|
||||||
|
|
||||||
SDL_Surface* surface = IMG_Load(image_path);
|
for (int x = 0; x < rect.w; x++)
|
||||||
|
{
|
||||||
|
int pos_x = x + rect.x;
|
||||||
|
for (int y = 0; y < rect.h; y++)
|
||||||
|
{
|
||||||
|
int pos_y = y + rect.y;
|
||||||
|
|
||||||
image_to_grayscale(surface);
|
Uint32 val = get_pixel_val(get_pixel_pt(surface, pos_x, pos_y), surface->format);
|
||||||
|
|
||||||
SDL_Rect window_rect;
|
Uint8 r,g,b;
|
||||||
|
|
||||||
window_rect.x = 100;
|
SDL_GetRGB(val, surface->format, &r, &g, &b);
|
||||||
window_rect.y = 100;
|
|
||||||
window_rect.w = 1080;
|
|
||||||
window_rect.h = 520;
|
|
||||||
|
|
||||||
render_surface(surface, renderer, NULL, &window_rect);
|
Uint32 n_val = SDL_MapRGB(surface->format, r, g, b);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
set_pixel_val(get_pixel_pt(sub, x, y), surface->format, n_val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sleep(5);
|
return sub;
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Surface* rotate_pixels(SDL_Surface* surface, double angle) {
|
||||||
|
int original_w = surface->w;
|
||||||
|
int original_h = surface->h;
|
||||||
|
|
||||||
|
double rad_angle = angle * M_PI / 180.0;
|
||||||
|
int new_w = fabs(original_w * cos(rad_angle)) + fabs(original_h * sin(rad_angle));
|
||||||
|
int new_h = fabs(original_w * sin(rad_angle)) + fabs(original_h * cos(rad_angle));
|
||||||
|
|
||||||
|
SDL_Surface* rotated_surface = SDL_CreateRGBSurface(0, new_w, new_h, surface->format->BitsPerPixel,
|
||||||
|
surface->format->Rmask, surface->format->Gmask,
|
||||||
|
surface->format->Bmask, surface->format->Amask);
|
||||||
|
|
||||||
|
if (!rotated_surface) {
|
||||||
|
errx(1, "rotate_pixels: fail to create rotated surface!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SDL_LockSurface(surface) < 0 || SDL_LockSurface(rotated_surface) < 0) {
|
||||||
|
errx(1, "rotate_pixels: fail to lock surface!");
|
||||||
|
}
|
||||||
|
|
||||||
|
int pixel_size = surface->pitch / surface->w;
|
||||||
|
int center_x = original_w / 2;
|
||||||
|
int center_y = original_h / 2;
|
||||||
|
int new_center_x = new_w / 2;
|
||||||
|
int new_center_y = new_h / 2;
|
||||||
|
|
||||||
|
uint8_t* original_pixels = (uint8_t*)surface->pixels;
|
||||||
|
uint8_t* rotated_pixels = (uint8_t*)rotated_surface->pixels;
|
||||||
|
|
||||||
|
for (int x = 0; x < new_w; x++) {
|
||||||
|
for (int y = 0; y < new_h; y++) {
|
||||||
|
int source_x = (int)((x - new_center_x) * cos(-rad_angle) - (y - new_center_y) * sin(-rad_angle) + center_x);
|
||||||
|
int source_y = (int)((x - new_center_x) * sin(-rad_angle) + (y - new_center_y) * cos(-rad_angle) + center_y);
|
||||||
|
|
||||||
|
if (source_x >= 0 && source_x < original_w && source_y >= 0 && source_y < original_h) {
|
||||||
|
uint8_t* src_pixel = original_pixels + (source_y * surface->pitch + source_x * pixel_size);
|
||||||
|
uint8_t* dest_pixel = rotated_pixels + (y * rotated_surface->pitch + x * pixel_size);
|
||||||
|
memcpy(dest_pixel, src_pixel, pixel_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_UnlockSurface(surface);
|
||||||
|
SDL_UnlockSurface(rotated_surface);
|
||||||
|
return rotated_surface;
|
||||||
|
}
|
||||||
|
|
||||||
|
// int testsupercool(void) {
|
||||||
|
// if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||||
|
// errx(1, "SDL_Init Error: %s", SDL_GetError());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// SDL_Window* window = SDL_CreateWindow("Test", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
|
||||||
|
// if (!window) {
|
||||||
|
// errx(1, "SDL_CreateWindow Error: %s", SDL_GetError());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
|
// if (!renderer) {
|
||||||
|
// SDL_DestroyWindow(window);
|
||||||
|
// errx(1, "SDL_CreateRenderer Error: %s", SDL_GetError());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// SDL_Surface* image_surface = IMG_Load("HandWriting.png");
|
||||||
|
// if (!image_surface) {
|
||||||
|
// SDL_DestroyRenderer(renderer);
|
||||||
|
// SDL_DestroyWindow(window);
|
||||||
|
// errx(1, "SDL_LoadBMP Error: %s", SDL_GetError());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// SDL_Surface* rotated_surface = rotate_pixels(image_surface, 90);
|
||||||
|
|
||||||
|
// // Déclaration et initialisation de window_rect pour centrer l'image pivotée
|
||||||
|
// SDL_Rect window_rect;
|
||||||
|
// window_rect.x = (800 - rotated_surface->w) / 2;
|
||||||
|
// window_rect.y = (600 - rotated_surface->h) / 2;
|
||||||
|
// window_rect.w = rotated_surface->w;
|
||||||
|
// window_rect.h = rotated_surface->h;
|
||||||
|
|
||||||
|
// // Rendu de l'image pivotée
|
||||||
|
// SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, rotated_surface);
|
||||||
|
// SDL_FreeSurface(image_surface);
|
||||||
|
// SDL_FreeSurface(rotated_surface);
|
||||||
|
|
||||||
|
// if (!texture) {
|
||||||
|
// SDL_DestroyRenderer(renderer);
|
||||||
|
// SDL_DestroyWindow(window);
|
||||||
|
// errx(1, "SDL_CreateTextureFromSurface Error: %s", SDL_GetError());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// SDL_RenderClear(renderer);
|
||||||
|
// SDL_RenderCopy(renderer, texture, NULL, &window_rect);
|
||||||
|
// SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
|
// SDL_Delay(5000);
|
||||||
|
|
||||||
|
// SDL_DestroyTexture(texture);
|
||||||
|
// SDL_DestroyRenderer(renderer);
|
||||||
|
// SDL_DestroyWindow(window);
|
||||||
|
// SDL_Quit();
|
||||||
|
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
#include <SDL_render.h>
|
#include <SDL_render.h>
|
||||||
|
|
||||||
|
#ifndef M_PI
|
||||||
|
#define M_PI acos(-1.0)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef IMAGE_UTILS_H
|
#ifndef IMAGE_UTILS_H
|
||||||
#define IMAGE_UTILS_H
|
#define IMAGE_UTILS_H
|
||||||
|
|
||||||
@ -11,10 +15,25 @@ void render_full_image_file(const char* file, SDL_Renderer* renderer, const SDL_
|
|||||||
|
|
||||||
void render_surface(SDL_Surface* surface, SDL_Renderer* renderer, const SDL_Rect* srcrect, const SDL_Rect* dstrect);
|
void render_surface(SDL_Surface* surface, SDL_Renderer* renderer, const SDL_Rect* srcrect, const SDL_Rect* dstrect);
|
||||||
|
|
||||||
|
void* get_pixel_pt(SDL_Surface* surface, size_t x, size_t y);
|
||||||
|
|
||||||
|
Uint32 get_pixel_val(void* pixel, SDL_PixelFormat* format);
|
||||||
|
|
||||||
|
void set_pixel_val(void* pixel, SDL_PixelFormat* format, Uint32 val);
|
||||||
|
|
||||||
void pixel_to_grayscale(void* pixel, SDL_PixelFormat* format);
|
void pixel_to_grayscale(void* pixel, SDL_PixelFormat* format);
|
||||||
|
|
||||||
void pixel_to_black_white(void* pixel, SDL_PixelFormat* format, Uint8 threshold);
|
void pixel_to_black_white(void* pixel, SDL_PixelFormat* format, Uint8 threshold);
|
||||||
|
|
||||||
void image_to_grayscale(SDL_Surface* surface);
|
void image_to_black_white(SDL_Surface* surface);
|
||||||
|
|
||||||
|
SDL_Surface* create_sub_surface(SDL_Surface* surface, SDL_Rect rect);
|
||||||
|
|
||||||
|
// void rotate_pixels(SDL_Surface* surface, double angle);
|
||||||
|
|
||||||
|
SDL_Surface* rotate_pixels(SDL_Surface* surface, double angle);
|
||||||
|
|
||||||
|
|
||||||
|
int testsupercool(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
290
src/utils/Spliting/Spliting.c
Normal file
290
src/utils/Spliting/Spliting.c
Normal file
@ -0,0 +1,290 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "Spliting.h"
|
||||||
|
#include "../Image/ImageUtils.h"
|
||||||
|
|
||||||
|
int split_margin = 1;
|
||||||
|
|
||||||
|
int get_grid_vertical_nb(SDL_Surface* surface, SDL_Rect rect)
|
||||||
|
{
|
||||||
|
int nb_vertical = 0;
|
||||||
|
|
||||||
|
int search_full_line = 0;
|
||||||
|
|
||||||
|
for (int y = 0; y < rect.h; y++)
|
||||||
|
{
|
||||||
|
int pos_y = rect.y + y;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int x = 0; x < rect.w; x++)
|
||||||
|
{
|
||||||
|
int pos_x = rect.x + x;
|
||||||
|
|
||||||
|
void* pixel = get_pixel_pt(surface, pos_x, pos_y);
|
||||||
|
|
||||||
|
Uint32 pixel32 = get_pixel_val(pixel, surface->format);
|
||||||
|
|
||||||
|
Uint8 r, g, b;
|
||||||
|
|
||||||
|
SDL_GetRGB(pixel32, surface->format, &r, &g, &b);
|
||||||
|
|
||||||
|
float bright = ((float)(r + g + b)) / 3.0;
|
||||||
|
|
||||||
|
if (bright < 127.0)
|
||||||
|
sum++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search_full_line && sum > split_margin)
|
||||||
|
{
|
||||||
|
search_full_line = 0;
|
||||||
|
nb_vertical++;
|
||||||
|
}
|
||||||
|
else if (!search_full_line && (sum <= split_margin || sum + split_margin >= rect.w))
|
||||||
|
{
|
||||||
|
search_full_line = 1;
|
||||||
|
nb_vertical++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nb_vertical - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* get_grid_vertical_split(SDL_Surface* surface, SDL_Rect rect, int* nb_splits)
|
||||||
|
{
|
||||||
|
int nb_split = get_grid_vertical_nb(surface, rect);
|
||||||
|
|
||||||
|
int* vertical_split_array = (int*)calloc(nb_split, sizeof(int));
|
||||||
|
|
||||||
|
*nb_splits = nb_split - (nb_split % 2);
|
||||||
|
|
||||||
|
if (vertical_split_array == NULL)
|
||||||
|
errx(EXIT_FAILURE, "Failed to create result array: Not enought memory");
|
||||||
|
|
||||||
|
int nb_vertical = 0;
|
||||||
|
|
||||||
|
int search_full_line = 0;
|
||||||
|
|
||||||
|
for (int y = 0; y < rect.h; y++)
|
||||||
|
{
|
||||||
|
int pos_y = rect.y + y;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int x = 0; x < rect.w; x++)
|
||||||
|
{
|
||||||
|
int pos_x = rect.x + x;
|
||||||
|
|
||||||
|
void* pixel = get_pixel_pt(surface, pos_x, pos_y);
|
||||||
|
|
||||||
|
Uint32 pixel32 = get_pixel_val(pixel, surface->format);
|
||||||
|
|
||||||
|
Uint8 r, g, b;
|
||||||
|
|
||||||
|
SDL_GetRGB(pixel32, surface->format, &r, &g, &b);
|
||||||
|
|
||||||
|
float bright = ((float)(r + g + b)) / 3.0;
|
||||||
|
|
||||||
|
if (bright < 127.0)
|
||||||
|
sum++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search_full_line && sum > split_margin)
|
||||||
|
{
|
||||||
|
search_full_line = 0;
|
||||||
|
if (nb_vertical > 0)
|
||||||
|
vertical_split_array[nb_vertical-1] = pos_y-1;
|
||||||
|
nb_vertical++;
|
||||||
|
}
|
||||||
|
else if (!search_full_line && (sum <= split_margin || sum + split_margin >= rect.w))
|
||||||
|
{
|
||||||
|
search_full_line = 1;
|
||||||
|
if (nb_vertical > 0)
|
||||||
|
vertical_split_array[nb_vertical-1] = pos_y;
|
||||||
|
nb_vertical++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return vertical_split_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_grid_horizontal_nb(SDL_Surface* surface, SDL_Rect rect)
|
||||||
|
{
|
||||||
|
int nb_horizontal = 0;
|
||||||
|
|
||||||
|
int search_full_line = 0;
|
||||||
|
|
||||||
|
for (int x = 0; x < rect.w; x++)
|
||||||
|
{
|
||||||
|
int pos_x = rect.x + x;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int y = 0; y < rect.h; y++)
|
||||||
|
{
|
||||||
|
int pos_y = rect.y + y;
|
||||||
|
|
||||||
|
void* pixel = get_pixel_pt(surface, pos_x, pos_y);
|
||||||
|
|
||||||
|
Uint32 pixel32 = get_pixel_val(pixel, surface->format);
|
||||||
|
|
||||||
|
Uint8 r, g, b;
|
||||||
|
|
||||||
|
SDL_GetRGB(pixel32, surface->format, &r, &g, &b);
|
||||||
|
|
||||||
|
float bright = ((float)(r + g + b)) / 3.0;
|
||||||
|
|
||||||
|
if (bright < 127.0)
|
||||||
|
sum++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search_full_line && sum > split_margin)
|
||||||
|
{
|
||||||
|
search_full_line = 0;
|
||||||
|
nb_horizontal++;
|
||||||
|
}
|
||||||
|
else if (!search_full_line && (sum <= split_margin || sum + split_margin >= rect.w))
|
||||||
|
{
|
||||||
|
search_full_line = 1;
|
||||||
|
nb_horizontal++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nb_horizontal - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* get_grid_horizontal_split(SDL_Surface* surface, SDL_Rect rect, int* nb_splits)
|
||||||
|
{
|
||||||
|
int nb_split = get_grid_horizontal_nb(surface, rect);
|
||||||
|
|
||||||
|
int* horizontal_split_array = (int*)calloc(nb_split, sizeof(int));
|
||||||
|
|
||||||
|
*nb_splits = nb_split - (nb_split % 2);
|
||||||
|
|
||||||
|
if (horizontal_split_array == NULL)
|
||||||
|
errx(EXIT_FAILURE, "Failed to create result array: Not enought memory");
|
||||||
|
|
||||||
|
int nb_horizontal = 0;
|
||||||
|
|
||||||
|
int search_full_line = 0;
|
||||||
|
|
||||||
|
for (int x = 0; x < rect.w; x++)
|
||||||
|
{
|
||||||
|
int pos_x = rect.x + x;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int y = 0; y < rect.h; y++)
|
||||||
|
{
|
||||||
|
int pos_y = rect.y + y;
|
||||||
|
|
||||||
|
void* pixel = get_pixel_pt(surface, pos_x, pos_y);
|
||||||
|
|
||||||
|
Uint32 pixel32 = get_pixel_val(pixel, surface->format);
|
||||||
|
|
||||||
|
Uint8 r, g, b;
|
||||||
|
|
||||||
|
SDL_GetRGB(pixel32, surface->format, &r, &g, &b);
|
||||||
|
|
||||||
|
float bright = ((float)(r + g + b)) / 3.0;
|
||||||
|
|
||||||
|
if (bright < 127.0)
|
||||||
|
sum++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search_full_line && sum > split_margin)
|
||||||
|
{
|
||||||
|
search_full_line = 0;
|
||||||
|
if (nb_horizontal > 0)
|
||||||
|
horizontal_split_array[nb_horizontal-1] = pos_x-1;
|
||||||
|
nb_horizontal++;
|
||||||
|
}
|
||||||
|
else if (!search_full_line && (sum <= split_margin || sum + split_margin >= rect.w))
|
||||||
|
{
|
||||||
|
search_full_line = 1;
|
||||||
|
if (nb_horizontal > 0)
|
||||||
|
horizontal_split_array[nb_horizontal-1] = pos_x;
|
||||||
|
nb_horizontal++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return horizontal_split_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Rect** get_grid_split(SDL_Surface* surface, SDL_Rect rect, int* first_dim, int* second_dim)
|
||||||
|
{
|
||||||
|
int s_w, s_h;
|
||||||
|
|
||||||
|
int* vertical_splits = get_grid_vertical_split(surface, rect, &s_h);
|
||||||
|
int* horizontal_splits = get_grid_horizontal_split(surface, rect, &s_w);
|
||||||
|
|
||||||
|
*first_dim = s_w / 2;
|
||||||
|
*second_dim = s_h / 2;
|
||||||
|
|
||||||
|
SDL_Rect** res = (SDL_Rect**)calloc(*first_dim, sizeof(SDL_Rect*));
|
||||||
|
|
||||||
|
if (res == NULL)
|
||||||
|
errx(EXIT_FAILURE, "get_grid_split: unable to allocate memory");
|
||||||
|
|
||||||
|
for (int x = 1; x < s_w; x+=2)
|
||||||
|
{
|
||||||
|
res[(x-1)/2] = (SDL_Rect*)calloc(*second_dim, sizeof(SDL_Rect));
|
||||||
|
|
||||||
|
if (res[(x-1)/2] == NULL)
|
||||||
|
errx(EXIT_FAILURE, "get_grid_split: unable to allocate memory");
|
||||||
|
|
||||||
|
for (int y = 1; y < s_h; y+=2)
|
||||||
|
{
|
||||||
|
SDL_Rect split = {
|
||||||
|
horizontal_splits[x-1],
|
||||||
|
vertical_splits[y-1],
|
||||||
|
horizontal_splits[x] - horizontal_splits[x-1],
|
||||||
|
vertical_splits[y] - vertical_splits[y-1]
|
||||||
|
};
|
||||||
|
|
||||||
|
res[(x-1)/2][(y-1)/2] = split;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void export_split_grid(SDL_Surface* surface, SDL_Rect rect, const char* save_dir)
|
||||||
|
{
|
||||||
|
int first_dim;
|
||||||
|
int second_dim;
|
||||||
|
|
||||||
|
struct stat st = {0};
|
||||||
|
|
||||||
|
if (stat(save_dir, &st) == -1)
|
||||||
|
mkdir(save_dir, 0700);
|
||||||
|
|
||||||
|
SDL_Rect** splits = get_grid_split(surface, rect, &first_dim, &second_dim);
|
||||||
|
|
||||||
|
for (int x = 0; x < first_dim; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < second_dim; y++)
|
||||||
|
{
|
||||||
|
SDL_Surface* new_s = create_sub_surface(surface, splits[x][y]);
|
||||||
|
|
||||||
|
char* file_path = (char*)calloc(strlen(save_dir)+35+1, sizeof(char));
|
||||||
|
char* file_name = (char*)calloc(35, sizeof(char));
|
||||||
|
|
||||||
|
if (file_path == NULL || file_name == NULL)
|
||||||
|
errx(EXIT_FAILURE, "unable to allocate memory");
|
||||||
|
|
||||||
|
strcpy(file_path, save_dir);
|
||||||
|
|
||||||
|
sprintf(file_name, "letter_%i_%i.png", x, y);
|
||||||
|
|
||||||
|
file_path = strcat(file_path, file_name);
|
||||||
|
|
||||||
|
if (IMG_SavePNG(new_s, file_path))
|
||||||
|
errx(EXIT_FAILURE, "Unable to export images");
|
||||||
|
|
||||||
|
free(file_path);
|
||||||
|
free(file_name);
|
||||||
|
SDL_FreeSurface(new_s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
src/utils/Spliting/Spliting.h
Normal file
16
src/utils/Spliting/Spliting.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
#ifndef SPLITING_H
|
||||||
|
#define SPLITING_H
|
||||||
|
|
||||||
|
void spilt_grid_vertical(SDL_Surface* surface, SDL_Rect rect);
|
||||||
|
|
||||||
|
int* get_grid_vertical_split(SDL_Surface* surface, SDL_Rect rect, int* nb_splits);
|
||||||
|
|
||||||
|
int* get_grid_horizontal_split(SDL_Surface* surface, SDL_Rect rect, int* nb_splits);
|
||||||
|
|
||||||
|
SDL_Rect** get_grid_split(SDL_Surface* surface, SDL_Rect rect, int* first_dim, int* second_dim);
|
||||||
|
|
||||||
|
void export_split_grid(SDL_Surface* surface, SDL_Rect rect, const char* save_dir);
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user