-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
121 lines (87 loc) · 2.52 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
#include <stdio.h>
#include <string.h>
#include "raylib.h"
#define QOI_IMPLEMENTATION
#include "qoi.h"
int check_qoi_get_channels(const char* filepath)
{
FILE *file = fopen(filepath, "r");
if (file == NULL)
{
printf("ERROR: Couldn't read file %s\n", filepath);
exit(1);
}
char *magic = malloc(sizeof(char) * 4);
fseek(file, 0, SEEK_SET);
fread(magic, sizeof(char), 4, file);
if(strcmp(magic, "qoif") != 0) {
printf("ERROR: Given file isn't a valid QOI image.\n");
exit(1);
}
free(magic);
char *channels = malloc(sizeof(char));
fseek(file, 12, SEEK_SET);
fread(channels, sizeof(char), 1, file);
int ret = atoi(channels);
free(channels);
fclose(file);
return ret;
}
int main(int argc, char **argv)
{
SetTraceLogLevel(LOG_ERROR);
(void)argc;
(void)*argv++;
if (*argv == NULL)
{
printf("USAGE: ./qoi-viewer <input>.qoi\n");
printf(" optional: ./qoi-viewer <input>.qoi [hexcolor: RGB]\n");
exit(1);
}
const char *filepath = *argv++;
Color back_color = RAYWHITE;
if (*argv != NULL)
{
__uint32_t val = (__uint32_t)strtoul(*argv, NULL, 16);
back_color.r = (val & 0xFF0000) >> 16;
back_color.g = (val & 0x00FF00) >> 8;
back_color.b = (val & 0x0000FF);
back_color.a = 0xFF;
}
int channels = check_qoi_get_channels(filepath);
qoi_desc desc = {0};
unsigned char *qoi = qoi_read(filepath, &desc, channels);
if (qoi == NULL)
{
printf("ERROR: Couldn't read QOI image.\n");
exit(1);
}
InitWindow(desc.width, desc.height, "qoi-viewer");
SetWindowTitle(filepath);
printf("QOI Desc: %dx%d, channels %d\n", desc.width, desc.height, desc.channels);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(back_color);
unsigned int y = 0;
unsigned int x = 0;
size_t amount = (desc.width * desc.height) * desc.channels;
unsigned int pixel = 0;
for (size_t i = 0; i < amount;)
{
unsigned char r = qoi[i++];
unsigned char g = qoi[i++];
unsigned char b = qoi[i++];
unsigned char a = 255;
if (desc.channels == 4)
a = qoi[i++];
x = pixel % desc.width;
y = (pixel - x) / desc.width;
DrawPixel(x, y, (Color){r, g, b, a});
pixel++;
}
EndDrawing();
}
free(qoi);
CloseWindow();
}