| 1 |
/* |
| 2 |
pamtoqoi - Converts PAM to a QOI - The "Quite OK Image" format file |
| 3 |
|
| 4 |
This program is part of Netpbm. |
| 5 |
|
| 6 |
--------------------------------------------------------------------- |
| 7 |
|
| 8 |
QOI - The "Quite OK Image" format for fast, lossless image compression |
| 9 |
|
| 10 |
Encoder by Dominic Szablewski - https://phoboslab.org |
| 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 |
| 17 |
of this software and associated documentation files(the "Software"), to deal |
| 18 |
in the Software without restriction, including without limitation the rights |
| 19 |
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 20 |
copies of the Software, and to permit persons to whom the Software is |
| 21 |
furnished to do so, subject to the following conditions : |
| 22 |
|
| 23 |
The above copyright notice and this permission notice shall be included in |
| 24 |
all copies or substantial portions of the Software. |
| 25 |
|
| 26 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
| 29 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 |
SOFTWARE. |
| 33 |
|
| 34 |
For more information on the format visit: https//qoiformat.org/ . |
| 35 |
|
| 36 |
Modifications for Netpbm read routines by Akira F. Urushibata. |
| 37 |
|
| 38 |
*/ |
| 39 |
|
| 40 |
#include <string.h> |
| 41 |
#include <assert.h> |
| 42 |
#include "pam.h" |
| 43 |
#include "nstring.h" |
| 44 |
#include "mallocvar.h" |
| 45 |
#include "shhopt.h" |
| 46 |
|
| 47 |
#include "qoi.h" |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
struct CmdlineInfo { |
| 52 |
/* All the information the user supplied in the command line, |
| 53 |
in a form easy for the program to use. |
| 54 |
*/ |
| 55 |
const char * inputFileName; /* '-' if stdin */ |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static void |
| 61 |
parseCommandLine(int argc, |
| 62 |
const char ** argv, |
| 63 |
struct CmdlineInfo * cmdlineP ) { |
| 64 |
/*---------------------------------------------------------------------------- |
| 65 |
Parse program command line described in Unix standard form by argc |
| 66 |
and argv. Return the information in the options as *cmdlineP. |
| 67 |
|
| 68 |
If command line is internally inconsistent (invalid options, etc.), |
| 69 |
issue error message to stderr and abort program. |
| 70 |
|
| 71 |
Note that the strings we return are stored in the storage that |
| 72 |
was passed to us as the argv array. We also trash *argv. |
| 73 |
-----------------------------------------------------------------------------*/ |
| 74 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 75 |
optStruct3 opt; |
| 76 |
|
| 77 |
unsigned int option_def_index; |
| 78 |
|
| 79 |
MALLOCARRAY(option_def, 100); |
| 80 |
|
| 81 |
OPTENTINIT; |
| 82 |
|
| 83 |
opt.opt_table = option_def; |
| 84 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 85 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 86 |
|
| 87 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 88 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 89 |
|
| 90 |
if (argc-1 < 1) |
| 91 |
cmdlineP->inputFileName = "-"; |
| 92 |
else if (argc-1 == 1) |
| 93 |
cmdlineP->inputFileName = argv[1]; |
| 94 |
else |
| 95 |
pm_error("Program takes at most one argument: input file name. " |
| 96 |
"you specified %d", argc-1); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static void |
| 102 |
encodeQoiHeader(qoi_Desc const qoiDesc) { |
| 103 |
|
| 104 |
assert (QOI_MAGIC_SIZE + 4 + 4 + 1 + 1 == QOI_HEADER_SIZE); |
| 105 |
|
| 106 |
fwrite (qoi_magic, QOI_MAGIC_SIZE, 1, stdout); |
| 107 |
pm_writebiglongu(stdout, qoiDesc.width); |
| 108 |
pm_writebiglongu(stdout, qoiDesc.height); |
| 109 |
putchar(qoiDesc.channelCt); |
| 110 |
putchar(qoiDesc.colorspace); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
enum Tupletype {BW, BWAlpha, GRAY, GRAYAlpha, RGB, RGBAlpha, |
| 117 |
GRAY255, GRAY255Alpha, RGB255, RGB255Alpha}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static void |
| 122 |
createSampleMap(sample const oldMaxval, |
| 123 |
sample ** const sampleMapP) { |
| 124 |
|
| 125 |
unsigned int i; |
| 126 |
sample * sampleMap; |
| 127 |
sample const newMaxval = 255; |
| 128 |
|
| 129 |
MALLOCARRAY_NOFAIL(sampleMap, oldMaxval+1); |
| 130 |
|
| 131 |
for (i = 0; i <= oldMaxval; ++i) |
| 132 |
sampleMap[i] = ROUNDDIV(i * newMaxval, oldMaxval); |
| 133 |
|
| 134 |
*sampleMapP = sampleMap; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static enum Tupletype |
| 140 |
tupleTypeFmPam(const char * const pamTupleType, |
| 141 |
sample const maxval) { |
| 142 |
|
| 143 |
enum Tupletype retval; |
| 144 |
|
| 145 |
if (streq(pamTupleType, PAM_PBM_TUPLETYPE)) { |
| 146 |
if (maxval !=1) |
| 147 |
pm_error("Invalid maxval (%lu) for tuple type '%s'.", |
| 148 |
maxval, pamTupleType); |
| 149 |
else |
| 150 |
retval = BW; |
| 151 |
} else if (streq(pamTupleType, PAM_PBM_ALPHA_TUPLETYPE)) { |
| 152 |
if (maxval !=1) |
| 153 |
pm_error("Invalid maxval (%lu) for tuple type '%s'.", |
| 154 |
maxval, pamTupleType); |
| 155 |
else |
| 156 |
retval = BWAlpha; |
| 157 |
} else if (maxval == 255) { |
| 158 |
if (streq(pamTupleType, PAM_PPM_TUPLETYPE)) |
| 159 |
retval = RGB255; |
| 160 |
else if(streq(pamTupleType, PAM_PPM_ALPHA_TUPLETYPE)) |
| 161 |
retval = RGB255Alpha; |
| 162 |
else if(streq(pamTupleType, PAM_PGM_TUPLETYPE)) |
| 163 |
retval = GRAY255; |
| 164 |
else if(streq(pamTupleType, PAM_PGM_ALPHA_TUPLETYPE)) |
| 165 |
retval = GRAY255Alpha; |
| 166 |
else |
| 167 |
pm_error("Don't know how to convert tuple type '%s'.", |
| 168 |
pamTupleType); |
| 169 |
} else { |
| 170 |
if (streq(pamTupleType, PAM_PPM_TUPLETYPE)) |
| 171 |
retval = RGB; |
| 172 |
else if(streq(pamTupleType, PAM_PPM_ALPHA_TUPLETYPE)) |
| 173 |
retval = RGBAlpha; |
| 174 |
else if(streq(pamTupleType, PAM_PGM_TUPLETYPE)) |
| 175 |
retval = GRAY; |
| 176 |
else if(streq(pamTupleType, PAM_PGM_ALPHA_TUPLETYPE)) |
| 177 |
retval = GRAYAlpha; |
| 178 |
else |
| 179 |
pm_error("Don't know how to convert tuple type '%s'.", |
| 180 |
pamTupleType); |
| 181 |
} |
| 182 |
|
| 183 |
return retval; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
#pragma GCC diagnostic push |
| 189 |
#pragma GCC diagnostic ignored "-Wreturn-type" |
| 190 |
|
| 191 |
static unsigned int |
| 192 |
channelCtFmTupleType(enum Tupletype const tupleType) { |
| 193 |
|
| 194 |
switch (tupleType) { |
| 195 |
case RGB: |
| 196 |
return 3; |
| 197 |
case RGB255: |
| 198 |
return 3; |
| 199 |
case RGBAlpha: |
| 200 |
return 4; |
| 201 |
case RGB255Alpha: |
| 202 |
return 4; |
| 203 |
case BW: |
| 204 |
case GRAY: |
| 205 |
return 3; |
| 206 |
case GRAY255: |
| 207 |
return 3; |
| 208 |
case BWAlpha: |
| 209 |
case GRAYAlpha: |
| 210 |
return 4; |
| 211 |
case GRAY255Alpha: |
| 212 |
return 4; |
| 213 |
} |
| 214 |
} |
| 215 |
#pragma GCC diagnostic pop |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
static qoi_Rgba |
| 220 |
pxFmTuple(tuple const tuple0, |
| 221 |
const sample * const sampleMap, |
| 222 |
enum Tupletype const tupleType) { |
| 223 |
/*---------------------------------------------------------------------------- |
| 224 |
Convert PAM tuple to qoi rgba pixel struct |
| 225 |
-----------------------------------------------------------------------------*/ |
| 226 |
qoi_Rgba px; |
| 227 |
|
| 228 |
switch (tupleType) { |
| 229 |
case RGB: |
| 230 |
px.rgba.r = sampleMap[tuple0[PAM_RED_PLANE]]; |
| 231 |
px.rgba.g = sampleMap[tuple0[PAM_GRN_PLANE]]; |
| 232 |
px.rgba.b = sampleMap[tuple0[PAM_BLU_PLANE]]; |
| 233 |
px.rgba.a = 255; |
| 234 |
break; |
| 235 |
case RGB255: |
| 236 |
px.rgba.r = tuple0[PAM_RED_PLANE]; |
| 237 |
px.rgba.g = tuple0[PAM_GRN_PLANE]; |
| 238 |
px.rgba.b = tuple0[PAM_BLU_PLANE]; |
| 239 |
px.rgba.a = 255; |
| 240 |
break; |
| 241 |
case RGBAlpha: |
| 242 |
px.rgba.r = sampleMap[tuple0[PAM_RED_PLANE]]; |
| 243 |
px.rgba.g = sampleMap[tuple0[PAM_GRN_PLANE]]; |
| 244 |
px.rgba.b = sampleMap[tuple0[PAM_BLU_PLANE]]; |
| 245 |
px.rgba.a = sampleMap[tuple0[PAM_TRN_PLANE]]; |
| 246 |
break; |
| 247 |
case RGB255Alpha: |
| 248 |
px.rgba.r = tuple0[PAM_RED_PLANE]; |
| 249 |
px.rgba.g = tuple0[PAM_GRN_PLANE]; |
| 250 |
px.rgba.b = tuple0[PAM_BLU_PLANE]; |
| 251 |
px.rgba.a = tuple0[PAM_TRN_PLANE]; |
| 252 |
break; |
| 253 |
case BW: |
| 254 |
case GRAY : { |
| 255 |
unsigned char const qoiSample = sampleMap[tuple0[0]]; |
| 256 |
px.rgba.r = qoiSample; |
| 257 |
px.rgba.g = qoiSample; |
| 258 |
px.rgba.b = qoiSample; |
| 259 |
px.rgba.a = 255; |
| 260 |
} break; |
| 261 |
case GRAY255: { |
| 262 |
unsigned char const qoiSample = tuple0[0]; |
| 263 |
px.rgba.r = qoiSample; |
| 264 |
px.rgba.g = qoiSample; |
| 265 |
px.rgba.b = qoiSample; |
| 266 |
px.rgba.a = 255; |
| 267 |
} break; |
| 268 |
case BWAlpha: |
| 269 |
case GRAYAlpha: { |
| 270 |
unsigned char const qoiSample = sampleMap[tuple0[0]]; |
| 271 |
px.rgba.r = qoiSample; |
| 272 |
px.rgba.g = qoiSample; |
| 273 |
px.rgba.b = qoiSample; |
| 274 |
px.rgba.a = sampleMap[tuple0[PAM_GRAY_TRN_PLANE]]; |
| 275 |
} break; |
| 276 |
case GRAY255Alpha: { |
| 277 |
unsigned char const qoiSample = tuple0[0]; |
| 278 |
px.rgba.r = qoiSample; |
| 279 |
px.rgba.g = qoiSample; |
| 280 |
px.rgba.b = qoiSample; |
| 281 |
px.rgba.a = tuple0[PAM_GRAY_TRN_PLANE]; |
| 282 |
} break; |
| 283 |
} |
| 284 |
|
| 285 |
return px; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static void |
| 291 |
encodeNewPixel(qoi_Rgba const px, |
| 292 |
qoi_Rgba const pxPrev) { |
| 293 |
|
| 294 |
if (px.rgba.a == pxPrev.rgba.a) { |
| 295 |
signed char const vr = px.rgba.r - pxPrev.rgba.r; |
| 296 |
signed char const vg = px.rgba.g - pxPrev.rgba.g; |
| 297 |
signed char const vb = px.rgba.b - pxPrev.rgba.b; |
| 298 |
|
| 299 |
signed char const vgR = vr - vg; |
| 300 |
signed char const vgB = vb - vg; |
| 301 |
|
| 302 |
if ( |
| 303 |
vr > -3 && vr < 2 && |
| 304 |
vg > -3 && vg < 2 && |
| 305 |
vb > -3 && vb < 2 |
| 306 |
) { |
| 307 |
putchar(QOI_OP_DIFF | |
| 308 |
(vr + 2) << 4 | (vg + 2) << 2 | (vb + 2)); |
| 309 |
} else if ( |
| 310 |
vgR > -9 && vgR < 8 && |
| 311 |
vg > -33 && vg < 32 && |
| 312 |
vgB > -9 && vgB < 8 |
| 313 |
) { |
| 314 |
putchar(QOI_OP_LUMA | (vg + 32)); |
| 315 |
putchar((vgR + 8) << 4 | (vgB + 8)); |
| 316 |
} else { |
| 317 |
putchar(QOI_OP_RGB); |
| 318 |
putchar(px.rgba.r); |
| 319 |
putchar(px.rgba.g); |
| 320 |
putchar(px.rgba.b); |
| 321 |
} |
| 322 |
} else { |
| 323 |
putchar(QOI_OP_RGBA); |
| 324 |
putchar(px.rgba.r); |
| 325 |
putchar(px.rgba.g); |
| 326 |
putchar(px.rgba.b); |
| 327 |
putchar(px.rgba.a); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static void |
| 334 |
qoiEncode(FILE * const ifP, |
| 335 |
struct pam * const inpamP) { |
| 336 |
|
| 337 |
tuple * tuplerow; |
| 338 |
unsigned int row; |
| 339 |
|
| 340 |
qoi_Rgba index[QOI_INDEX_SIZE]; |
| 341 |
qoi_Rgba pxPrev; |
| 342 |
unsigned int run; |
| 343 |
sample * sampleMap; |
| 344 |
qoi_Desc qoiDesc; |
| 345 |
|
| 346 |
enum Tupletype const tupleType = |
| 347 |
tupleTypeFmPam(inpamP->tuple_type, inpamP->maxval); |
| 348 |
|
| 349 |
if (inpamP->height > QOI_PIXELS_MAX / inpamP->width) |
| 350 |
pm_error("Too many pixels for QOI: %u x %u (max is %u)", |
| 351 |
inpamP->height, inpamP->width, QOI_PIXELS_MAX); |
| 352 |
|
| 353 |
qoiDesc.colorspace = QOI_SRGB; |
| 354 |
qoiDesc.width = inpamP->width; |
| 355 |
qoiDesc.height = inpamP->height; |
| 356 |
qoiDesc.channelCt = channelCtFmTupleType(tupleType); |
| 357 |
|
| 358 |
encodeQoiHeader(qoiDesc); |
| 359 |
|
| 360 |
tuplerow = pnm_allocpamrow(inpamP); |
| 361 |
|
| 362 |
if (inpamP->maxval != 255) |
| 363 |
createSampleMap(inpamP->maxval, &sampleMap); |
| 364 |
|
| 365 |
qoi_clearQoiIndex(index); |
| 366 |
|
| 367 |
pxPrev.rgba.r = 0; |
| 368 |
pxPrev.rgba.g = 0; |
| 369 |
pxPrev.rgba.b = 0; |
| 370 |
pxPrev.rgba.a = 255; |
| 371 |
|
| 372 |
/* Read and convert rows. */ |
| 373 |
for (row = 0, run = 0; row < inpamP->height; ++row) { |
| 374 |
unsigned int col; |
| 375 |
|
| 376 |
pnm_readpamrow(inpamP, tuplerow); |
| 377 |
|
| 378 |
for (col = 0; col < inpamP->width; ++col) { |
| 379 |
qoi_Rgba const px = pxFmTuple(tuplerow[col], sampleMap, tupleType); |
| 380 |
|
| 381 |
if (px.v == pxPrev.v) { |
| 382 |
++run; |
| 383 |
if (run == 62) { |
| 384 |
putchar(QOI_OP_RUN | (run - 1)); |
| 385 |
run = 0; |
| 386 |
} |
| 387 |
} else { |
| 388 |
unsigned int const indexPos = qoi_colorHash(px); |
| 389 |
|
| 390 |
if (run > 0) { |
| 391 |
putchar(QOI_OP_RUN | (run - 1)); |
| 392 |
run = 0; |
| 393 |
} |
| 394 |
|
| 395 |
if (index[indexPos].v == px.v) { |
| 396 |
putchar(QOI_OP_INDEX | indexPos); |
| 397 |
|
| 398 |
} else { |
| 399 |
index[indexPos] = px; |
| 400 |
encodeNewPixel(px, pxPrev); |
| 401 |
} |
| 402 |
} |
| 403 |
pxPrev = px; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (run > 0) |
| 408 |
putchar(QOI_OP_RUN | (run - 1)); |
| 409 |
|
| 410 |
fwrite(qoi_padding, sizeof(qoi_padding), 1, stdout); |
| 411 |
|
| 412 |
if (inpamP->maxval != 255) |
| 413 |
free(sampleMap); |
| 414 |
pnm_freepamrow(tuplerow); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
int |
| 420 |
main(int argc, const char **argv) { |
| 421 |
|
| 422 |
struct CmdlineInfo cmdline; |
| 423 |
struct pam inpam; |
| 424 |
FILE * ifP; |
| 425 |
|
| 426 |
pm_proginit(&argc, argv); |
| 427 |
|
| 428 |
parseCommandLine(argc, argv, &cmdline); |
| 429 |
|
| 430 |
ifP = pm_openr(cmdline.inputFileName); |
| 431 |
|
| 432 |
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 433 |
|
| 434 |
qoiEncode(ifP, &inpam); |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|