-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
147 lines (118 loc) · 2.91 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <signal.h>
#include <errno.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define WINDOW_WIDTH (800)
#define WINDOW_HEIGHT (600)
#define FRAME_RATE (30)
#define FRAME_BUFFER_SIZE (4 * WINDOW_WIDTH * WINDOW_HEIGHT)
static SDL_Window *window;
static SDL_Surface *window_surface;
static uint8_t *frame_buffer;
static size_t frame_buffer_size;
void handle_signal()
{
fprintf(stdout, "\nInterrupted! Exiting program..\n");
if (window) {
SDL_DestroyWindow(window);
}
if (SDL_WasInit(SDL_INIT_VIDEO)) {
SDL_Quit();
}
exit(0);
}
void draw()
{
SDL_RWops *rw;
SDL_Surface *surface;
rw = SDL_RWFromMem(frame_buffer, frame_buffer_size);
if (!rw) {
goto out;
}
surface = IMG_Load_RW(rw, 1);
if (!surface) {
goto out;
}
if (SDL_BlitSurface(surface, NULL, window_surface, NULL) < 0) {
goto out;
}
if (SDL_UpdateWindowSurface(window) < 0) {
goto out;
}
SDL_FreeSurface(surface);
return;
out:
fprintf(stderr, "%s\n", SDL_GetError());
}
size_t read_jpeg(FILE *file)
{
bool started = false;
size_t index = 0;
for (;;) {
const int c1 = fgetc(file);
const int c2 = fgetc(file);
if (started) {
frame_buffer[index++] = c1;
frame_buffer[index++] = c2;
const uint16_t code =((c1 << 8) | c2);
if (code == 0xffd9) {
break;
}
} else {
const uint16_t code =((c1 << 8) | c2);
if (code == 0xffd8) {
frame_buffer[index++] = c1;
frame_buffer[index++] = c2;
started = true;
}
}
}
return index;
}
int main(int argc, char **argv)
{
FILE *file = stdin;
signal(SIGINT, handle_signal);
if (argc > 1) {
file = fopen(argv[1], "r+");
if (!file) {
fprintf(stderr, "%s\n", strerror(errno));
return -1;
}
}
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"gPhoto2 LiveView Example",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN
);
if (!window) {
fprintf(stderr, "%s\n", SDL_GetError());
return -1;
}
window_surface = SDL_GetWindowSurface(window);
frame_buffer = (uint8_t *) malloc(FRAME_BUFFER_SIZE);
bool running = true;
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
}
}
frame_buffer_size = read_jpeg(file);
draw();
SDL_Delay(1000 / FRAME_RATE);
}
SDL_DestroyWindow(window);
SDL_Quit();
}