wow
[vg.git] / src / qoiconv.c
1 /*
2
3 Command line tool to convert between png <> qoi format
4
5 Requires "stb_image.h" and "stb_image_write.h"
6 Compile with:
7 gcc qoiconv.c -std=c99 -O3 -o qoiconv
8
9 Dominic Szablewski - https://phoboslab.org
10
11
12 -- LICENSE: The MIT License(MIT)
13
14 Copyright(c) 2021 Dominic Szablewski
15
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
30 SOFTWARE.
31
32 */
33
34
35 #define STB_IMAGE_IMPLEMENTATION
36 #define STBI_ONLY_PNG
37 #define STBI_NO_LINEAR
38 #include "stb/stb_image.h"
39
40 //#define STB_IMAGE_WRITE_IMPLEMENTATION
41 //#include "stb_image_write.h"
42
43 #define QOI_IMPLEMENTATION
44 #include "phoboslab/qoi.h"
45
46
47 #define STR_ENDS_WITH(S, E) (strcmp(S + strlen(S) - (sizeof(E)-1), E) == 0)
48
49 int main(int argc, char **argv) {
50 if (argc < 3) {
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");
55 exit(1);
56 }
57
58 stbi_set_flip_vertically_on_load(1);
59
60 void *pixels = NULL;
61 int w, h, channels;
62 if (STR_ENDS_WITH(argv[1], ".png")) {
63 pixels = (void *)stbi_load(argv[1], &w, &h, &channels, 4);
64 }
65 else if (STR_ENDS_WITH(argv[1], ".qoi")) {
66 qoi_desc desc;
67 pixels = qoi_read(argv[1], &desc, 0);
68 channels = desc.channels;
69 w = desc.width;
70 h = desc.height;
71 }
72
73 if (pixels == NULL) {
74 printf("Couldn't load/decode %s\n", argv[1]);
75 exit(1);
76 }
77
78 int encoded = 0;
79 if (STR_ENDS_WITH(argv[2], ".png")) {
80 //encoded = stbi_write_png(argv[2], w, h, channels, pixels, 0);
81 }
82 else if (STR_ENDS_WITH(argv[2], ".qoi")) {
83 encoded = qoi_write(argv[2], pixels, &(qoi_desc){
84 .width = w,
85 .height = h,
86 .channels = 4,
87 .colorspace = QOI_SRGB
88 });
89 }
90
91 if (!encoded) {
92 printf("Couldn't write/encode %s\n", argv[2]);
93 exit(1);
94 }
95
96 free(pixels);
97 return 0;
98 }