3 Command line tool to convert between png <> qoi format
5 Requires "stb_image.h" and "stb_image_write.h"
7 gcc qoiconv.c -std=c99 -O3 -o qoiconv
9 Dominic Szablewski - https://phoboslab.org
12 -- LICENSE: The MIT License(MIT)
14 Copyright(c) 2021 Dominic Szablewski
16 Permission is hereby granted, free of charge, to any person obtaining a copy of
17 this software and associated documentation files(the "Software"), to deal in
18 the Software without restriction, including without limitation the rights to
19 use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
20 of the Software, and to permit persons to whom the Software is furnished to do
21 so, subject to the following conditions :
22 The above copyright notice and this permission notice shall be included in all
23 copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #define STB_IMAGE_IMPLEMENTATION
37 #define STBI_NO_LINEAR
38 #include "stb/stb_image.h"
40 //#define STB_IMAGE_WRITE_IMPLEMENTATION
41 //#include "stb_image_write.h"
43 #define QOI_IMPLEMENTATION
44 #include "phoboslab/qoi.h"
47 #define STR_ENDS_WITH(S, E) (strcmp(S + strlen(S) - (sizeof(E)-1), E) == 0)
49 int main(int argc
, char **argv
) {
51 printf("Usage: qoiconv <infile> <outfile>\n");
52 printf("Examples:\n");
53 printf(" qoiconv input.png output.qoi\n");
54 printf(" qoiconv input.qoi output.png\n");
58 stbi_set_flip_vertically_on_load(1);
62 if (STR_ENDS_WITH(argv
[1], ".png")) {
63 pixels
= (void *)stbi_load(argv
[1], &w
, &h
, &channels
, 4);
65 else if (STR_ENDS_WITH(argv
[1], ".qoi")) {
67 pixels
= qoi_read(argv
[1], &desc
, 0);
68 channels
= desc
.channels
;
74 printf("Couldn't load/decode %s\n", argv
[1]);
79 if (STR_ENDS_WITH(argv
[2], ".png")) {
80 //encoded = stbi_write_png(argv[2], w, h, channels, pixels, 0);
82 else if (STR_ENDS_WITH(argv
[2], ".qoi")) {
83 encoded
= qoi_write(argv
[2], pixels
, &(qoi_desc
){
87 .colorspace
= QOI_SRGB
92 printf("Couldn't write/encode %s\n", argv
[2]);