-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqoipconv.c
232 lines (193 loc) · 7.16 KB
/
qoipconv.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
230
231
232
/* SPDX-License-Identifier: MIT */
/* Command line tool to convert between png <> qoip format
Requires "stb_image.h" and "stb_image_write.h"
Compile with:
gcc qoipconv.c -std=c99 -O3 -o qoipconv
Dominic Szablewski - https://phoboslab.org
Copyright 2021 Dominic Szablewski
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files(the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#define STBI_NO_LINEAR
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define QOIPCRUNCH_C
#include "qoipcrunch.h"
#define QOIP_C
#include "qoip.h"
#define OPT_C
#include "qoipconv-opt.h"
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
/* Encode raw RGB or RGBA pixels into a QOIP image and write it to the file
system. The qoip_desc struct must be filled with the image width, height,
number of channels (3 = RGB, 4 = RGBA) and the colorspace.
The function returns 0 on failure (invalid parameters, or fopen or malloc
failed) or the number of bytes written on success. */
size_t qoip_write(const char *filename, const void *data, const qoip_desc *desc);
/* Read and decode a QOIP image from the file system. If channels is 0, the
number of channels from the file header is used. If channels is 3 or 4 the
output format will be forced into this number of channels.
The function either returns NULL on failure (invalid data, or malloc or fopen
failed) or a pointer to the decoded pixels. On success, the qoip_desc struct
will be filled with the description from the file header.
The returned pixel data should be free()d after use. */
void *qoip_read(const char *filename, qoip_desc *desc, int channels);
#ifndef QOIP_MALLOC
#define QOIP_MALLOC(sz) malloc(sz)
#define QOIP_FREE(p) free(p)
#endif
size_t qoipcrunch_write(const char *filename, const void *data, const qoip_desc *desc, char *effort, int threads, int entropy) {
FILE *f;
size_t max_size, size;
void *encoded, *scratch;
int encode_ret;
max_size = qoip_maxsize(desc);
max_size = max_size < qoip_maxentropysize(max_size, entropy) ? qoip_maxentropysize(max_size, entropy) : max_size;
encoded = QOIP_MALLOC(max_size);
scratch = QOIP_MALLOC(max_size*threads);
encode_ret = qoipcrunch_encode(data, desc, encoded, &size, effort, scratch, threads, entropy);
if ( !encoded || !scratch || encode_ret || !(f = fopen(filename, "wb")) ) {
QOIP_FREE(encoded);
QOIP_FREE(scratch);
return 0;
}
fwrite(encoded, 1, size, f);
fclose(f);
QOIP_FREE(encoded);
QOIP_FREE(scratch);
return size;
}
void *qoip_read(const char *filename, qoip_desc *desc, int channels) {
FILE *f = fopen(filename, "rb");
size_t max_size, size;
void *pixels = NULL, *data = NULL, *scratch = NULL;
if (!f)
goto cleanup;
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size == 0)
goto cleanup;
rewind(f);
if ( !(data = QOIP_MALLOC(size)) )
goto cleanup;
if ( fread(data, 1, size, f)!=size )
goto cleanup;
qoip_read_header(data, NULL, desc);
max_size = qoip_maxsize_raw(desc, channels);
if ( desc->entropy && !(scratch = QOIP_MALLOC(desc->raw_cnt)) )
goto cleanup;
if ( !(pixels = QOIP_MALLOC(max_size)) )
goto cleanup;
if ( qoip_decode(data, size, desc, channels, pixels, scratch) ) {
free(pixels);
pixels = NULL;
}
cleanup:
if(data)
QOIP_FREE(data);
if(f)
fclose(f);
if(scratch)
free(scratch);
return pixels;
}
#define STR_ENDS_WITH(S, E) (strcmp(S + strlen(S) - (sizeof(E)-1), E) == 0)
int optmode_license(opt_t *opt) {
printf("The MIT License(MIT)\n");
printf("\n");
printf("Copyright 2021 Dominic Szablewski (QOI format)\n");
printf("Copyright 2021 Matthew Ling (QOIP format)\n");
printf("\n");
printf("Permission is hereby granted, free of charge, to any person obtaining a copy of\n");
printf("this software and associated documentation files(the \"Software\"), to deal in\n");
printf("the Software without restriction, including without limitation the rights to\n");
printf("use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies\n");
printf("of the Software, and to permit persons to whom the Software is furnished to do\n");
printf("so, subject to the following conditions :\n");
printf("The above copyright notice and this permission notice shall be included in all\n");
printf("copies or substantial portions of the Software.\n");
printf("THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n");
printf("IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n");
printf("FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE\n");
printf("AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n");
printf("LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n");
printf("OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n");
printf("SOFTWARE.\n");
return 0;
}
int main(int argc, char **argv) {
opt_t opt;
char effort_level[32];
/* Process args */
opt_init(&opt);
opt_process(&opt, argc, argv);
if(opt_dispatch(&opt))
return 1;
else if(argc==1)
optmode_help(&opt);
sprintf(effort_level, "%d", opt.effort);
if(!opt.in || !opt.out) {
printf("Input and output files need to be defined\n");
return 1;
}
void *pixels = NULL;
int w, h, channels;
if (STR_ENDS_WITH(opt.in, ".png")) {
if(!stbi_info(opt.in, &w, &h, &channels)) {
printf("Couldn't read header %s\n", opt.in);
return 1;
}
// Force all odd encodings to be RGBA
if(channels != 3) {
channels = 4;
}
pixels = (void *)stbi_load(opt.in, &w, &h, NULL, channels);
}
else if (STR_ENDS_WITH(opt.in, ".qoip")) {
qoip_desc desc;
pixels = qoip_read(opt.in, &desc, 0);
channels = desc.channels;
w = desc.width;
h = desc.height;
}
if (pixels == NULL) {
printf("Couldn't load/decode %s\n", opt.in);
exit(1);
}
int encoded = 0;
if (STR_ENDS_WITH(opt.out, ".png")) {
encoded = stbi_write_png(opt.out, w, h, channels, pixels, 0);
}
else if (STR_ENDS_WITH(opt.out, ".qoip")) {
encoded = qoipcrunch_write(opt.out, pixels, &(qoip_desc){
.width = w,
.height = h,
.channels = channels,
.colorspace = QOIP_SRGB
}, (opt.custom?opt.custom:effort_level), opt.threads, opt.entropy);
}
if (!encoded) {
printf("Couldn't write/encode %s\n", opt.out);
return 1;
}
free(pixels);
return 0;
}