-
Notifications
You must be signed in to change notification settings - Fork 3
/
png2qoi.c
180 lines (167 loc) · 5.19 KB
/
png2qoi.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
// png2qoi.c - command-line converter between PNG and QOI formats
//
// Copyright (C) 2021-2024 Piotr Fusik
//
// MIT License:
//
// 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.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include "QOI.h"
#include "QOI-stdio.h"
static void usage(void)
{
puts(
"Usage: png2qoi [OPTIONS] INPUTFILE...\n"
"INPUTFILE must be in PNG or QOI format.\n"
"Options:\n"
"-o FILE --output=FILE Set output file name\n"
"-h --help Display this information\n"
"-v --version Display version information"
);
}
static bool png2qoi(const char *input_file, FILE *f, const char *output_file)
{
if (fseek(f, 0, SEEK_SET) != 0) {
perror(input_file);
fclose(f);
return false;
}
png_image png;
png.opaque = NULL;
png.version = PNG_IMAGE_VERSION;
if (png_image_begin_read_from_stdio(&png, f) == 0) {
fclose(f);
fprintf(stderr, "png2qoi: %s: %s\n", input_file, png.message);
return false;
}
bool alpha = (png.format & PNG_FORMAT_FLAG_ALPHA) != 0;
png.format = PNG_FORMAT_BGRA;
void *pixels = malloc(PNG_IMAGE_SIZE(png));
if (pixels == NULL) {
fprintf(stderr, "png2qoi: %s: out of memory\n", input_file);
return false;
}
if (png_image_finish_read(&png, NULL, pixels, 0, NULL) == 0) {
free(pixels);
fclose(f);
fprintf(stderr, "png2qoi: %s: %s\n", input_file, png.message);
return false;
}
fclose(f);
QOIEncoder *qoi = QOIEncoder_New();
if (!QOIEncoder_Encode(qoi, png.width, png.height, pixels, alpha, false)) {
QOIEncoder_Delete(qoi);
free(pixels);
fprintf(stderr, "png2qoi: %s: error encoding\n", input_file);
return false;
}
free(pixels);
if (!QOIEncoder_SaveFile(qoi, output_file)) {
perror(output_file);
QOIEncoder_Delete(qoi);
return false;
}
QOIEncoder_Delete(qoi);
return true;
}
static bool qoi2png(const char *input_file, FILE *f, const char *output_file)
{
QOIDecoder *qoi = QOIDecoder_LoadStdio(f);
fclose(f);
if (qoi == NULL) {
fprintf(stderr, "png2qoi: %s: error loading\n", input_file);
return false;
}
png_image png = { NULL };
png.version = PNG_IMAGE_VERSION;
png.width = QOIDecoder_GetWidth(qoi);
png.height = QOIDecoder_GetHeight(qoi);
png.format = PNG_FORMAT_BGRA;
if (png_image_write_to_file(&png, output_file, 0, QOIDecoder_GetPixels(qoi), 0, NULL) == 0) {
QOIDecoder_Delete(qoi);
fprintf(stderr, "png2qoi: %s: error encoding\n", output_file);
return false;
}
QOIDecoder_Delete(qoi);
return true;
}
static bool process_file(const char *input_file, const char *output_file)
{
FILE *f = fopen(input_file, "rb");
if (f == NULL) {
perror(input_file);
return false;
}
uint8_t magic[8];
if (fread(magic, 1, sizeof(magic), f) != sizeof(magic)) {
fclose(f);
fprintf(stderr, "png2qoi: %s: file too short\n", input_file);
return false;
}
char default_output_file[FILENAME_MAX];
if (output_file == NULL) {
const char *ext = png_check_sig(magic, sizeof(magic)) ? "qoi" : "png";
if (snprintf(default_output_file, sizeof(default_output_file), "%s.%s", input_file, ext) >= sizeof(default_output_file)) {
fclose(f);
fprintf(stderr, "png2qoi: %s: filename too long\n", input_file);
return false;
}
output_file = default_output_file;
}
if (png_check_sig(magic, sizeof(magic)))
return png2qoi(input_file, f, output_file);
if (memcmp(magic, "qoif", 4) == 0)
return qoi2png(input_file, f, output_file);
fclose(f);
fprintf(stderr, "png2qoi: %s: unrecognized file format\n", input_file);
return false;
}
int main(int argc, char **argv)
{
if (argc < 2) {
usage();
return 1;
}
const char *output_file = NULL;
bool ok = true;
for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
if (arg[0] != '-') {
ok &= process_file(arg, output_file);
output_file = NULL;
}
else if (arg[1] == 'o' && arg[2] == '\0' && i + 1 < argc)
output_file = argv[++i];
else if (strncmp(arg, "--output=", 9) == 0)
output_file = arg + 9;
else if ((arg[1] == 'h' && arg[2] == '\0') || strcmp(arg, "--help") == 0)
usage();
else if ((arg[1] == 'v' && arg[2] == '\0') || strcmp(arg, "--version") == 0)
puts("png2qoi 3.0.0");
else {
fprintf(stderr, "png2qoi: unknown option: %s\n", arg);
return 1;
}
}
return ok ? 0 : 1;
}