Debian Codegraph @

converter/other/qoitopam.c

raw
1 /*
2 qoitopam - Converts from a QOI - The "Quite OK Image" format file to PAM
3
4 This program is part of Netpbm.
5
6 ---------------------------------------------------------------------
7
8
9 QOI - The "Quite OK Image" format for fast, lossless image compression
10
11 Decoder by Dominic Szablewski - https://phoboslab.org
12
13 -- LICENSE: The MIT License(MIT)
14
15 Copyright(c) 2021 Dominic Szablewski
16
17 Permission is hereby granted, free of charge, to any person obtaining a copy
18 of this software and associated documentation files(the "Software"), to deal
19 in the Software without restriction, including without limitation the rights
20 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
21 copies of the Software, and to permit persons to whom the Software is
22 furnished to do so, subject to the following conditions :
23
24 The above copyright notice and this permission notice shall be included in
25 all copies or substantial portions of the Software.
26
27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
30 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 SOFTWARE.
34
35 For more information on the format visit: https//qoiformat.org/ .
36
37 Modifications for Netpbm & PAM write routines by Akira F. Urushibata.
38 */
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <assert.h>
43
44 #include "pm.h"
45 #include "pam.h"
46 #include "mallocvar.h"
47 #include "nstring.h"
48 #include "shhopt.h"
49
50 #include "qoi.h"
51
52
53
54 struct CmdlineInfo {
55 /* All the information the user supplied in the command line,
56 in a form easy for the program to use.
57 */
58 const char * inputFileName; /* '-' if stdin */
59 };
60
61
62
63 static void
64 parseCommandLine(int argc,
65 const char ** argv,
66 struct CmdlineInfo * cmdlineP ) {
67 /*----------------------------------------------------------------------------
68 Parse program command line described in Unix standard form by argc
69 and argv. Return the information in the options as *cmdlineP.
70
71 If command line is internally inconsistent (invalid options, etc.),
72 issue error message to stderr and abort program.
73
74 Note that the strings we return are stored in the storage that
75 was passed to us as the argv array. We also trash *argv.
76 -----------------------------------------------------------------------------*/
77 optEntry * option_def; /* Used by OPTENT3 */
78 optStruct3 opt;
79
80 unsigned int option_def_index;
81
82 MALLOCARRAY(option_def, 100);
83
84 OPTENTINIT;
85
86 opt.opt_table = option_def;
87 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
88 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
89
90 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
91 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
92
93 if (argc-1 < 1)
94 cmdlineP->inputFileName = "-";
95 else if (argc-1 == 1)
96 cmdlineP->inputFileName = argv[1];
97 else
98 pm_error("Program takes at most one argument: input file name. "
99 "you specified %d", argc-1);
100 }
101
102
103
104 static void
105 readAndValidateMagic(FILE * const ifP){
106
107 char magicBuff[QOI_MAGIC_SIZE];
108 size_t charsReadCt;
109
110 charsReadCt = fread(magicBuff, 1, QOI_MAGIC_SIZE, ifP);
111
112 if (charsReadCt == 0)
113 pm_error("Input file is empty.");
114 else if (charsReadCt < QOI_MAGIC_SIZE || !MEMSEQ(&magicBuff, &qoi_magic)) {
115 assert(QOI_MAGIC_SIZE == 4);
116 pm_error("Invalid QOI image: does not start with magic number "
117 "'%c%c%c%c'",
118 qoi_magic[0], qoi_magic[1], qoi_magic[2], qoi_magic[3]);
119 }
120 }
121
122
123
124 /* The following two functions are from lib/pmfileio.c */
125
126 static void
127 abortWithReadError(FILE * const ifP) {
128
129 if (feof(ifP))
130 pm_error("Unexpected end of input file");
131 else
132 pm_error("Error (not EOF) reading file.");
133 }
134
135
136
137 static unsigned char
138 getcNofail(FILE * const ifP) {
139
140 int c;
141
142 c = getc(ifP);
143
144 if (c == EOF)
145 abortWithReadError(ifP);
146
147 return (unsigned char) c;
148 }
149
150
151
152 static void
153 decodeQoiHeader(FILE * const ifP,
154 qoi_Desc * const qoiDescP) {
155
156 unsigned long int width, height;
157
158 readAndValidateMagic(ifP);
159
160 pm_readbiglongu(ifP, &width);
161 if (width == 0)
162 pm_error("Invalid QOI image: width is zero");
163 else
164 qoiDescP->width = width;
165
166 pm_readbiglongu(ifP, &height);
167 if (height == 0)
168 pm_error("Invalid QOI image: height is zero");
169 else if (height > QOI_PIXELS_MAX / width)
170 pm_error ("Invalid QOI image: %u x %u is more than %u pixels",
171 (unsigned int) width, (unsigned int) height, QOI_PIXELS_MAX);
172 else
173 qoiDescP->height = height;
174
175 qoiDescP->channelCt = getcNofail(ifP);
176 if (qoiDescP->channelCt != 3 && qoiDescP->channelCt != 4)
177 pm_error("Invalid QOI image: channel count is %u. "
178 "Only 3 and 4 are valid", qoiDescP->channelCt);
179
180 qoiDescP->colorspace = getcNofail(ifP);
181 if (qoiDescP->colorspace != QOI_SRGB && qoiDescP->colorspace != QOI_LINEAR)
182 pm_error("Invalid QOI image: colorspace code is %u. "
183 "Only %u (SRGB) and %u (LINEAR) are valid",
184 qoiDescP->colorspace, QOI_SRGB, QOI_LINEAR);
185 }
186
187
188
189 static void
190 qoiDecode(FILE * const ifP,
191 qoi_Desc * const qoiDescP,
192 struct pam * const outpamP) {
193
194 qoi_Rgba index[QOI_INDEX_SIZE];
195 unsigned int row;
196 qoi_Rgba px;
197 unsigned int run;
198 tuple * tuplerow;
199
200 assert(qoiDescP);
201 tuplerow = pnm_allocpamrow(outpamP);
202
203 qoi_clearQoiIndex(index);
204 px.rgba.r = px.rgba.g = px.rgba.b = 0;
205 px.rgba.a = 255;
206
207 for (row = 0, run = 0; row < outpamP->height; ++row) {
208 unsigned int col;
209
210 for (col = 0; col < outpamP->width; ++col) {
211 if (run > 0) {
212 --run;
213 } else {
214 unsigned char const b1 = getcNofail(ifP);
215
216 if (b1 == QOI_OP_RGB) {
217 px.rgba.r = getcNofail(ifP);
218 px.rgba.g = getcNofail(ifP);
219 px.rgba.b = getcNofail(ifP);
220 } else if (b1 == QOI_OP_RGBA) {
221 px.rgba.r = getcNofail(ifP);
222 px.rgba.g = getcNofail(ifP);
223 px.rgba.b = getcNofail(ifP);
224 px.rgba.a = getcNofail(ifP);
225 } else if ((b1 & QOI_MASK_2) == QOI_OP_INDEX) {
226 /* Official spec says 2 or more consecutive instances of
227 QOI_OP_INDEX are not allowed, but we don't check */
228 px = index[b1];
229 } else if ((b1 & QOI_MASK_2) == QOI_OP_DIFF) {
230 px.rgba.r += ((b1 >> 4) & 0x03) - 2;
231 px.rgba.g += ((b1 >> 2) & 0x03) - 2;
232 px.rgba.b += ( b1 & 0x03) - 2;
233 } else if ((b1 & QOI_MASK_2) == QOI_OP_LUMA) {
234 unsigned char const b2 = getcNofail(ifP);
235 unsigned char const vg = (b1 & 0x3f) - 32;
236 px.rgba.r += vg - 8 + ((b2 >> 4) & 0x0f);
237 px.rgba.g += vg;
238 px.rgba.b += vg - 8 + (b2 & 0x0f);
239 } else if ((b1 & QOI_MASK_2) == QOI_OP_RUN) {
240 run = (b1 & 0x3f);
241 }
242 /* register pixel in hash lookup array */
243 index[qoi_colorHash(px)] = px;
244 }
245 tuplerow[col][PAM_RED_PLANE] = px.rgba.r;
246 tuplerow[col][PAM_GRN_PLANE] = px.rgba.g;
247 tuplerow[col][PAM_BLU_PLANE] = px.rgba.b;
248 if (qoiDescP->channelCt == 4)
249 tuplerow[col][PAM_TRN_PLANE] = px.rgba.a;
250 }
251 pnm_writepamrow(outpamP, tuplerow);
252 }
253 if (run > 0)
254 pm_error("Invalid QOI image: %u (or more) extra pixels "
255 "beyond end of image.", run);
256
257 pnm_freepamrow(tuplerow);
258 }
259
260
261
262 static void
263 readAndValidatePadding(FILE * const ifP){
264
265 unsigned char padBuff[QOI_PADDING_SIZE];
266 size_t charsReadCt;
267
268 charsReadCt = fread(padBuff, 1, QOI_PADDING_SIZE, ifP);
269
270 if(charsReadCt < QOI_PADDING_SIZE) {
271 pm_error("Invalid QOI image. Error reading final 8-byte padding. "
272 "Premature end of file.");
273 } else if (!MEMSEQ(&padBuff, &qoi_padding))
274 pm_error("Invalid QOI image. Final 8-byte padding incorrect.");
275 else if (fgetc(ifP) != EOF)
276 pm_error("Invalid QOI image. "
277 "Extraneous bytes after final 8-byte padding.");
278 }
279
280
281
282 int
283 main(int argc, const char **argv) {
284
285 struct CmdlineInfo cmdline;
286 qoi_Desc qoiDesc;
287 struct pam outpam;
288 FILE * ifP;
289
290 pm_proginit(&argc, argv);
291
292 parseCommandLine(argc, argv, &cmdline);
293
294 ifP = pm_openr(cmdline.inputFileName);
295
296 outpam.size = sizeof(struct pam);
297 outpam.len = PAM_STRUCT_SIZE(tuple_type);
298 outpam.maxval = QOI_MAXVAL;
299 outpam.plainformat = 0;
300
301 decodeQoiHeader(ifP, &qoiDesc);
302
303 outpam.depth = qoiDesc.channelCt == 3 ? 3 : 4;
304 outpam.width = qoiDesc.width;
305 outpam.height = qoiDesc.height;
306 outpam.format = PAM_FORMAT;
307 outpam.file = stdout;
308
309 if (qoiDesc.channelCt == 3)
310 strcpy(outpam.tuple_type, PAM_PPM_TUPLETYPE);
311 else
312 strcpy(outpam.tuple_type, PAM_PPM_ALPHA_TUPLETYPE);
313
314 pnm_writepaminit(&outpam);
315 qoiDecode(ifP, &qoiDesc, &outpam);
316
317 readAndValidatePadding(ifP);
318
319 return 0;
320 }
321
322
323