-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpxl.c
480 lines (387 loc) · 8.91 KB
/
pxl.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
/*
pxl - ppm image viewer
Written in 2012 by <Olga Miller> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "utils.h"
#include "image.h"
#include "reader.h"
char* icon = "pixelka"; //TODO: create icon
SDL_Surface* screen;
int scale;
int grid;
int args_num;
char** args;
struct image img;
char* filename;
int fb_dirty;
int last_cell_x;
int last_cell_y;
int offset_x;
int offset_y;
time_t last_mtime;
void exiterr(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, fmt, args);
fprintf(stderr, "%s\n", SDL_GetError());
exit(1);
}
void set_default_caption()
{
char c[100];
snprintf(c, 100, "%s zoom=%d", filename, scale);
SDL_WM_SetCaption(c, icon);
}
void set_caption(int x, int y, int r, int g, int b)
{
char c[100];
snprintf(c, 100, "%s zoom=%d [%d x %d] (%d; %d; %d)", filename, scale, x, y, r, g, b);
SDL_WM_SetCaption(c, icon);
}
void resize_video(int w, int h)
{
if(!screen || (screen->w != w || screen->h != h))
{
if(screen)
SDL_FreeSurface(screen);
screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
if(!screen)
exiterr("Unable to set video.\n");
if(screen->format->BitsPerPixel != 32)
exiterr("Could not init 32 bit format.\n");
if(screen->w != w || screen->h != h)
exiterr("Could not get %dx%d window.\n", w, h);
}
set_default_caption();
}
void set_pixel(int x, int y, uint32_t* fb, uint32_t rgb)
{
int i = y * screen->w + x;
if((0 <= x && x < screen->w) && (0 <= y && y < screen->h))
fb[i] = rgb;
}
void draw_tile(int size, int x0, int y0, uint32_t color)
{
uint32_t* fb0 = (uint32_t*)screen->pixels + x0 + screen->w * y0;
for(int y = 0; y < size; y++, fb0 += screen->w)
for(int x = 0; x < size; x++)
fb0[x] = color;
}
void draw_partial(int size, int x0, int y0, uint32_t color)
{
uint32_t* fb = (uint32_t*)screen->pixels;
int w = screen->w;
int h = screen->h;
for(int y = y0; y < (y0 + size); y++)
for(int x = x0; x < (x0 + size); x++)
if((0 <= x && x < w) && (0 <= y && y < h))
fb[y * w + x] = color;
}
void draw()
{
int step = scale + grid;
SDL_FillRect(screen, 0, 0);
// calc interval (a, b) for whole tiles in image coordinates
int xa = (max(-offset_x, 0) + (step - 1)) / step;
int ya = (max(-offset_y, 0) + (step - 1)) / step;
int xb = (max(-offset_x, 0) + min(img.w * step, screen->w)) / step;
int yb = (max(-offset_y, 0) + min(img.h * step, screen->h)) / step;
uint32_t* pa = img.pixels + ya * img.w;
for(int y = ya, y0 = ya * step + offset_y + grid; y < yb; y++, y0 += step, pa += img.w)
for(int x = xa, x0 = xa * step + offset_x + grid; x < xb; x++, x0 += step)
draw_tile(scale, x0, y0, pa[x]);
// calc interval (c, d) for partial tiles in image coordinates
int xc = max(-offset_x, 0) / step;
int yc = max(-offset_y, 0) / step;
int xd = (max(-offset_x, 0) + min(img.w * step, screen->w) + (step - 1)) / step;
int yd = (max(-offset_y, 0) + min(img.h * step, screen->h) + (step - 1)) / step;
uint32_t* pc = img.pixels + yc * img.w;
for(int y = yc, y0 = yc * step + offset_y + grid; y < yd; y++, y0 += step, pc += img.w)
{
for(int x = xc, x0 = xc * step + offset_x + grid; x < xd; x++, x0 += step)
{
if((x >= xa) && (x < xb) && (y >= ya) && (y < yb))
{
x = xb - 1;
x0 = (xb - 1) * step + offset_x + grid;
continue;
}
draw_partial(scale, x0, y0, pc[x]);
}
}
fb_dirty = 1;
}
void set_filename(int direction)
{
static int curr_arg = -1;
if(direction != 1 && direction != -1)
direction = 1;
curr_arg = (curr_arg + args_num + direction) % args_num;
filename = args[curr_arg + 1];
}
void read_image(int direction)
{
int i = 0;
struct stat sb;
set_filename(direction);
while(stat(filename, &sb) == -1 || !read_ppm_P6(filename, &img))
{
set_filename(direction);
if(i == args_num)
exiterr("No file is readable.\n");
i++;
}
last_mtime = sb.st_mtime;
}
void draw_grid_cell(uint32_t rgb)
{
uint32_t* fb = (uint32_t*) screen->pixels;
int jump = scale + 1;
int line = scale + 2;
for(int i = 0; i < line; i++)
{
int x = last_cell_x + i;
int y = last_cell_y;
set_pixel(x, y, fb, rgb);
set_pixel(x, y + jump, fb, rgb);
x = last_cell_x;
y = last_cell_y + i;
set_pixel(x, y, fb, rgb);
set_pixel(x + jump, y, fb, rgb);
}
fb_dirty = 1;
}
void change(int mouse_x, int mouse_y)
{
int step = scale + grid;
static int old_scale = 1;
int x = (mouse_x - offset_x - grid) / step;
int y = (mouse_y - offset_y - grid) / step;
SDL_ShowCursor(grid ^ 1);
if(grid && old_scale == scale)
draw_grid_cell(0);
old_scale = scale;
if((0 <= x && x < img.w) && (0 <= y && y < img.h))
{
if(grid)
{
last_cell_x = x * step + offset_x;
last_cell_y = y * step + offset_y;
draw_grid_cell(0x00dddddd);
}
uint32_t p = img.pixels[y * img.w + x];
set_caption(x, y, (p >> 16) & 255, (p >> 8) & 255, p & 255);
}
else
{
SDL_ShowCursor(1);
set_default_caption();
}
}
void set_offset(int new_x, int new_y)
{
int old_x = offset_x;
int old_y = offset_y;
int max_x = screen->w - (img.w * (scale + grid) + grid);
int max_y = screen->h - (img.h * (scale + grid) + grid);
if(max_x >= 0)
offset_x = max_x / 2;
else
offset_x = min(max(new_x, max_x), 0);
if(max_y >= 0)
offset_y = max_y / 2;
else
offset_y = min(max(new_y, max_y), 0);
last_cell_x += offset_x - old_x;
last_cell_y += offset_y - old_y;
}
void redraw()
{
set_offset(offset_x, offset_y);
set_default_caption();
draw();
}
void jump(int xrel, int yrel)
{
int xstep = xrel * screen->w / 2;
int ystep = yrel * screen->h / 2;
set_offset(offset_x + xstep, offset_y + ystep);
draw();
}
void handle_keydown(SDL_KeyboardEvent* event)
{
SDLKey key = event->keysym.sym;
switch(key)
{
case SDLK_LEFT:
jump(1, 0);
break;
case SDLK_UP:
jump(0, 1);
break;
case SDLK_RIGHT:
jump(-1, 0);
break;
case SDLK_DOWN:
jump(0, -1);
break;
case SDLK_g:
grid ^= 1;
redraw();
break;
case SDLK_r:
read_ppm_P6(filename, &img);
redraw();
break;
case SDLK_SPACE:
read_image(1);
redraw();
break;
case SDLK_BACKSPACE:
read_image(-1);
redraw();
break;
case SDLK_q:
case SDLK_ESCAPE:
exit(0);
break;
default:
if(SDLK_0 <= key && key <= SDLK_9)
{
scale = 1 << (key - SDLK_0);
redraw();
}
else if(SDLK_KP0 <= key && key <= SDLK_KP9)
{
scale = 1 << (key - SDLK_KP0);
redraw();
}
break;
}
}
void handle_event()
{
SDL_Event event;
static int mousebuttonleft_down = 0;
int mouse_x = -1;
int mouse_y = -1;
int mouse_xrel = 0;
int mouse_yrel = 0;
int w = 0;
int h = 0;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_MOUSEMOTION:
mouse_x = event.motion.x;
mouse_y = event.motion.y;
mouse_xrel += event.motion.xrel;
mouse_yrel += event.motion.yrel;
break;
case SDL_KEYDOWN:
handle_keydown(&event.key);
break;
case SDL_MOUSEBUTTONDOWN:
if(event.button.button == SDL_BUTTON_LEFT)
mousebuttonleft_down = 1;
break;
case SDL_MOUSEBUTTONUP:
if(event.button.button == SDL_BUTTON_LEFT)
mousebuttonleft_down = 0;
break;
case SDL_VIDEORESIZE:
w = event.resize.w;
h = event.resize.h;
break;
case SDL_QUIT:
exit(0);
break;
default:
break;
}
}
if(w && h)
{
resize_video(w, h);
redraw();
}
else
{
if(mousebuttonleft_down && (mouse_xrel || mouse_yrel))
{
set_offset(offset_x + mouse_xrel, offset_y + mouse_yrel);
draw();
}
if(mouse_x != -1 && mouse_y != -1)
change(mouse_x, mouse_y);
}
}
int file_changed()
{
struct stat sb;
if(stat(filename, &sb) == -1 || last_mtime == sb.st_mtime)
return 0;
last_mtime = sb.st_mtime;
return 1;
}
void reload_file_if_changed()
{
static uint32_t last_ticks = 0;
uint32_t ticks = SDL_GetTicks();
if((ticks - last_ticks) > 1000)
{
last_ticks = ticks;
if(file_changed())
{
read_ppm_P6(filename, &img);
redraw();
}
}
}
int main(int argc, char** argv)
{
if(argc <= 1)
exiterr("No args.\n");
screen = 0;
img.pixels = 0;
scale = 1;
grid = 0;
last_cell_x = 0;
last_cell_y = 0;
offset_x = 0;
offset_y = 0;
fb_dirty = 0;
args_num = argc - 1;
args = argv;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
exiterr("SDL can not be initialized.\n");
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
read_image(1);
resize_video(640, 480);
set_offset(0, 0);
draw();
for(;;)
{
reload_file_if_changed();
handle_event();
if(fb_dirty)
{
SDL_Flip(screen);
fb_dirty = 0;
}
SDL_Delay(10);
}
return 0;
}