forked from GabRayz/OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.c
375 lines (318 loc) · 8.94 KB
/
window.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <dirent.h>
#include "window.h"
#include "image.h"
#include "neuralnetwork.h"
#include "main.h"
#include "spellcheck.h"
#include "hough.h"
#include "ccl.h"
int count_saved_files()
{
int count = 0;
DIR *src = opendir("res");
struct dirent *file;
while ((file = readdir(src)) != NULL)
{
count++;
}
return count;
}
void save_result(char *res, char *filepath)
{
FILE *file = fopen(filepath, "w");
if (file == NULL)
{
printf("Failed to save the result\n");
return;
}
fprintf(file, "%s", res);
fclose(file);
printf("Result saved at: %s\n", filepath);
}
char *call_nn(Img *img)
{
Img *rotated = hough(img);
img_save(rotated, "res/rotated.png");
Img *new = img_import("res/rotated.png");
LinkedList *chars = ccl_segmentation(new, true);
NeuralNetwork *nn = nn_load("save/all");
char* res = send_images_to_cerveau(chars,nn);
printf("Spell checking...\n");
res = spellcheck(res);
return res;
}
int isClicked(SDL_MouseButtonEvent mouse, SDL_Rect btn, int btn_height, int btn_width)
{
return mouse.y > btn.y && mouse.y <= btn.y + btn_height && mouse.x > btn.x && mouse.x <= btn.x + btn_width;
}
int isOver(SDL_Rect btn, int btn_height, int btn_width)
{
int x;
int y;
SDL_GetMouseState(&x, &y);
return y > btn.y && y <= btn.y + btn_height && x > btn.x && x <= btn.x + btn_width;
}
void init_window()
{
//Create Window
SDL_Window *screen = NULL;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
int right = 563;
int left = 702;
int height = 795;
int width = right + left;
int margin = 10;
screen = SDL_CreateWindow("OCR - Les Croisillons", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
if (screen == NULL)
{
SDL_Log("Could not create window: %s", SDL_GetError());
SDL_Quit();
}
SDL_Surface *pScreen = SDL_GetWindowSurface(screen);
Uint32 color = SDL_MapRGB(pScreen->format, 75, 75, 75);
SDL_FillRect(pScreen, NULL, color);
//Create Gradien from img
SDL_Surface *gradient;
gradient = IMG_Load("Img/Gradient3.png");
SDL_Rect gradient_pos;
gradient_pos.x = 0;
gradient_pos.y = 0;
SDL_BlitSurface(gradient, NULL, pScreen, &gradient_pos);
//Create Drap&Drop side from img
SDL_Surface *dragdrop;
dragdrop = IMG_Load("Img/DragDrop3.png");
SDL_Rect dragdrop_pos;
dragdrop_pos.x = 563;
dragdrop_pos.y = 0;
SDL_BlitSurface(dragdrop, NULL, pScreen, &dragdrop_pos);
//Create rght area side from img
SDL_Surface *right_area;
right_area = IMG_Load("Img/RightArea.png");
SDL_Rect right_area_pos;
right_area_pos.x = 563;
right_area_pos.y = 0;
//Create GroupName from img
SDL_Surface *group_name;
group_name = IMG_Load("Img/GroupName3.png");
SDL_Rect group_name_pos;
group_name_pos.x = 120;
group_name_pos.y = 35;
SDL_BlitSurface(group_name, NULL, pScreen, &group_name_pos);
//Create Logo
SDL_Surface *logo;
logo = IMG_Load("Img/Logo3.png");
SDL_Rect logo_pos;
logo_pos.x = 200;
logo_pos.y = 150;
SDL_BlitSurface(logo, NULL, pScreen, &logo_pos);
//Create font
TTF_Font *font;
font = TTF_OpenFont("./Font/Raleway-Regular.ttf", 24);
SDL_Color font_color = {255, 255, 255, 0};
SDL_Color black = {0, 0, 0, 0};
//Create Text area
// int menu_x = 130; //200;
int menu_y = 400;
int menu_step = 60;
SDL_Surface *digitalize;
digitalize = TTF_RenderUTF8_Blended(font, "DIGITALIZE!", font_color);
SDL_Rect digitalize_pos;
digitalize_pos.x = 220;
digitalize_pos.y = 710;
SDL_Surface *save_btn;
save_btn = TTF_RenderUTF8_Blended(font, "SAVE", font_color);
SDL_Rect save_btn_pos;
save_btn_pos.x = 870;
save_btn_pos.y = 710;
SDL_Surface *save_at_btn;
save_at_btn = TTF_RenderUTF8_Blended(font, "SAVED AT RES/", font_color);
SDL_Rect save_at_btn_pos;
save_at_btn_pos.x = 820;
save_at_btn_pos.y = 710;
//Process button
SDL_Surface *digitalize_btn;
digitalize_btn = IMG_Load("Img/Btn.png");
SDL_Rect digitalize_btn_pos;
digitalize_btn_pos.x = 100;
digitalize_btn_pos.y = 650;
//Return button
SDL_Surface *return_btn;
return_btn = IMG_Load("Img/Return3.png");
SDL_Rect return_btn_pos;
return_btn_pos.x = 0;
return_btn_pos.y = 661;
//Quit button
SDL_Surface *quit_btn;
quit_btn = IMG_Load("Img/Quit3.png");
SDL_Rect quit_btn_pos;
quit_btn_pos.x = 400;
quit_btn_pos.y = 661;
SDL_Rect quit_pos;
quit_pos.x = 190;
quit_pos.y = menu_y + menu_step * 4;
SDL_BlitSurface(quit_btn, NULL, pScreen, &quit_pos);
//Save buton
SDL_Rect saved_btn_pos;
saved_btn_pos.x = 720;
saved_btn_pos.y = 650;
// imported image
SDL_Surface *image = NULL;
SDL_Surface *result;
SDL_Rect result_pos;
result_pos.x = dragdrop_pos.x+margin;
result_pos.y = dragdrop_pos.y+margin;
// SDL_BlitSurface(result, NULL, pScreen, &result_pos);
SDL_Rect display_img_pos;
display_img_pos.x = 0;
display_img_pos.y = 0;
display_img_pos.w = right;
display_img_pos.h = height;
display_img_pos.y = 100;
//Refresh the screen
SDL_UpdateWindowSurface(screen);
//Event Loop
int run = 1;
SDL_Event event;
//Enable drop file
char *dropped_filedir;
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
Img *img = NULL;
char *res = NULL;
int nb_file;
int right_y = 1;
int state = 0;
while (run)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
run = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE: // Esc to quit the program
run = 0;
break;
}
break;
case SDL_MOUSEBUTTONUP:
// If clicked on QUIT
if ((isClicked(event.button, quit_pos, 140, 140) & (state == 0))
|| (isClicked(event.button, quit_btn_pos, 140, 140) & (state == 1 || state == 2)))
{
run = 0;
}
// If clicked on RETURN
else if (isClicked(event.button, return_btn_pos, 140,140) & (state == 1|| state == 2))
{
state = 0;
SDL_BlitSurface(gradient, NULL, pScreen, &gradient_pos);
SDL_BlitSurface(dragdrop, NULL, pScreen, &dragdrop_pos);
SDL_BlitSurface(group_name, NULL, pScreen, &group_name_pos);
SDL_BlitSurface(logo, NULL, pScreen, &logo_pos);
SDL_BlitSurface(quit_btn, NULL, pScreen, &quit_pos);
}
// If clicked on DIGITALIZE
else if (isClicked(event.button, digitalize_btn_pos, 184, 368) & (state == 1))
{
res = call_nn(img);
if(res != NULL)
{
SDL_BlitSurface(right_area, NULL, pScreen, &right_area_pos);
result = TTF_RenderText_Blended_Wrapped(font, res, black,left-margin*2);
SDL_BlitSurface(result, NULL, pScreen, &result_pos);
state = 2;
SDL_BlitSurface(digitalize_btn,NULL,pScreen, &saved_btn_pos);
SDL_BlitSurface(save_btn,NULL,pScreen, &save_btn_pos);
}
}
// If clicked on SAVE
else if (isClicked(event.button, saved_btn_pos, 184, 368) & (state == 2))
{
char buffer[11];
nb_file = count_saved_files()-2;
sprintf(buffer, "%d", nb_file);
char *c = malloc(sizeof(char) * 16);
strcpy(c, "res/save");
strcat(c, buffer);
strcat(c, ".txt");
save_result(res,c);
SDL_BlitSurface(digitalize_btn,NULL,pScreen, &saved_btn_pos);
SDL_BlitSurface(save_at_btn,NULL,pScreen, &save_at_btn_pos);
state = 1;
}
break;
case (SDL_DROPFILE):
{
// In case if dropped file
dropped_filedir = event.drop.file;
img = img_import(dropped_filedir);
if (img)
{
SDL_BlitSurface(gradient, NULL, pScreen, &gradient_pos);
SDL_BlitSurface(group_name, NULL, pScreen, &group_name_pos);
//Resize and Display image to the left
image = IMG_Load(dropped_filedir);
float w = image->w;
float h = image->h;
display_img_pos.h = right * h / w;
SDL_BlitScaled(image, NULL, pScreen, &display_img_pos);
//Blit new menu
state = 1;
SDL_BlitSurface(digitalize_btn,NULL,pScreen, &digitalize_btn_pos);
SDL_BlitSurface(digitalize,NULL,pScreen, &digitalize_pos);
SDL_BlitSurface(return_btn,NULL,pScreen,&return_btn_pos);
SDL_BlitSurface(quit_btn,NULL,pScreen,&quit_btn_pos);
//Refresh the screen
SDL_UpdateWindowSurface(screen);
}
// Free dropped_filedir memory
// SDL_free(dropped_filedir);
break;
}
case (SDL_MOUSEWHEEL):
{
if ((state == 1 || state == 2) & isOver(dragdrop_pos,height,left))
{
SDL_BlitSurface(right_area, NULL, pScreen, &right_area_pos);
if (event.wheel.y > 0) // scroll up
{
right_y ++;
result_pos.y -= 30*right_y;
SDL_BlitSurface(result, NULL, pScreen, &result_pos);
}
else if (event.wheel.y < 0) // scroll down
{
if(result_pos.y- 30*right_y < 0)
right_y --;
result_pos.y -= 30*right_y;
SDL_BlitSurface(result, NULL, pScreen, &result_pos);
}
SDL_BlitSurface(digitalize_btn,NULL,pScreen, &saved_btn_pos);
SDL_BlitSurface(save_btn,NULL,pScreen, &save_btn_pos);
}
break;
}
}
//Refresh the screen
SDL_UpdateWindowSurface(screen);
}
//Free everything
SDL_FreeSurface(gradient);
SDL_FreeSurface(dragdrop);
SDL_FreeSurface(group_name);
SDL_FreeSurface(logo);
SDL_FreeSurface(digitalize_btn);
SDL_FreeSurface(image);
SDL_FreeSurface(right_area);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}