| 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 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | #ifndef QOI_H |
| 215 | #define QOI_H |
| 216 | |
| 217 | #ifdef __cplusplus |
| 218 | extern "C" { |
| 219 | #endif |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | #define QOI_SRGB 0 |
| 234 | #define QOI_LINEAR 1 |
| 235 | |
| 236 | typedef struct { |
| 237 | unsigned int width; |
| 238 | unsigned int height; |
| 239 | unsigned char channels; |
| 240 | unsigned char colorspace; |
| 241 | } qoi_desc; |
| 242 | |
| 243 | #ifndef QOI_NO_STDIO |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | int qoi_write(const char *filename, const void *data, const qoi_desc *desc); |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | void *qoi_read(const char *filename, qoi_desc *desc, int channels); |
| 266 | |
| 267 | #endif |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len); |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels); |
| 290 | |
| 291 | |
| 292 | #ifdef __cplusplus |
| 293 | } |
| 294 | #endif |
| 295 | #endif |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | #ifdef QOI_IMPLEMENTATION |
| 302 | #include <stdlib.h> |
| 303 | #include <string.h> |
| 304 | |
| 305 | #ifndef QOI_MALLOC |
| 306 | #define QOI_MALLOC(sz) malloc(sz) |
| 307 | #define QOI_FREE(p) free(p) |
| 308 | #endif |
| 309 | #ifndef QOI_ZEROARR |
| 310 | #define QOI_ZEROARR(a) memset((a),0,sizeof(a)) |
| 311 | #endif |
| 312 | |
| 313 | #define QOI_OP_INDEX 0x00 |
| 314 | #define QOI_OP_DIFF 0x40 |
| 315 | #define QOI_OP_LUMA 0x80 |
| 316 | #define QOI_OP_RUN 0xc0 |
| 317 | #define QOI_OP_RGB 0xfe |
| 318 | #define QOI_OP_RGBA 0xff |
| 319 | |
| 320 | #define QOI_MASK_2 0xc0 |
| 321 | |
| 322 | #define QOI_COLOR_HASH(C) (C.rgba.r*3 + C.rgba.g*5 + C.rgba.b*7 + C.rgba.a*11) |
| 323 | #define QOI_MAGIC \ |
| 324 | (((unsigned int)'q') << 24 | ((unsigned int)'o') << 16 | \ |
| 325 | ((unsigned int)'i') << 8 | ((unsigned int)'f')) |
| 326 | #define QOI_HEADER_SIZE 14 |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | #define QOI_PIXELS_MAX ((unsigned int)400000000) |
| 333 | |
| 334 | typedef union { |
| 335 | struct { unsigned char r, g, b, a; } rgba; |
| 336 | unsigned int v; |
| 337 | } qoi_rgba_t; |
| 338 | |
| 339 | static const unsigned char qoi_padding[8] = {0,0,0,0,0,0,0,1}; |
| 340 | |
| 341 | static void qoi_write_32(unsigned char *bytes, int *p, unsigned int v) { |
| 342 | bytes[(*p)++] = (0xff000000 & v) >> 24; |
| 343 | bytes[(*p)++] = (0x00ff0000 & v) >> 16; |
| 344 | bytes[(*p)++] = (0x0000ff00 & v) >> 8; |
| 345 | bytes[(*p)++] = (0x000000ff & v); |
| 346 | } |
| 347 | |
| 348 | static unsigned int qoi_read_32(const unsigned char *bytes, int *p) { |
| 349 | unsigned int a = bytes[(*p)++]; |
| 350 | unsigned int b = bytes[(*p)++]; |
| 351 | unsigned int c = bytes[(*p)++]; |
| 352 | unsigned int d = bytes[(*p)++]; |
| 353 | return a << 24 | b << 16 | c << 8 | d; |
| 354 | } |
| 355 | |
| 356 | void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) { |
| 357 | int i, max_size, p, run; |
| 358 | int px_len, px_end, px_pos, channels; |
| 359 | unsigned char *bytes; |
| 360 | const unsigned char *pixels; |
| 361 | qoi_rgba_t index[64]; |
| 362 | qoi_rgba_t px, px_prev; |
| 363 | |
| 364 | if ( |
| 365 | data == NULL || out_len == NULL || desc == NULL || |
| 366 | desc->width == 0 || desc->height == 0 || |
| 367 | desc->channels < 3 || desc->channels > 4 || |
| 368 | desc->colorspace > 1 || |
| 369 | desc->height >= QOI_PIXELS_MAX / desc->width |
| 370 | ) { |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | max_size = |
| 375 | desc->width * desc->height * (desc->channels + 1) + |
| 376 | QOI_HEADER_SIZE + sizeof(qoi_padding); |
| 377 | |
| 378 | p = 0; |
| 379 | bytes = (unsigned char *) QOI_MALLOC(max_size); |
| 380 | if (!bytes) { |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | qoi_write_32(bytes, &p, QOI_MAGIC); |
| 385 | qoi_write_32(bytes, &p, desc->width); |
| 386 | qoi_write_32(bytes, &p, desc->height); |
| 387 | bytes[p++] = desc->channels; |
| 388 | bytes[p++] = desc->colorspace; |
| 389 | |
| 390 | |
| 391 | pixels = (const unsigned char *)data; |
| 392 | |
| 393 | QOI_ZEROARR(index); |
| 394 | |
| 395 | run = 0; |
| 396 | px_prev.rgba.r = 0; |
| 397 | px_prev.rgba.g = 0; |
| 398 | px_prev.rgba.b = 0; |
| 399 | px_prev.rgba.a = 255; |
| 400 | px = px_prev; |
| 401 | |
| 402 | px_len = desc->width * desc->height * desc->channels; |
| 403 | px_end = px_len - desc->channels; |
| 404 | channels = desc->channels; |
| 405 | |
| 406 | for (px_pos = 0; px_pos < px_len; px_pos += channels) { |
| 407 | px.rgba.r = pixels[px_pos + 0]; |
| 408 | px.rgba.g = pixels[px_pos + 1]; |
| 409 | px.rgba.b = pixels[px_pos + 2]; |
| 410 | |
| 411 | if (channels == 4) { |
| 412 | px.rgba.a = pixels[px_pos + 3]; |
| 413 | } |
| 414 | |
| 415 | if (px.v == px_prev.v) { |
| 416 | run++; |
| 417 | if (run == 62 || px_pos == px_end) { |
| 418 | bytes[p++] = QOI_OP_RUN | (run - 1); |
| 419 | run = 0; |
| 420 | } |
| 421 | } |
| 422 | else { |
| 423 | int index_pos; |
| 424 | |
| 425 | if (run > 0) { |
| 426 | bytes[p++] = QOI_OP_RUN | (run - 1); |
| 427 | run = 0; |
| 428 | } |
| 429 | |
| 430 | index_pos = QOI_COLOR_HASH(px) & (64 - 1); |
| 431 | |
| 432 | if (index[index_pos].v == px.v) { |
| 433 | bytes[p++] = QOI_OP_INDEX | index_pos; |
| 434 | } |
| 435 | else { |
| 436 | index[index_pos] = px; |
| 437 | |
| 438 | if (px.rgba.a == px_prev.rgba.a) { |
| 439 | signed char vr = px.rgba.r - px_prev.rgba.r; |
| 440 | signed char vg = px.rgba.g - px_prev.rgba.g; |
| 441 | signed char vb = px.rgba.b - px_prev.rgba.b; |
| 442 | |
| 443 | signed char vg_r = vr - vg; |
| 444 | signed char vg_b = vb - vg; |
| 445 | |
| 446 | if ( |
| 447 | vr > -3 && vr < 2 && |
| 448 | vg > -3 && vg < 2 && |
| 449 | vb > -3 && vb < 2 |
| 450 | ) { |
| 451 | bytes[p++] = QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2); |
| 452 | } |
| 453 | else if ( |
| 454 | vg_r > -9 && vg_r < 8 && |
| 455 | vg > -33 && vg < 32 && |
| 456 | vg_b > -9 && vg_b < 8 |
| 457 | ) { |
| 458 | bytes[p++] = QOI_OP_LUMA | (vg + 32); |
| 459 | bytes[p++] = (vg_r + 8) << 4 | (vg_b + 8); |
| 460 | } |
| 461 | else { |
| 462 | bytes[p++] = QOI_OP_RGB; |
| 463 | bytes[p++] = px.rgba.r; |
| 464 | bytes[p++] = px.rgba.g; |
| 465 | bytes[p++] = px.rgba.b; |
| 466 | } |
| 467 | } |
| 468 | else { |
| 469 | bytes[p++] = QOI_OP_RGBA; |
| 470 | bytes[p++] = px.rgba.r; |
| 471 | bytes[p++] = px.rgba.g; |
| 472 | bytes[p++] = px.rgba.b; |
| 473 | bytes[p++] = px.rgba.a; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | px_prev = px; |
| 478 | } |
| 479 | |
| 480 | for (i = 0; i < (int)sizeof(qoi_padding); i++) { |
| 481 | bytes[p++] = qoi_padding[i]; |
| 482 | } |
| 483 | |
| 484 | *out_len = p; |
| 485 | return bytes; |
| 486 | } |
| 487 | |
| 488 | void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) { |
| 489 | const unsigned char *bytes; |
| 490 | unsigned int header_magic; |
| 491 | unsigned char *pixels; |
| 492 | qoi_rgba_t index[64]; |
| 493 | qoi_rgba_t px; |
| 494 | int px_len, chunks_len, px_pos; |
| 495 | int p = 0, run = 0; |
| 496 | |
| 497 | if ( |
| 498 | data == NULL || desc == NULL || |
| 499 | (channels != 0 && channels != 3 && channels != 4) || |
| 500 | size < QOI_HEADER_SIZE + (int)sizeof(qoi_padding) |
| 501 | ) { |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | bytes = (const unsigned char *)data; |
| 506 | |
| 507 | header_magic = qoi_read_32(bytes, &p); |
| 508 | desc->width = qoi_read_32(bytes, &p); |
| 509 | desc->height = qoi_read_32(bytes, &p); |
| 510 | desc->channels = bytes[p++]; |
| 511 | desc->colorspace = bytes[p++]; |
| 512 | |
| 513 | if ( |
| 514 | desc->width == 0 || desc->height == 0 || |
| 515 | desc->channels < 3 || desc->channels > 4 || |
| 516 | desc->colorspace > 1 || |
| 517 | header_magic != QOI_MAGIC || |
| 518 | desc->height >= QOI_PIXELS_MAX / desc->width |
| 519 | ) { |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | if (channels == 0) { |
| 524 | channels = desc->channels; |
| 525 | } |
| 526 | |
| 527 | px_len = desc->width * desc->height * channels; |
| 528 | pixels = (unsigned char *) QOI_MALLOC(px_len); |
| 529 | if (!pixels) { |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | QOI_ZEROARR(index); |
| 534 | px.rgba.r = 0; |
| 535 | px.rgba.g = 0; |
| 536 | px.rgba.b = 0; |
| 537 | px.rgba.a = 255; |
| 538 | |
| 539 | chunks_len = size - (int)sizeof(qoi_padding); |
| 540 | for (px_pos = 0; px_pos < px_len; px_pos += channels) { |
| 541 | if (run > 0) { |
| 542 | run--; |
| 543 | } |
| 544 | else if (p < chunks_len) { |
| 545 | int b1 = bytes[p++]; |
| 546 | |
| 547 | if (b1 == QOI_OP_RGB) { |
| 548 | px.rgba.r = bytes[p++]; |
| 549 | px.rgba.g = bytes[p++]; |
| 550 | px.rgba.b = bytes[p++]; |
| 551 | } |
| 552 | else if (b1 == QOI_OP_RGBA) { |
| 553 | px.rgba.r = bytes[p++]; |
| 554 | px.rgba.g = bytes[p++]; |
| 555 | px.rgba.b = bytes[p++]; |
| 556 | px.rgba.a = bytes[p++]; |
| 557 | } |
| 558 | else if ((b1 & QOI_MASK_2) == QOI_OP_INDEX) { |
| 559 | px = index[b1]; |
| 560 | } |
| 561 | else if ((b1 & QOI_MASK_2) == QOI_OP_DIFF) { |
| 562 | px.rgba.r += ((b1 >> 4) & 0x03) - 2; |
| 563 | px.rgba.g += ((b1 >> 2) & 0x03) - 2; |
| 564 | px.rgba.b += ( b1 & 0x03) - 2; |
| 565 | } |
| 566 | else if ((b1 & QOI_MASK_2) == QOI_OP_LUMA) { |
| 567 | int b2 = bytes[p++]; |
| 568 | int vg = (b1 & 0x3f) - 32; |
| 569 | px.rgba.r += vg - 8 + ((b2 >> 4) & 0x0f); |
| 570 | px.rgba.g += vg; |
| 571 | px.rgba.b += vg - 8 + (b2 & 0x0f); |
| 572 | } |
| 573 | else if ((b1 & QOI_MASK_2) == QOI_OP_RUN) { |
| 574 | run = (b1 & 0x3f); |
| 575 | } |
| 576 | |
| 577 | index[QOI_COLOR_HASH(px) & (64 - 1)] = px; |
| 578 | } |
| 579 | |
| 580 | pixels[px_pos + 0] = px.rgba.r; |
| 581 | pixels[px_pos + 1] = px.rgba.g; |
| 582 | pixels[px_pos + 2] = px.rgba.b; |
| 583 | |
| 584 | if (channels == 4) { |
| 585 | pixels[px_pos + 3] = px.rgba.a; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return pixels; |
| 590 | } |
| 591 | |
| 592 | #ifndef QOI_NO_STDIO |
| 593 | #include <stdio.h> |
| 594 | |
| 595 | int qoi_write(const char *filename, const void *data, const qoi_desc *desc) { |
| 596 | FILE *f = fopen(filename, "wb"); |
| 597 | int size, err; |
| 598 | void *encoded; |
| 599 | |
| 600 | if (!f) { |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | encoded = qoi_encode(data, desc, &size); |
| 605 | if (!encoded) { |
| 606 | fclose(f); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | fwrite(encoded, 1, size, f); |
| 611 | fflush(f); |
| 612 | err = ferror(f); |
| 613 | fclose(f); |
| 614 | |
| 615 | QOI_FREE(encoded); |
| 616 | return err ? 0 : size; |
| 617 | } |
| 618 | |
| 619 | void *qoi_read(const char *filename, qoi_desc *desc, int channels) { |
| 620 | FILE *f = fopen(filename, "rb"); |
| 621 | int size, bytes_read; |
| 622 | void *pixels, *data; |
| 623 | |
| 624 | if (!f) { |
| 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | fseek(f, 0, SEEK_END); |
| 629 | size = ftell(f); |
| 630 | if (size <= 0 || fseek(f, 0, SEEK_SET) != 0) { |
| 631 | fclose(f); |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | data = QOI_MALLOC(size); |
| 636 | if (!data) { |
| 637 | fclose(f); |
| 638 | return NULL; |
| 639 | } |
| 640 | |
| 641 | bytes_read = fread(data, 1, size, f); |
| 642 | fclose(f); |
| 643 | pixels = (bytes_read != size) ? NULL : qoi_decode(data, bytes_read, desc, channels); |
| 644 | QOI_FREE(data); |
| 645 | return pixels; |
| 646 | } |
| 647 | |
| 648 | #endif |
| 649 | #endif |