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
|
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
typedef struct {
int x;
int y;
int r;
int g;
int b;
} Pixel;
void show_pixels(Pixel *pixels, int len) {
for (int i = 0; i < len; i++) {
Pixel p = pixels[i];
printf("%d %d %d\n", p.r, p.g, p.b);
}
}
void show_image(Pixel *pixels, int len) {
printf("P3\n");
printf("%d %d\n", 6, 6);
printf("%d\n", 255);
show_pixels(pixels, len);
}
int main(void) {
sqlite3 *db;
sqlite3_open("fun.db", &db);
if (db == NULL) {
printf("Unable to open db\n");
return 1;
}
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db,
"select x, y, r, g, b from pixels ORDER BY y ASC, x ASC",
-1, &stmt, NULL);
Pixel pixels[200 * 200];
int index = 0;
while (sqlite3_step(stmt) != SQLITE_DONE) {
int num_cols = sqlite3_column_count(stmt);
Pixel pixel;
for (int col = 0; col < num_cols; col++) {
const char *name = sqlite3_column_name(stmt, col);
if (strcmp(name, "x") == 0)
pixel.x = sqlite3_column_int(stmt, col);
else if (strcmp(name, "y") == 0)
pixel.y = sqlite3_column_int(stmt, col);
else if (strcmp(name, "r") == 0)
pixel.r = sqlite3_column_int(stmt, col);
else if (strcmp(name, "g") == 0)
pixel.g = sqlite3_column_int(stmt, col);
else if (strcmp(name, "b") == 0)
pixel.b = sqlite3_column_int(stmt, col);
}
pixels[index++] = pixel;
}
int frames = 1;
for (int i = 0; i < frames; i++)
show_image(pixels, index);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
|