File size: 35,047 Bytes
ef28ed2 24463b6 ef28ed2 336a9be 24463b6 ef28ed2 336a9be 24463b6 ef28ed2 | 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 | #include "gfx/image_decoder.hpp"
#include "net/http_client.hpp"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <dlfcn.h>
#include <fstream>
#include <sstream>
namespace astra {
namespace {
uint32_t read_be32(const uint8_t* p) {
return (static_cast<uint32_t>(p[0]) << 24) |
(static_cast<uint32_t>(p[1]) << 16) |
(static_cast<uint32_t>(p[2]) << 8) |
static_cast<uint32_t>(p[3]);
}
uint32_t read_le32(const uint8_t* p) {
return static_cast<uint32_t>(p[0]) |
(static_cast<uint32_t>(p[1]) << 8) |
(static_cast<uint32_t>(p[2]) << 16) |
(static_cast<uint32_t>(p[3]) << 24);
}
uint16_t read_le16(const uint8_t* p) {
return static_cast<uint16_t>(p[0] | (p[1] << 8));
}
uint8_t paeth_predictor(int a, int b, int c) {
int p = a + b - c;
int pa = std::abs(p - a);
int pb = std::abs(p - b);
int pc = std::abs(p - c);
if (pa <= pb && pa <= pc) return static_cast<uint8_t>(a);
if (pb <= pc) return static_cast<uint8_t>(b);
return static_cast<uint8_t>(c);
}
} // namespace
DecodedImage ImageDecoder::decode_file(const std::string& path) {
std::ifstream f(path, std::ios::binary);
if (!f) {
DecodedImage img;
img.error = "Cannot open image file: " + path;
return img;
}
std::ostringstream oss;
oss << f.rdbuf();
std::string bytes = oss.str();
return decode_bytes(reinterpret_cast<const uint8_t*>(bytes.data()), bytes.size());
}
DecodedImage ImageDecoder::decode_bytes(const uint8_t* data, size_t len) {
if (!data || len < 8) {
DecodedImage img;
img.error = "Image buffer too small";
return img;
}
static const uint8_t kPngSig[8] = {0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A};
if (std::memcmp(data, kPngSig, 8) == 0) {
return decode_png(data, len);
}
if (data[0] == 0xFF && data[1] == 0xD8) {
return decode_jpeg(data, len);
}
if (len >= 13 && (std::memcmp(data, "GIF87a", 6) == 0 || std::memcmp(data, "GIF89a", 6) == 0)) {
return decode_gif(data, len);
}
if (len >= 16 && std::memcmp(data, "RIFF", 4) == 0 && std::memcmp(data + 8, "WEBP", 4) == 0) {
return decode_webp(data, len);
}
if (data[0] == 'B' && data[1] == 'M' && len >= 54) {
return decode_bmp(data, len);
}
if (data[0] == 'P' && data[1] == '6') {
return decode_ppm(data, len);
}
DecodedImage img;
img.error = "Unsupported image signature";
return img;
}
DecodedImage ImageDecoder::decode_png(const uint8_t* data, size_t len) {
DecodedImage out;
out.format = "PNG";
uint32_t w = 0, h = 0;
uint8_t bit_depth = 0, color_type = 0, interlace = 0;
std::vector<uint8_t> idat;
std::vector<uint8_t> palette;
size_t pos = 8;
while (pos + 12 <= len) {
uint32_t chunk_len = read_be32(data + pos);
const char* type = reinterpret_cast<const char*>(data + pos + 4);
pos += 8;
if (pos + chunk_len + 4 > len) break;
if (std::memcmp(type, "IHDR", 4) == 0 && chunk_len >= 13) {
w = read_be32(data + pos);
h = read_be32(data + pos + 4);
bit_depth = data[pos + 8];
color_type = data[pos + 9];
interlace = data[pos + 12];
} else if (std::memcmp(type, "PLTE", 4) == 0) {
palette.assign(data + pos, data + pos + chunk_len);
} else if (std::memcmp(type, "IDAT", 4) == 0) {
idat.insert(idat.end(), data + pos, data + pos + chunk_len);
} else if (std::memcmp(type, "IEND", 4) == 0) {
break;
}
pos += chunk_len + 4; // skip CRC
}
if (w == 0 || h == 0 || w > 8192 || h > 8192 || bit_depth != 8 || interlace != 0) {
out.error = "Unsupported PNG dimensions/depth/interlace";
return out;
}
int bpp = 0;
switch (color_type) {
case 0: bpp = 1; break; // Grayscale
case 2: bpp = 3; break; // RGB
case 3: bpp = 1; break; // Indexed
case 4: bpp = 2; break; // Grayscale+Alpha
case 6: bpp = 4; break; // RGBA
default: out.error = "Unsupported PNG color type"; return out;
}
std::string inflated;
if (!HttpClient::decompress_gzip_or_zlib(idat.data(), idat.size(), inflated)) {
out.error = "PNG IDAT zlib decompression failed";
return out;
}
size_t stride = static_cast<size_t>(w) * static_cast<size_t>(bpp);
if (inflated.size() < static_cast<size_t>(h) * (stride + 1)) {
out.error = "PNG scanline buffer truncated";
return out;
}
std::vector<uint8_t> recon(static_cast<size_t>(h) * stride, 0);
const uint8_t* src = reinterpret_cast<const uint8_t*>(inflated.data());
for (uint32_t y = 0; y < h; ++y) {
uint8_t filter = src[y * (stride + 1)];
const uint8_t* row_in = src + y * (stride + 1) + 1;
uint8_t* row_out = recon.data() + y * stride;
const uint8_t* prev_row = (y > 0) ? (recon.data() + (y - 1) * stride) : nullptr;
for (size_t i = 0; i < stride; ++i) {
uint8_t raw = row_in[i];
uint8_t a = (i >= static_cast<size_t>(bpp)) ? row_out[i - bpp] : 0;
uint8_t b = prev_row ? prev_row[i] : 0;
uint8_t c = (prev_row && i >= static_cast<size_t>(bpp)) ? prev_row[i - bpp] : 0;
uint8_t val = raw;
switch (filter) {
case 0: val = raw; break;
case 1: val = static_cast<uint8_t>(raw + a); break;
case 2: val = static_cast<uint8_t>(raw + b); break;
case 3: val = static_cast<uint8_t>(raw + ((static_cast<int>(a) + static_cast<int>(b)) >> 1)); break;
case 4: val = static_cast<uint8_t>(raw + paeth_predictor(a, b, c)); break;
default: break;
}
row_out[i] = val;
}
}
out.width = static_cast<int>(w);
out.height = static_cast<int>(h);
out.argb.resize(static_cast<size_t>(w) * static_cast<size_t>(h));
for (uint32_t y = 0; y < h; ++y) {
const uint8_t* row = recon.data() + y * stride;
for (uint32_t x = 0; x < w; ++x) {
uint8_t r = 0, g = 0, b = 0, a = 255;
if (color_type == 6) {
r = row[x * 4 + 0]; g = row[x * 4 + 1]; b = row[x * 4 + 2]; a = row[x * 4 + 3];
} else if (color_type == 2) {
r = row[x * 3 + 0]; g = row[x * 3 + 1]; b = row[x * 3 + 2];
} else if (color_type == 0) {
r = g = b = row[x];
} else if (color_type == 4) {
r = g = b = row[x * 2]; a = row[x * 2 + 1];
} else if (color_type == 3) {
size_t idx = static_cast<size_t>(row[x]) * 3;
if (idx + 2 < palette.size()) {
r = palette[idx]; g = palette[idx + 1]; b = palette[idx + 2];
}
}
out.argb[static_cast<size_t>(y * w + x)] =
(static_cast<uint32_t>(a) << 24) | (static_cast<uint32_t>(r) << 16) |
(static_cast<uint32_t>(g) << 8) | static_cast<uint32_t>(b);
}
}
out.valid = true;
return out;
}
DecodedImage ImageDecoder::decode_bmp(const uint8_t* data, size_t len) {
DecodedImage out;
out.format = "BMP";
uint32_t pixel_off = read_le32(data + 10);
int32_t w = static_cast<int32_t>(read_le32(data + 18));
int32_t h_signed = static_cast<int32_t>(read_le32(data + 22));
uint16_t bpp = read_le16(data + 28);
bool top_down = (h_signed < 0);
int h = std::abs(h_signed);
if (w <= 0 || h <= 0 || w > 8192 || h > 8192 || (bpp != 24 && bpp != 32)) {
out.error = "Unsupported BMP format";
return out;
}
size_t row_stride = ((static_cast<size_t>(w) * (bpp / 8) + 3) & ~size_t(3));
if (pixel_off + row_stride * static_cast<size_t>(h) > len) {
out.error = "Truncated BMP pixel data";
return out;
}
out.width = w;
out.height = h;
out.argb.resize(static_cast<size_t>(w * h));
for (int y = 0; y < h; ++y) {
int src_y = top_down ? y : (h - 1 - y);
const uint8_t* row = data + pixel_off + static_cast<size_t>(src_y) * row_stride;
for (int x = 0; x < w; ++x) {
uint8_t b = row[x * (bpp / 8) + 0];
uint8_t g = row[x * (bpp / 8) + 1];
uint8_t r = row[x * (bpp / 8) + 2];
uint8_t a = (bpp == 32) ? row[x * 4 + 3] : 255;
out.argb[static_cast<size_t>(y * w + x)] =
(static_cast<uint32_t>(a) << 24) | (static_cast<uint32_t>(r) << 16) |
(static_cast<uint32_t>(g) << 8) | static_cast<uint32_t>(b);
}
}
out.valid = true;
return out;
}
DecodedImage ImageDecoder::decode_ppm(const uint8_t* data, size_t len) {
DecodedImage out;
out.format = "PPM";
size_t pos = 2;
auto next_int = [&]() -> int {
while (pos < len) {
if (data[pos] == '#') {
while (pos < len && data[pos] != '\n') ++pos;
} else if (std::isspace(data[pos])) {
++pos;
} else {
break;
}
}
int val = 0;
while (pos < len && std::isdigit(data[pos])) {
val = val * 10 + (data[pos++] - '0');
}
return val;
};
int w = next_int(), h = next_int(), maxv = next_int();
if (pos < len && std::isspace(data[pos])) ++pos;
if (w <= 0 || h <= 0 || maxv != 255 || pos + static_cast<size_t>(w * h * 3) > len) {
out.error = "Invalid PPM P6 image";
return out;
}
out.width = w;
out.height = h;
out.argb.resize(static_cast<size_t>(w * h));
for (int i = 0; i < w * h; ++i) {
uint8_t r = data[pos + i * 3 + 0];
uint8_t g = data[pos + i * 3 + 1];
uint8_t b = data[pos + i * 3 + 2];
out.argb[static_cast<size_t>(i)] = 0xFF000000u | (r << 16) | (g << 8) | b;
}
out.valid = true;
return out;
}
DecodedImage ImageDecoder::decode_jpeg(const uint8_t* data, size_t len) {
DecodedImage out;
out.format = "JPEG";
static constexpr int kZigZag[64] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63
};
struct HuffTable {
bool valid = false;
int counts[16] = {};
std::vector<uint8_t> symbols;
std::vector<uint32_t> codes;
std::vector<uint8_t> lengths;
};
struct JpegComp {
uint8_t id = 0;
int h_samp = 1;
int v_samp = 1;
int qt_id = 0;
int dc_id = 0;
int ac_id = 0;
int dc_pred = 0;
};
uint16_t qt[4][64] = {};
bool qt_valid[4] = {};
HuffTable huff_dc[4];
HuffTable huff_ac[4];
JpegComp comps[4];
int num_comps = 0;
int width = 0, height = 0;
int max_h = 1, max_v = 1;
int restart_interval = 0;
size_t scan_offset = 0;
auto read_be16 = [](const uint8_t* p) -> uint16_t {
return static_cast<uint16_t>((static_cast<uint16_t>(p[0]) << 8) | p[1]);
};
size_t pos = 2;
while (pos + 2 <= len) {
if (data[pos] != 0xFF) { ++pos; continue; }
while (pos < len && data[pos] == 0xFF) ++pos;
if (pos >= len) break;
uint8_t marker = data[pos++];
if (marker == 0xD9) break; // EOI
if (marker == 0x00 || (marker >= 0xD0 && marker <= 0xD7) || marker == 0x01) continue;
if (pos + 2 > len) break;
uint16_t seg_len = read_be16(data + pos);
if (seg_len < 2 || pos + seg_len > len) break;
const uint8_t* seg = data + pos + 2;
size_t slen = seg_len - 2;
if (marker == 0xDB) { // DQT
size_t o = 0;
while (o < slen) {
uint8_t pq_tq = seg[o++];
int prec = pq_tq >> 4;
int tq = pq_tq & 0x0F;
if (tq > 3) break;
if (prec == 0) {
if (o + 64 > slen) break;
for (int i = 0; i < 64; ++i) qt[tq][kZigZag[i]] = seg[o++];
} else {
if (o + 128 > slen) break;
for (int i = 0; i < 64; ++i) {
qt[tq][kZigZag[i]] = read_be16(seg + o);
o += 2;
}
}
qt_valid[tq] = true;
}
} else if (marker == 0xC0) { // SOF0 Baseline DCT
if (slen < 6) { out.error = "Truncated SOF0"; return out; }
uint8_t precision = seg[0];
height = read_be16(seg + 1);
width = read_be16(seg + 3);
num_comps = seg[5];
if (precision != 8 || (num_comps != 1 && num_comps != 3) || slen < static_cast<size_t>(6 + num_comps * 3)) {
out.error = "Unsupported JPEG SOF0 format";
return out;
}
max_h = 1;
max_v = 1;
for (int c = 0; c < num_comps; ++c) {
comps[c].id = seg[6 + c * 3];
uint8_t hv = seg[6 + c * 3 + 1];
comps[c].h_samp = std::max(1, static_cast<int>(hv >> 4));
comps[c].v_samp = std::max(1, static_cast<int>(hv & 0x0F));
comps[c].qt_id = seg[6 + c * 3 + 2] & 3;
max_h = std::max(max_h, comps[c].h_samp);
max_v = std::max(max_v, comps[c].v_samp);
}
} else if (marker == 0xC2) {
out.error = "Progressive JPEG (SOF2) not yet supported";
return out;
} else if (marker == 0xC4) { // DHT
size_t o = 0;
while (o + 17 <= slen) {
uint8_t tc_th = seg[o++];
int tc = tc_th >> 4;
int th = tc_th & 0x0F;
if (th > 3) break;
HuffTable& ht = (tc == 0) ? huff_dc[th] : huff_ac[th];
ht = HuffTable{};
int total_syms = 0;
for (int i = 0; i < 16; ++i) {
ht.counts[i] = seg[o++];
total_syms += ht.counts[i];
}
if (o + static_cast<size_t>(total_syms) > slen) break;
ht.symbols.assign(seg + o, seg + o + total_syms);
o += static_cast<size_t>(total_syms);
uint32_t code = 0;
for (int bits = 1; bits <= 16; ++bits) {
for (int k = 0; k < ht.counts[bits - 1]; ++k) {
ht.codes.push_back(code++);
ht.lengths.push_back(static_cast<uint8_t>(bits));
}
code <<= 1;
}
ht.valid = true;
}
} else if (marker == 0xDD) { // DRI
if (slen >= 2) restart_interval = read_be16(seg);
} else if (marker == 0xDA) { // SOS
if (slen < 1) { out.error = "Invalid SOS"; return out; }
int ns = seg[0];
if (slen < static_cast<size_t>(1 + ns * 2 + 3)) { out.error = "Truncated SOS"; return out; }
for (int i = 0; i < ns; ++i) {
uint8_t cid = seg[1 + i * 2];
uint8_t td_ta = seg[1 + i * 2 + 1];
for (int c = 0; c < num_comps; ++c) {
if (comps[c].id == cid) {
comps[c].dc_id = (td_ta >> 4) & 3;
comps[c].ac_id = td_ta & 3;
}
}
}
scan_offset = pos + seg_len;
break;
}
pos += seg_len;
}
if (width <= 0 || height <= 0 || width > 8192 || height > 8192 || scan_offset == 0 || num_comps == 0) {
out.error = "Invalid or incomplete Baseline JPEG";
return out;
}
// Bit reader for JPEG entropy-coded scan (handles 0xFF 0x00 byte stuffing)
struct BitReader {
const uint8_t* buf;
size_t len;
size_t pos;
uint32_t bit_buf = 0;
int bit_cnt = 0;
uint8_t next_byte() {
if (pos >= len) return 0;
uint8_t b = buf[pos++];
if (b == 0xFF && pos < len) {
uint8_t next = buf[pos];
if (next == 0x00) {
++pos; // stuffed zero
} else if (next >= 0xD0 && next <= 0xD7) {
++pos; // restart marker consumed
return next_byte();
} else {
--pos; // hit EOI or next marker
return 0;
}
}
return b;
}
int read_bit() {
if (bit_cnt == 0) {
bit_buf = next_byte();
bit_cnt = 8;
}
--bit_cnt;
return static_cast<int>((bit_buf >> bit_cnt) & 1u);
}
int read_bits(int n) {
int v = 0;
for (int i = 0; i < n; ++i) v = (v << 1) | read_bit();
return v;
}
int receive_extend(int n) {
if (n == 0) return 0;
int v = read_bits(n);
int half = 1 << (n - 1);
if (v < half) v -= (1 << n) - 1;
return v;
}
int decode_huff(const HuffTable& ht) {
uint32_t code = 0;
size_t idx = 0;
for (int bits = 1; bits <= 16; ++bits) {
code = (code << 1) | static_cast<uint32_t>(read_bit());
int cnt = ht.counts[bits - 1];
for (int k = 0; k < cnt; ++k, ++idx) {
if (ht.codes[idx] == code) return ht.symbols[idx];
}
}
return -1;
}
void align_byte() {
bit_cnt = 0;
bit_buf = 0;
}
} br{data, len, scan_offset};
// Precompute 8x8 cosine table for separable 2D IDCT
static float cos_tbl[8][8];
static bool cos_init = false;
if (!cos_init) {
constexpr float kPi = 3.14159265358979323846f;
for (int x = 0; x < 8; ++x) {
for (int u = 0; u < 8; ++u) {
float alpha = (u == 0) ? 0.35355339f : 0.5f; // 1/(2*sqrt(2)) vs 1/2
cos_tbl[x][u] = alpha * std::cos((2.0f * x + 1.0f) * u * kPi / 16.0f);
}
}
cos_init = true;
}
auto idct_8x8 = [&](const int coef[64], float out_px[64]) {
float tmp[64];
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
float sum = 0.0f;
for (int u = 0; u < 8; ++u) {
sum += static_cast<float>(coef[y * 8 + u]) * cos_tbl[x][u];
}
tmp[y * 8 + x] = sum;
}
}
for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; ++y) {
float sum = 0.0f;
for (int v = 0; v < 8; ++v) {
sum += tmp[v * 8 + x] * cos_tbl[y][v];
}
out_px[y * 8 + x] = sum + 128.0f;
}
}
};
int mcu_w = max_h * 8;
int mcu_h = max_v * 8;
int mcus_x = (width + mcu_w - 1) / mcu_w;
int mcus_y = (height + mcu_h - 1) / mcu_h;
out.width = width;
out.height = height;
out.argb.assign(static_cast<size_t>(width) * height, 0xFF000000u);
// Component block storage per MCU (up to 4x4 blocks per component)
float comp_buf[3][32 * 32];
int mcu_count = 0;
for (int my = 0; my < mcus_y; ++my) {
for (int mx = 0; mx < mcus_x; ++mx) {
if (restart_interval > 0 && mcu_count > 0 && (mcu_count % restart_interval) == 0) {
br.align_byte();
for (int c = 0; c < num_comps; ++c) comps[c].dc_pred = 0;
}
++mcu_count;
for (int c = 0; c < num_comps; ++c) {
JpegComp& cp = comps[c];
const HuffTable& dc_ht = huff_dc[cp.dc_id];
const HuffTable& ac_ht = huff_ac[cp.ac_id];
if (!dc_ht.valid || !ac_ht.valid || !qt_valid[cp.qt_id]) {
out.error = "Missing JPEG Huffman or Quantization table";
return out;
}
int cw = cp.h_samp * 8;
for (int bv = 0; bv < cp.v_samp; ++bv) {
for (int bh = 0; bh < cp.h_samp; ++bh) {
int coef[64] = {};
int s = br.decode_huff(dc_ht);
if (s < 0) s = 0;
int diff = br.receive_extend(s & 0x0F);
cp.dc_pred += diff;
coef[0] = cp.dc_pred * qt[cp.qt_id][0];
int k = 1;
while (k < 64) {
int rs = br.decode_huff(ac_ht);
if (rs <= 0) break; // EOB or error
int run = rs >> 4;
int sz = rs & 0x0F;
if (sz == 0) {
if (run == 15) { k += 16; continue; }
break;
}
k += run;
if (k >= 64) break;
int val = br.receive_extend(sz);
int z = kZigZag[k];
coef[z] = val * qt[cp.qt_id][z];
++k;
}
float block[64];
idct_8x8(coef, block);
for (int by = 0; by < 8; ++by) {
for (int bx = 0; bx < 8; ++bx) {
int py = bv * 8 + by;
int px = bh * 8 + bx;
if (py < 32 && px < 32) {
comp_buf[c][py * cw + px] = block[by * 8 + bx];
}
}
}
}
}
}
// Convert MCU YCbCr -> ARGB32
for (int py = 0; py < mcu_h; ++py) {
int gy = my * mcu_h + py;
if (gy >= height) break;
for (int px = 0; px < mcu_w; ++px) {
int gx = mx * mcu_w + px;
if (gx >= width) break;
int yx = (px * comps[0].h_samp) / max_h;
int yy = (py * comps[0].v_samp) / max_v;
float Y = comp_buf[0][yy * (comps[0].h_samp * 8) + yx];
uint8_t r = 0, g = 0, b = 0;
if (num_comps == 1) {
uint8_t v = static_cast<uint8_t>(std::clamp(std::round(Y), 0.0f, 255.0f));
r = g = b = v;
} else {
int cbx = (px * comps[1].h_samp) / max_h;
int cby = (py * comps[1].v_samp) / max_v;
float Cb = comp_buf[1][cby * (comps[1].h_samp * 8) + cbx] - 128.0f;
int crx = (px * comps[2].h_samp) / max_h;
int cry = (py * comps[2].v_samp) / max_v;
float Cr = comp_buf[2][cry * (comps[2].h_samp * 8) + crx] - 128.0f;
r = static_cast<uint8_t>(std::clamp(std::round(Y + 1.402f * Cr), 0.0f, 255.0f));
g = static_cast<uint8_t>(std::clamp(std::round(Y - 0.344136f * Cb - 0.714136f * Cr), 0.0f, 255.0f));
b = static_cast<uint8_t>(std::clamp(std::round(Y + 1.772f * Cb), 0.0f, 255.0f));
}
out.argb[static_cast<size_t>(gy * width + gx)] =
0xFF000000u | (static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | b;
}
}
}
}
out.valid = true;
return out;
}
DecodedImage ImageDecoder::decode_gif(const uint8_t* data, size_t len) {
DecodedImage out;
out.format = "GIF";
if (!data || len < 14) { out.error = "Truncated GIF header"; return out; }
int canvas_w = read_le16(data + 6);
int canvas_h = read_le16(data + 8);
if (canvas_w <= 0 || canvas_h <= 0 || canvas_w > 4096 || canvas_h > 4096) {
out.error = "Invalid GIF dimensions";
return out;
}
uint8_t packed = data[10];
bool has_gct = (packed & 0x80u) != 0;
size_t gct_entries = has_gct ? (size_t(1) << ((packed & 0x07u) + 1)) : 0;
size_t pos = 13;
std::vector<uint32_t> gct;
if (has_gct) {
if (pos + gct_entries * 3 > len) { out.error = "Truncated GIF GCT"; return out; }
gct.resize(gct_entries);
for (size_t i = 0; i < gct_entries; ++i) {
gct[i] = 0xFF000000u |
(static_cast<uint32_t>(data[pos + i * 3]) << 16) |
(static_cast<uint32_t>(data[pos + i * 3 + 1]) << 8) |
static_cast<uint32_t>(data[pos + i * 3 + 2]);
}
pos += gct_entries * 3;
}
out.width = canvas_w;
out.height = canvas_h;
out.argb.assign(static_cast<size_t>(canvas_w) * static_cast<size_t>(canvas_h), 0x00000000u);
bool has_trans = false;
uint8_t trans_idx = 0;
while (pos < len) {
uint8_t intro = data[pos++];
if (intro == 0x3Bu) break; // Trailer
if (intro == 0x21u) { // Extension block
if (pos >= len) break;
uint8_t label = data[pos++];
if (label == 0xF9u && pos + 5 <= len && data[pos] == 4) {
uint8_t gce_flags = data[pos + 1];
has_trans = (gce_flags & 0x01u) != 0;
trans_idx = data[pos + 4];
}
// Skip sub-blocks
while (pos < len) {
uint8_t sz = data[pos++];
if (sz == 0) break;
if (pos + sz > len) { pos = len; break; }
pos += sz;
}
continue;
}
if (intro != 0x2Cu) continue; // Wait for Image Descriptor
if (pos + 9 > len) { out.error = "Truncated GIF image descriptor"; return out; }
int left = read_le16(data + pos);
int top = read_le16(data + pos + 2);
int iw = read_le16(data + pos + 4);
int ih = read_le16(data + pos + 6);
uint8_t ipacked = data[pos + 8];
pos += 9;
bool has_lct = (ipacked & 0x80u) != 0;
bool interlaced = (ipacked & 0x40u) != 0;
std::vector<uint32_t> lct;
if (has_lct) {
size_t lct_entries = size_t(1) << ((ipacked & 0x07u) + 1);
if (pos + lct_entries * 3 > len) { out.error = "Truncated GIF LCT"; return out; }
lct.resize(lct_entries);
for (size_t i = 0; i < lct_entries; ++i) {
lct[i] = 0xFF000000u |
(static_cast<uint32_t>(data[pos + i * 3]) << 16) |
(static_cast<uint32_t>(data[pos + i * 3 + 1]) << 8) |
static_cast<uint32_t>(data[pos + i * 3 + 2]);
}
pos += lct_entries * 3;
}
const auto& palette = has_lct ? lct : gct;
if (palette.empty() || pos >= len) { out.error = "Missing GIF color table"; return out; }
uint8_t min_code_size = data[pos++];
if (min_code_size < 2 || min_code_size > 11) { out.error = "Invalid GIF LZW code size"; return out; }
// Gather LZW sub-blocks
std::vector<uint8_t> lzw_bytes;
while (pos < len) {
uint8_t sz = data[pos++];
if (sz == 0) break;
if (pos + sz > len) { out.error = "Truncated GIF LZW stream"; return out; }
lzw_bytes.insert(lzw_bytes.end(), data + pos, data + pos + sz);
pos += sz;
}
// Pure-C++20 GIF LZW Decompressor (max 4096 table entries)
const int clear_code = 1 << min_code_size;
const int eoi_code = clear_code + 1;
int code_size = min_code_size + 1;
int next_code = eoi_code + 1;
std::vector<int> prefix(4096, -1);
std::vector<uint8_t> suffix(4096, 0);
for (int i = 0; i < clear_code; ++i) suffix[i] = static_cast<uint8_t>(i);
size_t bit_pos = 0;
const size_t total_bits = lzw_bytes.size() * 8;
auto read_code = [&](int width_bits) -> int {
if (bit_pos + static_cast<size_t>(width_bits) > total_bits) return eoi_code;
int val = 0;
for (int b = 0; b < width_bits; ++b) {
size_t bp = bit_pos + static_cast<size_t>(b);
if ((lzw_bytes[bp >> 3] >> (bp & 7)) & 1u) val |= (1 << b);
}
bit_pos += static_cast<size_t>(width_bits);
return val;
};
std::vector<uint8_t> indices;
const size_t max_pixels = static_cast<size_t>(iw) * static_cast<size_t>(ih);
indices.reserve(std::min<size_t>(max_pixels, 1024 * 1024));
std::vector<uint8_t> stack;
stack.reserve(4096);
int prev_code = -1;
uint8_t first_char = 0;
while (indices.size() < max_pixels) {
int code = read_code(code_size);
if (code == eoi_code) break;
if (code == clear_code) {
code_size = min_code_size + 1;
next_code = eoi_code + 1;
prev_code = -1;
continue;
}
int in_code = code;
stack.clear();
if (code >= next_code) {
if (prev_code < 0 || code > next_code) break; // Corrupt stream guard
stack.push_back(first_char);
code = prev_code;
}
int guard = 0;
while (code >= clear_code && guard < 4096) {
stack.push_back(suffix[code]);
code = prefix[code];
if (code < 0 || code >= 4096) break;
++guard;
}
if (code < 0 || code >= clear_code) break;
first_char = static_cast<uint8_t>(code);
stack.push_back(first_char);
for (auto it = stack.rbegin(); it != stack.rend() && indices.size() < max_pixels; ++it) {
indices.push_back(*it);
}
if (prev_code >= 0 && next_code < 4096) {
prefix[next_code] = prev_code;
suffix[next_code] = first_char;
++next_code;
if (next_code == (1 << code_size) && code_size < 12) ++code_size;
}
prev_code = in_code;
}
// Map decoded indices onto canvas (handling optional interlacing & transparency)
static const int kPassStart[4] = {0, 4, 2, 1};
static const int kPassStep[4] = {8, 8, 4, 2};
size_t src_i = 0;
auto write_row = [&](int ry) {
int gy = top + ry;
for (int rx = 0; rx < iw && src_i < indices.size(); ++rx, ++src_i) {
int gx = left + rx;
if (gy < 0 || gy >= canvas_h || gx < 0 || gx >= canvas_w) continue;
uint8_t idx = indices[src_i];
if (has_trans && idx == trans_idx) continue;
if (idx < palette.size()) {
out.argb[static_cast<size_t>(gy * canvas_w + gx)] = palette[idx];
}
}
};
if (interlaced) {
for (int p = 0; p < 4; ++p) {
for (int ry = kPassStart[p]; ry < ih; ry += kPassStep[p]) write_row(ry);
}
} else {
for (int ry = 0; ry < ih; ++ry) write_row(ry);
}
out.valid = !indices.empty();
break; // First frame decoded
}
return out;
}
DecodedImage ImageDecoder::decode_webp(const uint8_t* data, size_t len) {
DecodedImage out;
out.format = "WEBP";
if (!data || len < 20) { out.error = "Truncated WebP buffer"; return out; }
// Runtime dlopen("libwebp.so.7" / "libwebp.so.6") for full VP8/VP8L/VP8X decoding with zero compile deps
using DecodeRGBAFn = uint8_t* (*)(const uint8_t*, size_t, int*, int*);
using WebPFreeFn = void (*)(void*);
static void* webp_lib = nullptr;
static DecodeRGBAFn fn_decode = nullptr;
static WebPFreeFn fn_free = nullptr;
static bool tried_dl = false;
if (!tried_dl) {
tried_dl = true;
webp_lib = dlopen("libwebp.so.7", RTLD_LAZY);
if (!webp_lib) webp_lib = dlopen("libwebp.so.6", RTLD_LAZY);
if (!webp_lib) webp_lib = dlopen("libwebp.so", RTLD_LAZY);
if (webp_lib) {
fn_decode = reinterpret_cast<DecodeRGBAFn>(dlsym(webp_lib, "WebPDecodeRGBA"));
fn_free = reinterpret_cast<WebPFreeFn>(dlsym(webp_lib, "WebPFree"));
}
}
if (fn_decode) {
int w = 0, h = 0;
uint8_t* rgba = fn_decode(data, len, &w, &h);
if (rgba && w > 0 && h > 0 && w <= 4096 && h <= 4096) {
out.width = w;
out.height = h;
out.argb.resize(static_cast<size_t>(w) * static_cast<size_t>(h));
for (size_t i = 0; i < out.argb.size(); ++i) {
uint8_t r = rgba[i * 4 + 0];
uint8_t g = rgba[i * 4 + 1];
uint8_t b = rgba[i * 4 + 2];
uint8_t a = rgba[i * 4 + 3];
out.argb[i] = (static_cast<uint32_t>(a) << 24) |
(static_cast<uint32_t>(r) << 16) |
(static_cast<uint32_t>(g) << 8) | b;
}
if (fn_free) fn_free(rgba);
else std::free(rgba);
out.valid = true;
return out;
}
if (rgba) {
if (fn_free) fn_free(rgba);
else std::free(rgba);
}
}
// Pure-C++20 fallback: probe VP8X / VP8L / VP8 chunk dimensions cleanly
int w = 0, h = 0;
if (len >= 30 && std::memcmp(data + 12, "VP8X", 4) == 0) {
w = 1 + (data[24] | (data[25] << 8) | (data[26] << 16));
h = 1 + (data[27] | (data[28] << 8) | (data[29] << 16));
} else if (len >= 25 && std::memcmp(data + 12, "VP8L", 4) == 0 && data[20] == 0x2Fu) {
uint32_t bits = read_le32(data + 21);
w = static_cast<int>((bits & 0x3FFFu) + 1);
h = static_cast<int>(((bits >> 14) & 0x3FFFu) + 1);
} else if (len >= 30 && std::memcmp(data + 12, "VP8 ", 4) == 0 &&
data[23] == 0x9Du && data[24] == 0x01u && data[25] == 0x2Au) {
w = read_le16(data + 26) & 0x3FFFu;
h = read_le16(data + 28) & 0x3FFFu;
}
if (w > 0 && h > 0 && w <= 4096 && h <= 4096) {
out.width = w;
out.height = h;
out.error = "libwebp not installed on host (container header parsed: " + std::to_string(w) + "x" + std::to_string(h) + ")";
} else {
out.error = "Invalid WebP bitstream";
}
return out;
}
} // namespace astra
|