2021-11-24 10:07:17 +00:00
|
|
|
/*
|
|
|
|
|
2022-08-05 22:39:12 +01:00
|
|
|
Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org
|
|
|
|
SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
2021-11-24 10:07:17 +00:00
|
|
|
Command line tool to convert between png <> qoi format
|
|
|
|
|
2022-06-11 18:58:41 +01:00
|
|
|
Requires:
|
|
|
|
-"stb_image.h" (https://github.com/nothings/stb/blob/master/stb_image.h)
|
|
|
|
-"stb_image_write.h" (https://github.com/nothings/stb/blob/master/stb_image_write.h)
|
2022-06-11 19:26:59 +01:00
|
|
|
-"qoi.h" (https://github.com/phoboslab/qoi/blob/master/qoi.h)
|
2022-06-11 18:58:41 +01:00
|
|
|
|
2021-11-24 10:07:17 +00:00
|
|
|
Compile with:
|
|
|
|
gcc qoiconv.c -std=c99 -O3 -o qoiconv
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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 QOI_IMPLEMENTATION
|
|
|
|
#include "qoi.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define STR_ENDS_WITH(S, E) (strcmp(S + strlen(S) - (sizeof(E)-1), E) == 0)
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc < 3) {
|
2021-12-21 09:41:33 +00:00
|
|
|
puts("Usage: qoiconv <infile> <outfile>");
|
|
|
|
puts("Examples:");
|
|
|
|
puts(" qoiconv input.png output.qoi");
|
|
|
|
puts(" qoiconv input.qoi output.png");
|
2021-11-24 10:07:17 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *pixels = NULL;
|
2021-11-27 17:36:17 +00:00
|
|
|
int w, h, channels;
|
2021-11-24 10:07:17 +00:00
|
|
|
if (STR_ENDS_WITH(argv[1], ".png")) {
|
2021-12-13 16:34:39 +00:00
|
|
|
if(!stbi_info(argv[1], &w, &h, &channels)) {
|
|
|
|
printf("Couldn't read header %s\n", argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-12-14 19:54:37 +00:00
|
|
|
|
|
|
|
// Force all odd encodings to be RGBA
|
|
|
|
if(channels != 3) {
|
2021-12-13 16:34:39 +00:00
|
|
|
channels = 4;
|
|
|
|
}
|
2021-12-14 19:54:37 +00:00
|
|
|
|
|
|
|
pixels = (void *)stbi_load(argv[1], &w, &h, NULL, channels);
|
2021-11-24 10:07:17 +00:00
|
|
|
}
|
|
|
|
else if (STR_ENDS_WITH(argv[1], ".qoi")) {
|
2021-11-27 17:36:17 +00:00
|
|
|
qoi_desc desc;
|
|
|
|
pixels = qoi_read(argv[1], &desc, 0);
|
|
|
|
channels = desc.channels;
|
|
|
|
w = desc.width;
|
|
|
|
h = desc.height;
|
2021-11-24 10:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pixels == NULL) {
|
|
|
|
printf("Couldn't load/decode %s\n", argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int encoded = 0;
|
|
|
|
if (STR_ENDS_WITH(argv[2], ".png")) {
|
2021-11-27 17:36:17 +00:00
|
|
|
encoded = stbi_write_png(argv[2], w, h, channels, pixels, 0);
|
2021-11-24 10:07:17 +00:00
|
|
|
}
|
|
|
|
else if (STR_ENDS_WITH(argv[2], ".qoi")) {
|
2021-11-27 17:36:17 +00:00
|
|
|
encoded = qoi_write(argv[2], pixels, &(qoi_desc){
|
|
|
|
.width = w,
|
|
|
|
.height = h,
|
|
|
|
.channels = channels,
|
|
|
|
.colorspace = QOI_SRGB
|
|
|
|
});
|
2021-11-24 10:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!encoded) {
|
|
|
|
printf("Couldn't write/encode %s\n", argv[2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:36:17 +00:00
|
|
|
free(pixels);
|
2021-11-24 10:07:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|