-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
491 lines (401 loc) · 13.4 KB
/
main.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
* @file IUPGL.c Simple exemple of use of OpenGL and IUP.
*
* Creates a dialog with a tool bar, a canvas and a msg label bar.
* It is a simple WIMP program with color assigment for each pixel
* in the canvas, handling of mouse events and messages.
*
* Link this program with:
* iup.lib;iupgl.lib;iupimglib.lib;comctl32.lib;opengl32.lib;glu32.lib;
*
* You can find iup libs (iup.lib;iupgl.lib;iupimglib.lib;) at:
* http://www.tecgraf.puc-rio.br/iup/
* or at:
* http://www.tecgraf.puc-rio.br/~mgattass/tec/iup32vc8.zip
* the others lib (comctl32.lib;opengl32.lib;glu32.lib;) are inluded in the Visual Studio
*
* Do not forget to put the path for the include and lib in the project file.
* Here we use: c:\tec\inc and c:\tec\lib
*
* Ignore: libcmt.lib;
*
* Last modification: Marcelo Gattass, 02ago2010,10h.
*
**/
/*- Include lib interfaces: ANSI C, IUP and OpenGL ------*/
#include <stdio.h>
#include <stdlib.h>
#include <iup.h> /* IUP functions*/
#include <iupgl.h> /* IUP functions related to OpenGL (IupGLCanvasOpen,IupGLMakeCurrent and IupGLSwapBuffers) */
#include "image.h"
#ifdef WIN32
#include <windows.h> /* includes only in MSWindows not in UNIX */
#include <gl/gl.h> /* OpenGL functions*/
#include <gl/glu.h> /* OpenGL utilitary functions*/
#else
#include <GL/gl.h> /* OpenGL functions*/
#include <GL/glu.h> /* OpenGL utilitary functions*/
#endif
/*- Program context: -------------------------------------------------*/
static Image* cur_img;
static Image* orig_img;
static Image* myeffect_img;
static Image* sobel_img;
static Image* high_img;
static Image* grey_img;
static Image* gauss_img;
static Image* median_img;
static Image* reduce_img;
static Image* otsu_img;
static Image* ohbuchi_img;
static Ihandle* dialog;
static Ihandle *canvas; /* canvas handle */
static Ihandle *msgbar; /* message bar handle */
static int width=640,height=480; /* width and height of the canvas */
int param_action(Ihandle *_dialog, int param_index, void *user_data)
{
return IUP_DEFAULT; /* returns the control to the main loop */
}
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
Image *do_highlight(Image *orig, Image *sobel)
{
int x,y;
float th = 0;
float ro,go,bo;
float rs,gs,bs;
Image *high;
high = imgCopy(orig);
if (!IupGetParam("set threshold", param_action, 0,
"threshold: %r\n", &th, NULL))
th = 0.1;
for(y=0; y<imgGetHeight(orig); y++){
for(x=0; x<imgGetWidth(orig); x++){
imgGetPixel3f(orig, x, y, &ro, &go, &bo);
imgGetPixel3f(sobel, x, y, &rs, &gs, &bs);
rs = ro - th*rs;
gs = go - th*gs;
bs = bo - th*bs;
/* clipping img */
if (rs < 0) rs = 0; if (rs > 1) rs = 1;
if (gs < 0) gs = 0; if (gs > 1) rs = 1;
if (bs < 0) bs = 0; if (bs > 1) rs = 1;
imgSetPixel3f(high, x, y, rs, gs, bs);
}
}
return high;
}
Image *do_myeffect(Image *orig)
{
int x,y,z;
Image *high;
high = imgCopy(orig);
int remap[][2] = {
{-1, -1}, { 0, -1}, { 1, -1},
{-1, 0}, { 0, 0}, { 1, 0},
{-1, 1}, { 0, 1}, { 1, 1},
};
for(y=1; y<imgGetHeight(orig)-1; y+=3){
for(x=1; x<imgGetWidth(orig)-1; x+=3){
float ro = 0,go = 0,bo = 0;
float rs = 0,gs = 0,bs = 0;
for(z=0; z<ARRAY_SIZE(remap); z++){
imgGetPixel3f(orig, x+remap[z][0],
y+remap[z][1],
&ro, &go, &bo);
rs += ro;
gs += go;
bs += bo;
}
rs = rs/ARRAY_SIZE(remap);
gs = gs/ARRAY_SIZE(remap);
bs = bs/ARRAY_SIZE(remap);
for(z=0; z<ARRAY_SIZE(remap); z++)
imgSetPixel3f(high, x+remap[z][0],
y+remap[z][1],
rs, gs, bs);
}
}
return high;
}
void update_dialog_size(Ihandle* _dialog, Ihandle* _canvas, int w, int h )
{
char buffer[64];
sprintf(buffer,"%dx%d",w,h);
IupSetAttribute(_canvas, IUP_RASTERSIZE, buffer);
IupSetAttribute(_dialog, IUP_RASTERSIZE, NULL);
IupShowXY(_dialog, IUP_CENTER, IUP_CENTER);
}
char * get_file_name( void )
{
Ihandle* getfile = IupFileDlg();
char* filename = NULL;
IupSetAttribute(getfile, IUP_TITLE, "Abertura de arquivo" );
IupSetAttribute(getfile, IUP_DIALOGTYPE, IUP_OPEN);
IupSetAttribute(getfile, IUP_FILTER, "*.bmp");
IupSetAttribute(getfile, IUP_FILTERINFO, "Arquivo de imagem (*.bmp)");
IupPopup(getfile, IUP_CENTER, IUP_CENTER);
filename = IupGetAttribute(getfile, IUP_VALUE);
return filename;
}
char* get_new_file_name( void )
{
Ihandle* getfile = IupFileDlg();
char* filename = NULL;
IupSetAttribute(getfile, IUP_TITLE, "Salva arquivo" );
IupSetAttribute(getfile, IUP_DIALOGTYPE, IUP_SAVE);
IupSetAttribute(getfile, IUP_FILTER, "*.bmp");
IupSetAttribute(getfile, IUP_FILTERINFO, "Arquivo de imagem (*.bmp)");
IupPopup(getfile, IUP_CENTER, IUP_CENTER);
filename = IupGetAttribute(getfile, IUP_VALUE);
return filename;
}
/*------------------------------------------*/
/* IUP Callbacks */
/*------------------------------------------*/
/* function called when the canvas is exposed in the screen */
int repaint_cb(Ihandle *self)
{
int x,y;
IupGLMakeCurrent(self);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); /* black */
glClear(GL_COLOR_BUFFER_BIT); /* clear the color buffer */
/* assing to each pixel of the canvas a red-green color in a given blue value (global variable) */
if (!cur_img) return IUP_DEFAULT;
glBegin(GL_POINTS);
for (y=0; y < imgGetHeight(cur_img); y++) {
for (x=0; x < imgGetWidth(cur_img); x++) {
float r,g,b;
imgGetPixel3f(cur_img, x, y, &r, &g, &b);
glColor3f(r,g,b); /* define a current color */
glVertex2i(x,y); /* paint a point in the position (x,y,0) */
}
}
glEnd();
IupGLSwapBuffers(self); /* change the back buffer with the front buffer */
return IUP_DEFAULT; /* returns the control to the main loop */
}
/* function called in the event of changes in the width or in the height of the canvas */
int resize_cb(Ihandle *self, int new_width, int new_height)
{
IupGLMakeCurrent(self); /* Make the canvas current in OpenGL */
/* define the entire canvas as the viewport */
glViewport(0,0,new_width,new_height);
/* transformation applied to each vertex */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); /* identity, i. e. no transformation */
/* projection transformation (orthographic in the xy plane) */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0, (GLsizei)(new_width), 0.0, (GLsizei)(new_height)); /* window of interest [0,w]x[0,h] */
/* update canvas size and repaint */
width=new_width;
height=new_height;
repaint_cb(canvas);
return IUP_DEFAULT; /* return to the IUP main loop */
}
int open_file_cb(void)
{
char *fname = get_file_name();
if (!fname) /*TODO show dialog de erro. */
printf ("invalid file name: %s\n", fname);
cur_img = orig_img = imgReadBMP(fname);
update_dialog_size(dialog, canvas, imgGetWidth(orig_img), imgGetHeight(orig_img));
sobel_img = imgEdges(orig_img);
myeffect_img = do_myeffect(orig_img);
grey_img = imgGrey(orig_img);
gauss_img = imgCopy(orig_img);
imgGauss(gauss_img, orig_img);
median_img = imgCopy(orig_img);
imgMedian(median_img);
reduce_img = imgCopy(orig_img);
imgReduceColors(orig_img, reduce_img, 255);
otsu_img = imgBinOtsu(orig_img);
ohbuchi_img = imgBinOhbuchi(orig_img);
resize_cb(canvas, imgGetWidth(cur_img), imgGetHeight(cur_img));
repaint_cb(canvas);
return IUP_DEFAULT;
}
int save_file_cb(void)
{
char *fname = get_new_file_name();
if (!fname) /*TODO show dialog de erro. */
printf ("invalid file name: %s\n", fname);
imgWriteBMP(fname, cur_img);
return IUP_DEFAULT;
}
int show_orig_cb(Ihandle *ih, int state)
{
cur_img = orig_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int highlight_cb(Ihandle *ih, int state)
{
imgDestroy(high_img);
high_img = do_highlight(orig_img, sobel_img);
cur_img = high_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int sobel_cb(Ihandle *ih, int state)
{
cur_img = sobel_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int myeffect_cb(Ihandle *ih, int state)
{
cur_img = myeffect_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int grey_cb(Ihandle *ih, int state)
{
cur_img = grey_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int gauss_cb(Ihandle *ih, int state)
{
cur_img = gauss_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int median_cb(Ihandle *ih, int state)
{
cur_img = median_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int reduce_cb(Ihandle *ih, int state)
{
int num;
imgDestroy(reduce_img);
if (!IupGetParam("set number of colors", param_action, 0,
"Number of colors: %i\n", &num, NULL))
num = 255;
reduce_img = imgCopy(orig_img);
imgReduceColors(orig_img, reduce_img, num);
cur_img = reduce_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int otsu_cb(Ihandle *ih, int state)
{
cur_img = otsu_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int ohbuchi_cb(Ihandle *ih, int state)
{
cur_img = ohbuchi_img;
repaint_cb(canvas);
return IUP_DEFAULT;
}
int motion_cb(Ihandle *self, int xm, int ym, char *status){
int x=xm;
int y=height-ym;
IupSetfAttribute(msgbar, "TITLE", "Motion: x = %d, y=%d",x,y);
return IUP_DEFAULT;
}
int button_cb(Ihandle* self, int button, int pressed, int xm, int ym, char* status){
int x=xm;
int y=height-ym;
IupSetfAttribute(msgbar, "TITLE", "pressed=%d, x=%d, y=%d, status=%s",
pressed, x, y, status);
return IUP_DEFAULT;
}
int exit_cb(void)
{
printf("Function to free memory and do finalizations...\n");
imgDestroy(sobel_img);
sobel_img = cur_img = NULL;
return IUP_CLOSE;
}
/*-------------------------------------------------------------------------*/
/* Incializa o programa. */
/*-------------------------------------------------------------------------*/
Ihandle* InitToolbar(void)
{
Ihandle* toolbar;
/* Create four buttons */
Ihandle* hopen_file = IupButton("Open", "open_file_action");
Ihandle* hsave_file = IupButton("Save", "save_file_action");
Ihandle* hhighlight_img = IupButton("Highlight", "highlight_img_action");
Ihandle* horig_img = IupButton("Original", "orig_img_action");
Ihandle* hsobel_img = IupButton("Sobel", "sobel_img_action");
Ihandle* hmyeffect_img = IupButton("Pixelize", "myeffect_img_action");
Ihandle* hgrey_img = IupButton("Grey", "grey_img_action");
Ihandle* hgauss_img = IupButton("Gauss", "gauss_img_action");
Ihandle* hmedian_img = IupButton("Median", "median_img_action");
Ihandle* hreduce_img = IupButton("Reduce", "reduce_img_action");
Ihandle* hotsu_img = IupButton("Otsu", "otsu_img_action");
Ihandle* hohbuchi_img = IupButton("Ohbuchi", "ohbuchi_img_action");
/* Associate images with this buttons */
IupSetAttribute(hopen_file,"IMAGE","IUP_FileOpen");
IupSetAttribute(hsave_file,"IMAGE","IUP_FileSave");
/* Associate tip's (text that appear when the mouse is over) */
IupSetAttribute(hopen_file,"TIP","go to open_file");
/* Associate function callbacks to the button actions */
IupSetFunction("open_file_action", (Icallback)open_file_cb);
IupSetFunction("save_file_action", (Icallback)save_file_cb);
IupSetFunction("highlight_img_action", (Icallback)highlight_cb);
IupSetFunction("orig_img_action", (Icallback)show_orig_cb);
IupSetFunction("sobel_img_action", (Icallback)sobel_cb);
IupSetFunction("myeffect_img_action", (Icallback)myeffect_cb);
IupSetFunction("grey_img_action", (Icallback)grey_cb);
IupSetFunction("gauss_img_action", (Icallback)gauss_cb);
IupSetFunction("median_img_action", (Icallback)median_cb);
IupSetFunction("reduce_img_action", (Icallback)reduce_cb);
IupSetFunction("otsu_img_action", (Icallback)otsu_cb);
IupSetFunction("ohbuchi_img_action", (Icallback)ohbuchi_cb);
toolbar=IupHbox(hopen_file, hsave_file, horig_img, hhighlight_img,
hsobel_img, hmyeffect_img, hgrey_img, hgauss_img,
hmedian_img, hreduce_img, hotsu_img,
hohbuchi_img, IupFill(),NULL);
return toolbar;
}
/* Create a IUP canvas */
Ihandle* InitCanvas(void)
{
Ihandle* _canvas = IupGLCanvas("repaint_cb"); /* create _canvas and define its repaint callback */
IupSetAttribute(_canvas,IUP_RASTERSIZE,"640x480"); /* define the size in pixels */
IupSetAttribute(_canvas,IUP_BUFFER,IUP_DOUBLE); /* define that this OpenGL _canvas has double buffer (front and back) */
IupSetAttribute(_canvas, "RESIZE_CB", "resize_cb"); /* define callback action associate with the change in size of the _canvas */
IupSetAttribute(_canvas, "BUTTON_CB","button_cb");
IupSetAttribute(_canvas, "MOTION_CB","motion_cb");
/* bind callback actions with callback functions */
IupSetFunction("repaint_cb", (Icallback) repaint_cb);
IupSetFunction("resize_cb", (Icallback) resize_cb);
IupSetFunction("button_cb",(Icallback) button_cb);
IupSetFunction("motion_cb",(Icallback) motion_cb);
return _canvas;
}
Ihandle* InitDialog(void)
{
Ihandle* _dialog; /* dialog containing the canvas */
Ihandle* toolbar=InitToolbar(); /* buttons tool bar */
canvas = InitCanvas(); /* canvas to paint with OpenGL */
msgbar = IupLabel("A msg bar"); /* a msg bar */
IupSetAttribute(msgbar,IUP_RASTERSIZE,"640x20"); /* define the size in pixels */
/* create the dialog and set its attributes */
_dialog = IupDialog(IupVbox(toolbar,canvas,msgbar,NULL));
IupSetAttribute(_dialog, "TITLE", "T1 - Marcelo e Peter");
IupSetAttribute(_dialog, "CLOSE_CB", "exit_cb");
IupSetFunction("exit_cb", (Icallback) exit_cb);
return _dialog;
}
/*-----------------------*/
/* Main function. */
/*-----------------------*/
int main(int argc, char* argv[])
{
IupOpen(&argc, &argv);
IupImageLibOpen();
IupGLCanvasOpen();
dialog = InitDialog();
IupShowXY(dialog, IUP_CENTER, IUP_CENTER);
IupMainLoop();
IupClose();
return 0;
}