-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw1.cpp
More file actions
executable file
·560 lines (505 loc) · 14.6 KB
/
hw1.cpp
File metadata and controls
executable file
·560 lines (505 loc) · 14.6 KB
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//Program: Homework 1
//Author: Bryant Dinh
//cs371 Fall 2014
//Purpose: To learn and apply the Floyd Steinberg algorithm on images which demonstrate knowledge on RGB color spectrum and process of dithering. Rotation to test knowledge on pixel location, knowing
//where to move a pixel of an image to rotate the whole image. In a way it tests our knowledge of 2D arrays even though I don't use them, I used a single data stream and used pointer arithmetic to dither
//image.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <math.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include "log.h"
#include "ppm.h"
//-------------------------------------------
//XWindows globals to make your life easier.
Display *dpy;
Window win;
GC gc;
//---------------------------
void init();
void init_xwindows();
void cleanup_xwindows();
void check_resize(XEvent *e);
void check_mouse(XEvent *e);
int check_keys(XEvent *e);
void render();
//
#define MAX_IMAGES 9
//a global structure
struct G {
int xres, yres;
int draw;
int imageNum;
int grayscale;
int dither;
int rgbDither;
int rotate;
int menuWidth;
char imageName[MAX_IMAGES][64];
int nimages;
} g;
int main()
{
int done=0;
logOpen();
init();
init_xwindows();
while(!done) {
//Check the event queue
while(XPending(dpy)) {
XEvent e;
XNextEvent(dpy, &e);
check_resize(&e);
check_mouse(&e);
done = check_keys(&e);
render();
}
}
cleanup_xwindows();
logClose();
return 0;
}
void init()
{
//Get a list of all .ppm files in current directory.
struct dirent *entry;
DIR *dp = opendir(".");
g.nimages = 0;
if (dp) {
while ((entry = readdir(dp)) != NULL
&& g.nimages < MAX_IMAGES ) {
if (strstr(entry->d_name, ".ppm"))
strcpy(g.imageName[g.nimages++], entry->d_name);
}
closedir(dp);
}
//initialize variables
g.xres=640;
g.yres=480;
g.draw=0;
g.imageNum=0;
g.grayscale=0;
g.dither=0;
g.rgbDither=0;
g.rotate=0;
g.menuWidth=160;
}
void cleanup_xwindows()
{
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void init_xwindows()
{
int scr;
if(!(dpy=XOpenDisplay(NULL))) {
fprintf(stderr, "ERROR: could not open display\n");
exit(EXIT_FAILURE);
}
scr = DefaultScreen(dpy);
win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 1, 1,
g.xres, g.yres, 0, 0x00ffffff, 0x00000000);
gc = XCreateGC(dpy, win, 0, NULL);
XMapWindow(dpy, win);
XSelectInput(dpy, win, ExposureMask | StructureNotifyMask |
PointerMotionMask | ButtonPressMask |
ButtonReleaseMask | KeyPressMask);
XStoreName(dpy, win, "CS371 - Lab1 Student Selfie Editor");
}
void check_resize(XEvent *e)
{
//ConfigureNotify is sent when window size changes.
if (e->type != ConfigureNotify)
return;
XConfigureEvent xce = e->xconfigure;
g.xres = xce.width;
g.yres = xce.height;
}
void resize_window(int w, int h)
{
g.xres = w;
g.yres = h;
unsigned int value_mask=0;
value_mask |= CWWidth;
value_mask |= CWHeight;
XWindowChanges xwc;
xwc.width = w;
xwc.height = h;
XConfigureWindow(dpy, win, value_mask, &xwc);
}
void check_image_fit(int width, int height)
{
//make sure image fits in window.
if (width+g.menuWidth > g.xres || height > g.yres) {
resize_window(width+g.menuWidth, height);
}
}
void clear_screen()
{
XClearWindow(dpy, win);
}
void check_mouse(XEvent *e)
{
static int savex = 0;
static int savey = 0;
//
int mx = e->xbutton.x;
int my = e->xbutton.y;
//
if (e->type == ButtonRelease) {
return;
}
if (e->type == ButtonPress) {
//Log("ButtonPress %i %i\n", e->xbutton.x, e->xbutton.y);
if (e->xbutton.button==1) {
//Left button pressed
}
if (e->xbutton.button==3) {
//Right button pressed
}
}
if (savex != mx || savey != my) {
//mouse moved
savex = mx;
savey = my;
}
}
int check_keys(XEvent *e)
{
int key = XLookupKeysym(&e->xkey, 0);
if (e->type != KeyPress)
return 0;
if (g.nimages <= 0)
return 1;
//a key was pressed
if (key >= XK_1 && key <= XK_9) {
g.imageNum = key - XK_1;
g.imageNum = (g.imageNum > g.nimages) ? g.nimages : g.imageNum;
g.draw=1;
}
switch (key) {
case XK_r:
g.rgbDither ^= 1;
break;
case XK_g:
g.grayscale ^= 1;
break;
case XK_d:
g.dither ^= 1;
if (g.dither && !g.grayscale)
g.grayscale=1;
break;
case XK_z:
if (g.rotate >= 3) {
g.rotate = 0;
}
else {
g.rotate++;
}
break;
case XK_equal:
case XK_minus:
break;
case XK_Delete:
break;
case XK_Escape:
//quitting the program
return 1;
}
//clear_screen();
g.draw=1;
return 0;
}
inline void set_color_3i(int r, int g, int b)
{
unsigned long cref = 0L;
cref += r;
cref <<= 8;
cref += g;
cref <<= 8;
cref += b;
XSetForeground(dpy, gc, cref);
}
void show_menu()
{
int i,x=10, y=12;
char ts[64];
set_color_3i(10,10,10);
XFillRectangle(dpy, win, gc, 0, 0, g.menuWidth, g.yres);
if (g.nimages <= 0) {
set_color_3i(255,255,0);
sprintf(ts,"NOTICE:");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
sprintf(ts,"No image found!");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
sprintf(ts,"Put some PPM images in your directory.");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
return;
}
set_color_3i(255,255,255);
sprintf(ts,"Show Image...");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
for (i=0; i<g.nimages; i++) {
sprintf(ts,"%i - %s",i+1,g.imageName[i]);
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
}
y += 16;
sprintf(ts,(g.grayscale)?"G - Gray Scale (ON)":"G - Gray Scale (OFF)");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
sprintf(ts,(g.dither)?"D - Dither (ON)":"D - Dither (OFF)");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
sprintf(ts,(g.rgbDither)?"R - RGB Dither (ON)":"R - RGB Dither (OFF)");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
if (g.rotate == 0) sprintf(ts,"Z - Rotate 0 degrees");
if (g.rotate == 1) sprintf(ts,"Z - Rotate 90 degrees");
if (g.rotate == 2) sprintf(ts,"Z - Rotate 180 degrees");
if (g.rotate == 3) sprintf(ts,"Z - Rotate 270 degrees");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
sprintf(ts,"Esc - Quit");
XDrawString(dpy, win, gc, x, y, ts, strlen(ts));
y += 16;
//vertical line
//set_color_3i(200,200,200);
//XDrawLine(dpy, win, gc, 160, 0,160,yres);
}
void floydSteinbergDither(unsigned char *ucdata, int h, int w)
{
//Floyd Steinberg dithering
//
//Follow these steps:
//1. Create an integer array that can hold 3 colors for each pixel.
//2. Copy the color data to the integer array.
// note: We use integers because a color value could go negative or
// above 255 during the algorithm.
//3. Apply Floyd-Steinberg algorithm:
// Starting at the beginning of the integer data, apply the
// At each pixel...
// Set the pixel to black or white
// Record the change in color that happened
// Distribute this change (error) to the pixels around it
// using the following pattern:
//
// * 7/16
// 3/16 5/16 1/16
//
// This means:
// Add 7/16 of the error to the pixel to the right
// Add 3/16 of the error to the pixel to the lower-left
// Add 5/16 of the error to the pixel below
// Add 1/16 of the error to the pixel to the lower-right
//
// Check pixel coordinates to verify they are not outside the image.
//
//4. Put integer pixels back into the original data structure.
//5. Display the image on screen.
//6. Free the integer array.
int *idata = new int[w * h * 3];
int *iptr = idata;
for (int i = 0; i < w * h * 3; i++) {
idata[i] = (int)ucdata[i];
}
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
int error;
int newpixel;
int oldpixel = *iptr;
if (oldpixel > 128){
newpixel = 255;
error = oldpixel - 255;
}
else {
newpixel = 0;
error = oldpixel;
}
*(iptr + 2) = *(iptr + 1) = *(iptr) = newpixel;
if (k == 0 && j != h - 1) {
*(iptr + 3) = *(iptr + 3) + error * 7/16;
*(iptr + (w * 3) + 3) = *(iptr + (w * 3) + 3) + error * 1/16;
*(iptr + (w * 3)) = *(iptr + (w * 3)) + error * 5/16;
}
if (k == w - 1 && j != h - 1) {
*(iptr + (w * 3)) = *(iptr + (w * 3)) + error * 5/16;
*(iptr + (w * 3) - 3) = *(iptr + (w * 3) - 3) + error * 3/16;
}
if (j == h - 1 && k != w - 1) {
*(iptr + 3) = *(iptr + 3) + error * 7/16;
}
else if (j != h - 1 && k != w - 1){
*(iptr + 3) = *(iptr + 3) + error * 7/16;
*(iptr + (w * 3) - 3) = *(iptr + (w * 3) - 3) + error * 3/16;
*(iptr + (w * 3) + 3) = *(iptr + (w * 3) + 3) + error * 1/16;
*(iptr + (w * 3)) = *(iptr + (w * 3)) + error * 5/16;
}
iptr+=3;
}
}
for (int i = 0; i < w * h * 3; i++) {
ucdata[i] = (char)idata[i];
}
delete idata;
}
void do_rgb_dither(unsigned char *ucdata, int h, int w)
{
int *idata = new int[w * h *3];
int *iptr = idata;
for (int i = 0; i < w * h * 3; i++) {
idata[i] = (int)ucdata[i];
}
int RGBcount = 0;
while (RGBcount < 3) {
iptr = idata + RGBcount;
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
int error;
int newpixel;
int oldpixel = *iptr;
if (oldpixel > 128){
newpixel = 255;
error = oldpixel - 255;
}
else {
newpixel = 0;
error = oldpixel;
}
*iptr = newpixel;
if (k == 0 && j != h - 1) {
*(iptr + 3) = *(iptr + 3) + error * 7/16;
*(iptr + (w * 3) + 3) = *(iptr + (w * 3) + 3) + error * 1/16;
*(iptr + (w * 3)) = *(iptr + (w * 3)) + error * 5/16;
}
if (k == w - 1 && j != h - 1) {
*(iptr + (w * 3)) = *(iptr + (w * 3)) + error * 5/16;
*(iptr + (w * 3) - 3) = *(iptr + (w * 3) - 3) + error * 3/16;
}
if (j == h - 1 && k != w - 1) {
*(iptr + 3) = *(iptr + 3) + error * 7/16;
}
else if (j != h - 1 && k != w - 1){
*(iptr + 3) = *(iptr + 3) + error * 7/16;
*(iptr + (w * 3) - 3) = *(iptr + (w * 3) - 3) + error * 3/16;
*(iptr + (w * 3) + 3) = *(iptr + (w * 3) + 3) + error * 1/16;
*(iptr + (w * 3)) = *(iptr + (w * 3)) + error * 5/16;
}
iptr+=3;
}
}
RGBcount++;
}
for (int i = 0; i < w * h * 3; i++) {
ucdata[i] = (char)idata[i];
}
delete idata;
}
void show_image()
{
//Show image on the screen.
//
int i, j, red, green, blue, avg;
Ppmimage *image = ppm6GetImage(g.imageName[g.imageNum]);
int w = image->width;
int h = image->height;
unsigned char *p, *ptr = (unsigned char *)image->data;
//make sure image fits in window.
check_image_fit(w, h);
//
if (g.grayscale) {
//make image gray-scale now
int col;
p = ptr;
for (i=0; i<h; i++) {
for (j=0; j<w; j++) {
//
//Please add some code here...
//
//find the average of the 3 color components
//red = *(p+0)
//green = *(p+1)
//blue = *(p+2)
red = *(p+0);
green = *(p+1);
blue = *(p+2);
//avg = (red+green+blue) / 3
avg = (red+green+blue) / 3;
//Put gray color into pixel
//*(p+0) = avg;
//*(p+1) = avg;
//*(p+2) = avg;
*(p+0) = avg;
*(p+1) = avg;
*(p+2) = avg;
//increment the pointer here
p+=3;
}
}
if (g.dither) {
floydSteinbergDither(ptr, h, w);
}
}
if (g.rgbDither) {
do_rgb_dither(ptr, h, w);
}
if (g.rotate == 1) {
p = ptr;
for (int i = h; i > 0; i--) {
for (int j = 0; j < w; j++) {
set_color_3i(*p, *(p+1), *(p+2));
XDrawPoint(dpy, win, gc, g.menuWidth+i, j);
p += 3;
}
}
}
else if (g.rotate == 2) {
p = ptr;
for (int i = h; i > 0; i--) {
for (int j = w; j > 0; j--) {
set_color_3i(*p, *(p+1), *(p+2));
XDrawPoint(dpy, win, gc, g.menuWidth+j, i);
p += 3;
}
}
}
else if (g.rotate == 3) {
p = ptr;
for (int i = 0; i < h; i++) {
for (int j = w; j > 0; j--) {
set_color_3i(*p, *(p+1), *(p+2));
XDrawPoint(dpy, win, gc, g.menuWidth+i, j);
p += 3;
}
}
}
//display the image data on screen
else {
p = ptr;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
set_color_3i(*p, *(p+1), *(p+2));
XDrawPoint(dpy, win, gc, g.menuWidth+j, i);
p += 3;
}
}
}
//finish
ppm6CleanupImage(image);
g.draw=0;
}
void render()
{
if (g.draw)
show_image();
show_menu();
}