-
Notifications
You must be signed in to change notification settings - Fork 0
/
qoibench.c
229 lines (177 loc) · 5.51 KB
/
qoibench.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
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#define STBI_NO_LINEAR
#include "stb_image.h" //PNG Decoder
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h" //PNG Encoder
#define QOI_IMPLEMENTATION
#include "qoi.h"
#define STR_ENDS_WITH(S, E) (strcmp(S + strlen(S) - (sizeof(E) - 1), E) == 0)
// Function to concatenate three strings
char *concatenateThreeStrings(const char *str1, const char *str2, const char *str3)
{
// Calculate the total length of the concatenated string
size_t totalLength = strlen(str1) + strlen(str2) + strlen(str3);
// Allocate memory for the concatenated string
char *result = (char *)malloc(totalLength + 1);
if (result == NULL)
{
// Memory allocation failed
return NULL;
}
// Copy the first string into the result
strcpy(result, str1);
// Concatenate the second string to the result
strcat(result, str2);
// Concatenate the third string to the result
strcat(result, str3);
return result;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
puts("Usage: qoibench <infile> <bench_name>");
puts("Examples:");
puts(" qoiconv input.png pic-01");
exit(1);
}
void *pixels = NULL;
int w, h, channels;
if (STR_ENDS_WITH(argv[1], ".png"))
{
if (!stbi_info(argv[1], &w, &h, &channels))
{
printf("Couldn't read header %s\n", argv[1]);
exit(1);
}
// Force all odd encodings to be RGBA
if (channels != 3)
{
channels = 4;
}
pixels = (void *)stbi_load(argv[1], &w, &h, NULL, channels);
}
else if (STR_ENDS_WITH(argv[1], ".qoi"))
{
qoi_desc desc;
pixels = qoi_read(argv[1], &desc, 0);
channels = desc.channels;
w = desc.width;
h = desc.height;
}
if (pixels == NULL)
{
printf("Couldn't load/decode %s\n", argv[1]);
exit(1);
}
// We found
// Width , Hight , Channels , Raw Pixels
clock_t start, end;
double cpu_time_used;
//--------------------------------------------------------------------------------------------------------------
// First we have to store binary data in SB
int decodedLength = w * h * channels;
char *name = concatenateThreeStrings("SB/", argv[2], ".bin");
if (name == NULL)
{
printf("Memory allocation of name failed.\n");
}
FILE *fp = fopen(name, "wb");
fwrite(pixels, 1, decodedLength, fp);
fclose(fp);
printf("Wrote decoded Binary image (%i bytes)\n", decodedLength);
//--------------------------------------------------------------------------------------------------------------
// Second we have to store png data in SP
int png_encoded_size = 0;
name = concatenateThreeStrings("SP/", argv[2], ".png");
if (name == NULL)
{
printf("Memory allocation of name failed.\n");
}
start = clock();
png_encoded_size = stbi_write_png(name, w, h, channels, pixels, 0);
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
if (!png_encoded_size) {
printf("Couldn't write/encode png %s\n", argv[2]);
exit(1);
} else {
printf("\nPNG ENCODE TIME -\t%lf \tSIZE -\t%d\n", cpu_time_used, png_encoded_size);
}
//--------------------------------------------------------------------------------------------------------------
// Third we have to store png data in SQ
int qoi_encoded_size = 0;
name = concatenateThreeStrings("SQ/", argv[2], ".qoi");
if (name == NULL)
{
printf("Memory allocation of name failed.\n");
}
start = clock();
qoi_encoded_size = qoi_write(name, pixels, &(qoi_desc){
.width = w,
.height = h,
.channels = channels,
.colorspace = QOI_SRGB
});
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
if (!qoi_encoded_size) {
printf("Couldn't write/encode qoi %s\n", argv[2]);
exit(1);
} else {
printf("\nQOI ENCODE TIME -\t%lf \tSIZE - \t%d\n", cpu_time_used, qoi_encoded_size);
}
free(pixels);
free(name);
//DECODE TIME CALCULATION
//PNG Decode Time
name = concatenateThreeStrings("SP/", argv[2], ".png");
if (name == NULL)
{
printf("Memory allocation of name failed.\n");
}
if (!stbi_info(name, &w, &h, &channels))
{
printf("Couldn't read header %s\n", argv[1]);
exit(1);
}
// Force all odd encodings to be RGBA
if (channels != 3)
{
channels = 4;
}
start = clock();
pixels = (void *)stbi_load(argv[1], &w, &h, NULL, channels);
end = clock();
if (pixels == NULL)
{
printf("Couldn't load/decode %s\n", name);
exit(1);
}else{
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("\nPNG DECODE TIME -\t%lf\n", cpu_time_used);
}
//QOI Decode Time
name = concatenateThreeStrings("SQ/", argv[2], ".qoi");
if (name == NULL)
{
printf("Memory allocation of name failed.\n");
}
qoi_desc desc;
start = clock();
pixels = qoi_read(name , &desc, 0);
end = clock();
if (pixels == NULL)
{
printf("Couldn't load/decode %s\n", name);
exit(1);
}else{
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("\nQOI DECODE TIME -\t%lf\n", cpu_time_used);
}
return 0;
}