-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbms2sp.c
More file actions
121 lines (100 loc) · 3.25 KB
/
pbms2sp.c
File metadata and controls
121 lines (100 loc) · 3.25 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
//******************************************************************************
#include "display.h"
#define HAVE_BOOL 1
#include <pbm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#define SEPARATORS (DISPLAY_TILES_VERT - 1)
#define MIN_DELAY 40
#define MAX_DELAY 150
#define DELAY 70
//******************************************************************************
int main(int ac, const char *av[])
{
int display_select = 0;
bit on_value = PBM_WHITE;
int time = 0;
int ai;
for (ai = 1; ai < ac; ai++)
{
if (av[ai][0] != '-')
break;
if (av[ai][1] == 'r')
on_value = PBM_BLACK;
else if (av[ai][1] == 's')
display_select |= DISPLAY_SELECT_SP;
else if (av[ai][1] == 'x')
display_select |= DISPLAY_SELECT_GX;
else if (isdigit(av[ai][1]))
time = atoi(&av[ai][1]) * 13;
else
assert(0);
}
int frames = ac - ai;
char bits[frames][DISPLAY_HEIGHT][DISPLAY_WIDTH];
memset(bits, 0, frames * DISPLAY_HEIGHT * DISPLAY_WIDTH * sizeof(char));
int delay = DELAY;
for (int frame = 0; frame < frames; frame++)
{
int bitmap_width, bitmap_height;
FILE *fh = fopen(av[ai + frame], "r");
assert(fh);
bit** bitmap = pbm_readpbm(fh, &bitmap_width, &bitmap_height);
assert(bitmap);
fclose(fh);
float ratio_x = (float)bitmap_width / (float)DISPLAY_WIDTH;
float ratio_y = (float)bitmap_height / (float)DISPLAY_HEIGHT_VIRT;
float ratio = ratio_x > ratio_y ? ratio_x : ratio_y;
int scaled_width = (int)((float)bitmap_width / ratio + 0.5);
int scaled_height = (int)((float)bitmap_height / ratio + 0.5);
int offset_x = (DISPLAY_WIDTH - scaled_width) / 2;
int offset_y = (DISPLAY_HEIGHT_VIRT - scaled_height) / 2;
for (int display_y = offset_y; display_y < DISPLAY_HEIGHT - offset_y; display_y++)
for (int display_x = offset_x; display_x < DISPLAY_WIDTH - offset_x; display_x++)
{
int virtual_y = display_y + (display_y / DISPLAY_TILE_SIZE) * DISPLAY_SEPARATOR_WIDTH;
int bitmap_x = (int)((float)(display_x - offset_x) * ratio + 0.5);
int bitmap_y = (int)((float)(virtual_y - offset_y) * ratio + 0.5);
bits[frame][display_y][display_x] = bitmap[bitmap_y][bitmap_x] == on_value ? 1 : 0;
}
pbm_freearray(bitmap, bitmap_height);
}
display_create(display_select);
for (int frame = 0; ; frame = (frame + 1) % frames)
{
time--;
if (!time)
break;
for (int display_y = 0; display_y < DISPLAY_HEIGHT; display_y++)
for (int display_x = 0; display_x < DISPLAY_WIDTH; display_x++)
display_set(display_x, display_y, bits[frame][display_y][display_x]);
display_flush();
int button = display_button();
if (button == 3) // right
break;
if (button == 1) // left
display_wait();
else if (button == 2) // center
delay = DELAY;
else if (button == 4) // up
{
int new = (int)((float)delay * 0.91);
if (new >= MIN_DELAY)
delay = new;
}
else if (button == 5) // down
{
int new = (int)((float)delay * 1.1);
if (new <= MAX_DELAY)
delay = new;
}
usleep(delay * 1000);
}
display_free();
return EXIT_SUCCESS;
}
//******************************************************************************