Coverage Report

Created: 2026-11-12 08:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libraw/src/decoders/sony_arw6.cpp
Line
Count
Source
1
/* -*- C++ -*-
2
 * Copyright 2026 LibRaw LLC (info@libraw.org)
3
 * Copyright 2026 Jiang Yaogeng ( https://github.com/y-g-jiang )
4
 
5
  Sony Compressed/Compressed HQ file decoder
6
  Kindly provided by Jiang Yaogeng  for inclusion in the library 
7
  via this pull request: https://github.com/LibRaw/LibRaw/pull/824
8
  code generated with assistance from GLM-5
9
10
 LibRaw is free software; you can redistribute it and/or modify
11
 it under the terms of the one of two licenses as you choose:
12
13
1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
14
   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
15
16
2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
17
   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
18
19
 */
20
21
#include "../../internal/dcraw_defs.h"
22
23
#include <algorithm>
24
#include <stdint.h>
25
#include <string.h>
26
#include <vector>
27
28
namespace
29
{
30
const int SONY_ARW6_STREAM_OFFSET = 0x200;
31
const int SONY_ARW6_INTERNAL_BIAS = 2048;
32
const size_t SONY_ARW6_MAX_PLANE_SAMPLES = size_t(128) * 1024 * 1024;
33
const INT64 SONY_ARW6_WORKING_BYTES_PER_TILE_PIXEL = 24;
34
35
static inline int sony_arw6_align_up(int value, int multiple)
36
6
{
37
6
  return ((value + multiple - 1) / multiple) * multiple;
38
6
}
39
40
static inline INT64 sony_arw6_memory_limit_bytes(unsigned max_raw_memory_mb)
41
22
{
42
22
  return INT64(max_raw_memory_mb) * INT64(1024 * 1024);
43
22
}
44
45
static inline uint16_t sony_arw6_be16(const uchar *p)
46
442
{
47
442
  return uint16_t((uint16_t(p[0]) << 8) | p[1]);
48
442
}
49
50
static inline uint32_t sony_arw6_be32(const uchar *p)
51
692
{
52
692
  return (uint32_t(p[0]) << 24) | (uint32_t(p[1]) << 16) |
53
692
         (uint32_t(p[2]) << 8) | uint32_t(p[3]);
54
692
}
55
56
static inline uint32_t sony_arw6_le32(const uchar *p)
57
409
{
58
409
  return (uint32_t(p[3]) << 24) | (uint32_t(p[2]) << 16) |
59
409
         (uint32_t(p[1]) << 8) | uint32_t(p[0]);
60
409
}
61
62
static inline void sony_arw6_require(bool cond)
63
738
{
64
738
  if (!cond)
65
58
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
66
738
}
67
68
static inline int32_t sony_arw6_floor_shift(int64_t x, int bits)
69
0
{
70
0
  if (bits <= 0)
71
0
    return int32_t(x);
72
0
  const int64_t d = int64_t(1) << bits;
73
0
  return int32_t(x >= 0 ? x >> bits : -(((-x) + d - 1) >> bits));
74
0
}
75
76
static inline int32_t sony_arw6_sign16(int32_t x)
77
0
{
78
0
  x &= 0xffff;
79
0
  return x >= 0x8000 ? x - 0x10000 : x;
80
0
}
81
82
static inline int32_t sony_arw6_edge_detail(int32_t x, bool odd_mode)
83
0
{
84
0
  const int32_t signed_half_step = x > 0 ? 1 : (x < 0 ? -1 : 0);
85
0
  const bool is_odd = (x % 2) != 0;
86
0
  const bool use_half_step =
87
0
      odd_mode ? is_odd : (x != 0 && !is_odd);
88
0
  return 2 * x + (use_half_step ? signed_half_step : 0);
89
0
}
90
91
static const ushort sony_arw6_llvc3_curve[4096] = {
92
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
93
  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
94
  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
95
  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
96
  64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
97
  80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
98
  96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
99
  112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
100
  128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
101
  144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
102
  160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
103
  176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
104
  192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
105
  208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
106
  224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
107
  240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
108
  256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271,
109
  272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287,
110
  288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303,
111
  304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319,
112
  320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335,
113
  336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351,
114
  352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367,
115
  368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383,
116
  384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399,
117
  400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415,
118
  416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431,
119
  432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447,
120
  448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463,
121
  464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479,
122
  480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495,
123
  496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511,
124
  512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527,
125
  528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543,
126
  544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559,
127
  560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575,
128
  576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591,
129
  592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607,
130
  608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623,
131
  624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639,
132
  640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655,
133
  656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671,
134
  672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687,
135
  688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703,
136
  704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719,
137
  720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735,
138
  736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751,
139
  752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767,
140
  768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783,
141
  784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799,
142
  800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815,
143
  816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831,
144
  832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847,
145
  848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863,
146
  864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879,
147
  880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895,
148
  896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911,
149
  912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927,
150
  928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943,
151
  944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959,
152
  960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975,
153
  976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991,
154
  992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
155
  1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
156
  1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039,
157
  1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
158
  1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071,
159
  1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087,
160
  1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103,
161
  1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119,
162
  1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135,
163
  1136, 1137, 1138, 1139, 1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 1151,
164
  1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167,
165
  1168, 1169, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1182, 1183,
166
  1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 1195, 1196, 1197, 1198, 1199,
167
  1200, 1201, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215,
168
  1216, 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 1228, 1229, 1230, 1231,
169
  1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247,
170
  1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 1261, 1262, 1263,
171
  1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279,
172
  1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 1294, 1295,
173
  1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1306, 1307, 1308, 1309, 1310, 1311,
174
  1312, 1313, 1314, 1315, 1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 1327,
175
  1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343,
176
  1344, 1345, 1346, 1347, 1348, 1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359,
177
  1360, 1361, 1362, 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1370, 1371, 1372, 1373, 1374, 1375,
178
  1376, 1377, 1378, 1379, 1380, 1381, 1382, 1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391,
179
  1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1400, 1401, 1402, 1403, 1404, 1405, 1406, 1407,
180
  1408, 1409, 1410, 1411, 1412, 1413, 1414, 1415, 1416, 1417, 1418, 1419, 1420, 1421, 1422, 1423,
181
  1424, 1425, 1426, 1427, 1429, 1430, 1431, 1432, 1433, 1434, 1435, 1436, 1438, 1439, 1440, 1441,
182
  1442, 1443, 1444, 1445, 1446, 1447, 1448, 1449, 1450, 1451, 1452, 1453, 1455, 1456, 1457, 1458,
183
  1459, 1460, 1461, 1462, 1463, 1464, 1465, 1466, 1467, 1468, 1469, 1470, 1471, 1472, 1473, 1474,
184
  1475, 1476, 1477, 1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1489, 1490, 1491, 1492,
185
  1493, 1494, 1495, 1496, 1497, 1498, 1499, 1500, 1501, 1502, 1503, 1504, 1505, 1506, 1507, 1508,
186
  1509, 1510, 1511, 1512, 1513, 1514, 1515, 1516, 1517, 1518, 1519, 1520, 1522, 1523, 1524, 1525,
187
  1526, 1527, 1529, 1530, 1531, 1532, 1534, 1535, 1536, 1537, 1538, 1539, 1541, 1542, 1543, 1544,
188
  1545, 1546, 1548, 1549, 1550, 1551, 1553, 1554, 1555, 1556, 1558, 1559, 1561, 1562, 1563, 1565,
189
  1566, 1567, 1568, 1569, 1571, 1572, 1573, 1574, 1575, 1576, 1578, 1579, 1580, 1581, 1583, 1584,
190
  1585, 1586, 1588, 1589, 1590, 1591, 1593, 1594, 1595, 1596, 1598, 1599, 1601, 1602, 1603, 1605,
191
  1606, 1607, 1608, 1609, 1611, 1612, 1613, 1614, 1615, 1616, 1618, 1619, 1621, 1622, 1623, 1625,
192
  1626, 1627, 1629, 1630, 1631, 1632, 1634, 1635, 1636, 1637, 1639, 1640, 1641, 1642, 1644, 1645,
193
  1646, 1647, 1649, 1650, 1651, 1652, 1654, 1655, 1656, 1657, 1659, 1660, 1662, 1663, 1664, 1666,
194
  1667, 1668, 1670, 1671, 1673, 1674, 1675, 1677, 1678, 1680, 1681, 1683, 1685, 1686, 1688, 1689,
195
  1691, 1692, 1694, 1695, 1697, 1698, 1699, 1701, 1702, 1704, 1705, 1707, 1709, 1710, 1712, 1713,
196
  1715, 1716, 1718, 1719, 1720, 1721, 1723, 1724, 1725, 1727, 1728, 1730, 1731, 1733, 1734, 1736,
197
  1737, 1739, 1740, 1742, 1743, 1745, 1746, 1748, 1749, 1750, 1752, 1753, 1755, 1756, 1757, 1759,
198
  1760, 1762, 1763, 1765, 1767, 1768, 1770, 1771, 1773, 1774, 1776, 1777, 1779, 1780, 1781, 1783,
199
  1784, 1786, 1787, 1789, 1791, 1792, 1794, 1795, 1797, 1799, 1801, 1802, 1804, 1806, 1808, 1809,
200
  1811, 1813, 1814, 1816, 1818, 1819, 1821, 1822, 1824, 1826, 1827, 1829, 1831, 1832, 1834, 1835,
201
  1837, 1839, 1841, 1842, 1844, 1846, 1848, 1849, 1851, 1853, 1854, 1856, 1858, 1859, 1861, 1862,
202
  1864, 1866, 1868, 1869, 1871, 1873, 1875, 1876, 1878, 1880, 1881, 1883, 1885, 1886, 1888, 1889,
203
  1891, 1893, 1894, 1896, 1898, 1899, 1901, 1902, 1904, 1906, 1908, 1909, 1911, 1913, 1915, 1916,
204
  1918, 1920, 1922, 1924, 1926, 1927, 1929, 1931, 1933, 1935, 1937, 1938, 1940, 1942, 1944, 1945,
205
  1947, 1949, 1951, 1953, 1955, 1957, 1959, 1961, 1963, 1965, 1967, 1968, 1970, 1972, 1974, 1975,
206
  1977, 1979, 1981, 1983, 1985, 1986, 1988, 1990, 1992, 1994, 1996, 1998, 2000, 2001, 2003, 2005,
207
  2007, 2009, 2011, 2013, 2015, 2016, 2018, 2020, 2022, 2024, 2026, 2027, 2029, 2031, 2033, 2034,
208
  2036, 2038, 2040, 2042, 2044, 2046, 2048, 2050, 2052, 2054, 2056, 2058, 2060, 2062, 2064, 2066,
209
  2068, 2070, 2072, 2074, 2076, 2078, 2080, 2082, 2084, 2086, 2088, 2090, 2092, 2094, 2096, 2098,
210
  2100, 2102, 2104, 2106, 2109, 2111, 2113, 2115, 2117, 2119, 2121, 2123, 2125, 2127, 2129, 2131,
211
  2133, 2135, 2137, 2139, 2141, 2143, 2145, 2147, 2149, 2151, 2153, 2155, 2157, 2159, 2161, 2163,
212
  2165, 2167, 2170, 2172, 2175, 2177, 2179, 2182, 2184, 2186, 2188, 2190, 2193, 2195, 2197, 2199,
213
  2201, 2203, 2206, 2208, 2210, 2212, 2215, 2217, 2219, 2221, 2224, 2226, 2228, 2230, 2233, 2235,
214
  2237, 2239, 2242, 2244, 2247, 2249, 2251, 2254, 2256, 2258, 2261, 2263, 2265, 2267, 2270, 2272,
215
  2274, 2276, 2279, 2281, 2283, 2285, 2288, 2290, 2292, 2294, 2297, 2299, 2302, 2304, 2306, 2309,
216
  2311, 2314, 2316, 2319, 2321, 2324, 2326, 2329, 2331, 2333, 2336, 2338, 2340, 2342, 2345, 2347,
217
  2349, 2352, 2354, 2357, 2359, 2362, 2364, 2367, 2369, 2371, 2374, 2376, 2379, 2381, 2383, 2386,
218
  2388, 2390, 2393, 2395, 2398, 2400, 2402, 2405, 2407, 2410, 2412, 2415, 2417, 2420, 2422, 2425,
219
  2427, 2430, 2432, 2435, 2438, 2440, 2443, 2445, 2448, 2451, 2453, 2456, 2459, 2461, 2464, 2466,
220
  2469, 2472, 2475, 2477, 2480, 2483, 2486, 2488, 2491, 2494, 2496, 2499, 2501, 2504, 2506, 2509,
221
  2511, 2514, 2517, 2519, 2522, 2525, 2528, 2530, 2533, 2536, 2538, 2541, 2543, 2546, 2548, 2551,
222
  2553, 2556, 2559, 2561, 2564, 2567, 2570, 2572, 2575, 2578, 2581, 2584, 2587, 2589, 2592, 2595,
223
  2598, 2601, 2604, 2606, 2609, 2612, 2615, 2617, 2620, 2623, 2626, 2629, 2632, 2634, 2637, 2640,
224
  2643, 2646, 2649, 2652, 2655, 2657, 2660, 2663, 2666, 2669, 2672, 2675, 2678, 2680, 2683, 2686,
225
  2689, 2692, 2695, 2698, 2701, 2704, 2707, 2710, 2713, 2716, 2719, 2722, 2725, 2727, 2730, 2733,
226
  2736, 2739, 2742, 2745, 2749, 2752, 2755, 2758, 2761, 2764, 2767, 2770, 2773, 2776, 2779, 2782,
227
  2785, 2788, 2791, 2794, 2798, 2801, 2804, 2807, 2810, 2813, 2816, 2819, 2823, 2826, 2829, 2832,
228
  2835, 2838, 2841, 2844, 2848, 2851, 2854, 2857, 2860, 2863, 2867, 2870, 2873, 2876, 2880, 2883,
229
  2886, 2889, 2893, 2896, 2899, 2902, 2906, 2909, 2912, 2915, 2919, 2922, 2925, 2928, 2932, 2935,
230
  2938, 2941, 2945, 2948, 2952, 2955, 2958, 2962, 2965, 2968, 2972, 2975, 2979, 2982, 2985, 2989,
231
  2992, 2995, 2999, 3002, 3006, 3009, 3012, 3016, 3019, 3023, 3026, 3030, 3033, 3037, 3040, 3044,
232
  3047, 3051, 3054, 3058, 3061, 3065, 3068, 3072, 3075, 3079, 3082, 3086, 3089, 3093, 3096, 3100,
233
  3103, 3107, 3110, 3114, 3118, 3121, 3125, 3128, 3132, 3136, 3139, 3143, 3146, 3150, 3153, 3157,
234
  3160, 3164, 3168, 3171, 3175, 3179, 3183, 3186, 3190, 3194, 3197, 3201, 3205, 3208, 3212, 3215,
235
  3219, 3223, 3227, 3231, 3235, 3238, 3242, 3246, 3250, 3254, 3258, 3261, 3265, 3269, 3273, 3276,
236
  3280, 3284, 3288, 3292, 3296, 3299, 3303, 3307, 3311, 3315, 3319, 3323, 3327, 3331, 3335, 3339,
237
  3343, 3347, 3351, 3354, 3358, 3362, 3366, 3369, 3373, 3377, 3381, 3385, 3389, 3393, 3397, 3401,
238
  3405, 3409, 3413, 3417, 3422, 3426, 3430, 3434, 3438, 3442, 3446, 3450, 3455, 3459, 3463, 3467,
239
  3471, 3475, 3479, 3483, 3487, 3491, 3495, 3499, 3503, 3507, 3512, 3516, 3520, 3524, 3529, 3533,
240
  3537, 3541, 3546, 3550, 3554, 3558, 3563, 3567, 3571, 3575, 3580, 3584, 3589, 3593, 3597, 3602,
241
  3606, 3610, 3615, 3619, 3624, 3628, 3632, 3637, 3641, 3645, 3650, 3654, 3658, 3662, 3667, 3671,
242
  3675, 3680, 3684, 3689, 3693, 3698, 3702, 3707, 3711, 3716, 3720, 3725, 3729, 3734, 3738, 3743,
243
  3747, 3752, 3756, 3761, 3766, 3770, 3775, 3779, 3784, 3789, 3793, 3798, 3802, 3807, 3811, 3816,
244
  3820, 3825, 3830, 3834, 3839, 3844, 3849, 3853, 3858, 3863, 3868, 3872, 3877, 3882, 3887, 3891,
245
  3896, 3901, 3906, 3910, 3915, 3920, 3925, 3929, 3934, 3939, 3944, 3949, 3954, 3958, 3963, 3968,
246
  3973, 3978, 3983, 3988, 3993, 3997, 4002, 4007, 4012, 4017, 4022, 4027, 4032, 4037, 4042, 4047,
247
  4052, 4057, 4062, 4067, 4072, 4077, 4082, 4087, 4092, 4097, 4102, 4107, 4113, 4118, 4123, 4128,
248
  4133, 4138, 4143, 4148, 4154, 4159, 4164, 4169, 4174, 4179, 4185, 4190, 4195, 4200, 4206, 4211,
249
  4216, 4221, 4227, 4232, 4237, 4242, 4248, 4253, 4258, 4263, 4269, 4274, 4280, 4285, 4290, 4296,
250
  4301, 4306, 4312, 4317, 4323, 4328, 4333, 4339, 4344, 4350, 4355, 4361, 4366, 4372, 4377, 4383,
251
  4388, 4394, 4399, 4405, 4410, 4416, 4421, 4427, 4432, 4438, 4444, 4449, 4455, 4461, 4467, 4472,
252
  4478, 4484, 4489, 4495, 4500, 4506, 4511, 4517, 4522, 4528, 4534, 4540, 4546, 4551, 4557, 4563,
253
  4569, 4575, 4581, 4586, 4592, 4598, 4604, 4609, 4615, 4621, 4627, 4633, 4639, 4644, 4650, 4656,
254
  4662, 4668, 4674, 4680, 4686, 4692, 4698, 4704, 4710, 4716, 4722, 4728, 4734, 4740, 4746, 4752,
255
  4758, 4764, 4771, 4777, 4783, 4789, 4796, 4802, 4808, 4814, 4820, 4826, 4832, 4838, 4844, 4850,
256
  4856, 4862, 4869, 4875, 4882, 4888, 4894, 4901, 4907, 4913, 4920, 4926, 4933, 4939, 4945, 4952,
257
  4958, 4964, 4971, 4977, 4983, 4989, 4996, 5002, 5008, 5015, 5021, 5028, 5034, 5041, 5047, 5054,
258
  5060, 5067, 5073, 5080, 5087, 5093, 5100, 5106, 5113, 5120, 5126, 5133, 5140, 5146, 5153, 5159,
259
  5166, 5173, 5180, 5186, 5193, 5200, 5207, 5213, 5220, 5227, 5234, 5240, 5247, 5254, 5261, 5267,
260
  5274, 5281, 5288, 5295, 5302, 5309, 5316, 5323, 5330, 5337, 5344, 5351, 5358, 5365, 5372, 5379,
261
  5386, 5393, 5400, 5407, 5414, 5421, 5428, 5435, 5442, 5449, 5456, 5463, 5471, 5478, 5485, 5492,
262
  5499, 5506, 5514, 5521, 5528, 5535, 5543, 5550, 5557, 5564, 5572, 5579, 5587, 5594, 5601, 5609,
263
  5616, 5623, 5631, 5638, 5646, 5653, 5660, 5668, 5675, 5683, 5690, 5698, 5705, 5713, 5720, 5728,
264
  5735, 5743, 5751, 5758, 5766, 5774, 5782, 5789, 5797, 5805, 5812, 5820, 5828, 5835, 5843, 5850,
265
  5858, 5866, 5874, 5881, 5889, 5897, 5905, 5912, 5920, 5928, 5936, 5944, 5952, 5959, 5967, 5975,
266
  5983, 5991, 5999, 6007, 6015, 6023, 6031, 6039, 6047, 6055, 6063, 6071, 6079, 6087, 6095, 6103,
267
  6111, 6119, 6128, 6136, 6144, 6152, 6161, 6169, 6177, 6185, 6194, 6202, 6210, 6218, 6227, 6235,
268
  6243, 6251, 6260, 6268, 6277, 6285, 6293, 6302, 6310, 6319, 6327, 6336, 6344, 6353, 6361, 6370,
269
  6378, 6387, 6395, 6404, 6412, 6421, 6429, 6438, 6446, 6455, 6464, 6472, 6481, 6490, 6499, 6507,
270
  6516, 6525, 6534, 6543, 6552, 6560, 6569, 6578, 6587, 6596, 6605, 6613, 6622, 6631, 6640, 6648,
271
  6657, 6666, 6675, 6684, 6694, 6703, 6712, 6721, 6730, 6739, 6748, 6757, 6767, 6776, 6785, 6794,
272
  6803, 6812, 6822, 6831, 6840, 6849, 6859, 6868, 6877, 6886, 6896, 6905, 6914, 6923, 6933, 6942,
273
  6951, 6961, 6970, 6980, 6989, 6999, 7008, 7018, 7027, 7037, 7046, 7056, 7065, 7075, 7084, 7094,
274
  7103, 7113, 7123, 7132, 7142, 7152, 7162, 7171, 7181, 7191, 7201, 7210, 7220, 7230, 7240, 7249,
275
  7259, 7269, 7279, 7289, 7299, 7309, 7319, 7329, 7339, 7349, 7359, 7369, 7379, 7389, 7399, 7409,
276
  7419, 7429, 7440, 7450, 7460, 7470, 7481, 7491, 7501, 7511, 7522, 7532, 7543, 7553, 7563, 7574,
277
  7584, 7594, 7605, 7615, 7626, 7636, 7646, 7657, 7667, 7678, 7688, 7699, 7709, 7720, 7730, 7741,
278
  7751, 7762, 7772, 7783, 7794, 7804, 7815, 7825, 7836, 7847, 7858, 7869, 7880, 7890, 7901, 7912,
279
  7923, 7934, 7945, 7956, 7967, 7977, 7988, 7999, 8010, 8021, 8032, 8043, 8055, 8066, 8077, 8088,
280
  8099, 8110, 8122, 8133, 8144, 8155, 8167, 8178, 8189, 8200, 8212, 8223, 8234, 8245, 8257, 8268,
281
  8279, 8291, 8302, 8314, 8326, 8337, 8349, 8360, 8372, 8384, 8395, 8407, 8418, 8430, 8441, 8453,
282
  8464, 8476, 8488, 8500, 8512, 8523, 8535, 8547, 8559, 8571, 8583, 8595, 8607, 8618, 8630, 8642,
283
  8654, 8666, 8678, 8690, 8702, 8714, 8726, 8738, 8750, 8762, 8775, 8787, 8799, 8811, 8824, 8836,
284
  8848, 8860, 8873, 8885, 8898, 8910, 8922, 8935, 8947, 8960, 8972, 8985, 8997, 9010, 9022, 9035,
285
  9047, 9060, 9072, 9085, 9098, 9110, 9123, 9135, 9148, 9161, 9174, 9187, 9200, 9212, 9225, 9238,
286
  9251, 9264, 9277, 9290, 9303, 9316, 9329, 9342, 9355, 9368, 9381, 9394, 9408, 9421, 9434, 9447,
287
  9460, 9473, 9487, 9500, 9513, 9526, 9540, 9553, 9566, 9580, 9593, 9607, 9620, 9634, 9647, 9661,
288
  9674, 9688, 9701, 9715, 9728, 9742, 9755, 9769, 9782, 9796, 9810, 9824, 9838, 9851, 9865, 9879,
289
  9893, 9907, 9921, 9935, 9949, 9963, 9977, 9991, 10005, 10019, 10033, 10047, 10061, 10075, 10089, 10103,
290
  10117, 10131, 10146, 10160, 10175, 10189, 10203, 10218, 10232, 10247, 10261, 10276, 10290, 10305, 10319, 10334,
291
  10348, 10363, 10377, 10392, 10407, 10421, 10436, 10450, 10465, 10480, 10495, 10509, 10524, 10539, 10554, 10568,
292
  10583, 10598, 10613, 10628, 10643, 10658, 10673, 10688, 10703, 10718, 10734, 10749, 10764, 10779, 10795, 10810,
293
  10825, 10840, 10856, 10871, 10887, 10902, 10917, 10933, 10948, 10964, 10979, 10995, 11010, 11026, 11041, 11057,
294
  11072, 11088, 11104, 11120, 11136, 11151, 11167, 11183, 11199, 11215, 11231, 11247, 11263, 11278, 11294, 11310,
295
  11326, 11342, 11358, 11374, 11391, 11407, 11423, 11439, 11455, 11471, 11488, 11504, 11521, 11537, 11553, 11570,
296
  11586, 11603, 11619, 11636, 11652, 11669, 11685, 11702, 11718, 11735, 11752, 11768, 11785, 11802, 11819, 11835,
297
  11852, 11869, 11886, 11903, 11920, 11937, 11954, 11971, 11988, 12005, 12022, 12039, 12057, 12074, 12091, 12108,
298
  12125, 12142, 12160, 12177, 12195, 12212, 12229, 12247, 12264, 12282, 12299, 12317, 12335, 12352, 12370, 12387,
299
  12405, 12423, 12441, 12458, 12476, 12494, 12512, 12529, 12547, 12565, 12583, 12601, 12619, 12637, 12655, 12673,
300
  12691, 12709, 12728, 12746, 12764, 12782, 12801, 12819, 12837, 12855, 12874, 12892, 12911, 12929, 12947, 12966,
301
  12984, 13003, 13022, 13040, 13059, 13078, 13097, 13115, 13134, 13153, 13172, 13191, 13210, 13228, 13247, 13266,
302
  13285, 13304, 13323, 13342, 13362, 13381, 13400, 13419, 13438, 13457, 13477, 13496, 13516, 13535, 13554, 13574,
303
  13593, 13613, 13632, 13652, 13672, 13691, 13711, 13730, 13750, 13770, 13790, 13810, 13830, 13849, 13869, 13889,
304
  13909, 13929, 13949, 13969, 13990, 14010, 14030, 14050, 14070, 14090, 14111, 14131, 14151, 14171, 14192, 14212,
305
  14232, 14253, 14273, 14294, 14315, 14335, 14356, 14376, 14397, 14418, 14439, 14460, 14481, 14501, 14522, 14543,
306
  14564, 14585, 14606, 14627, 14648, 14669, 14690, 14711, 14732, 14753, 14775, 14796, 14818, 14839, 14860, 14882,
307
  14903, 14925, 14946, 14968, 14990, 15011, 15033, 15054, 15076, 15098, 15120, 15142, 15164, 15185, 15207, 15229,
308
  15251, 15273, 15295, 15317, 15340, 15362, 15384, 15406, 15428, 15450, 15473, 15495, 15518, 15540, 15562, 15585,
309
  15607, 15630, 15653, 15675, 15698, 15721, 15744, 15766, 15789, 15812, 15835, 15858, 15881, 15903, 15926, 15949,
310
  15972, 15995, 16019, 16042, 16065, 16088, 16112, 16135, 16158, 16182, 16205, 16229, 16253, 16276, 16300, 16323,
311
  16347, 16371, 16395, 16418, 16442, 16466, 16490, 16513, 16537, 16561, 16585, 16609, 16634, 16658, 16682, 16706,
312
  16730, 16754, 16779, 16803, 16828, 16852, 16876, 16901, 16925, 16950, 16975, 16999, 17024, 17049, 17074, 17098,
313
  17123, 17148, 17173, 17198, 17223, 17248, 17273, 17298, 17323, 17348, 17374, 17399, 17424, 17449, 17475, 17500,
314
  17525, 17551, 17576, 17602, 17628, 17653, 17679, 17704, 17730, 17756, 17782, 17808, 17834, 17860, 17886, 17912,
315
  17938, 17964, 17991, 18017, 18043, 18069, 18096, 18122, 18148, 18175, 18201, 18228, 18254, 18281, 18307, 18334,
316
  18360, 18387, 18414, 18441, 18468, 18494, 18521, 18548, 18575, 18602, 18630, 18657, 18684, 18711, 18739, 18766,
317
  18793, 18821, 18848, 18876, 18904, 18931, 18959, 18986, 19014, 19042, 19070, 19097, 19125, 19153, 19181, 19208,
318
  19236, 19264, 19293, 19321, 19349, 19377, 19406, 19434, 19462, 19491, 19519, 19548, 19577, 19605, 19634, 19662,
319
  19691, 19720, 19749, 19778, 19807, 19835, 19864, 19893, 19922, 19951, 19981, 20010, 20040, 20069, 20098, 20128,
320
  20157, 20187, 20216, 20246, 20276, 20305, 20335, 20364, 20394, 20424, 20454, 20484, 20514, 20544, 20574, 20604,
321
  20634, 20664, 20695, 20725, 20756, 20786, 20816, 20847, 20877, 20908, 20939, 20969, 21000, 21031, 21062, 21092,
322
  21123, 21154, 21185, 21216, 21247, 21278, 21309, 21340, 21371, 21403, 21434, 21466, 21497, 21529, 21560, 21592,
323
  21623, 21655, 21687, 21719, 21751, 21782, 21814, 21846, 21878, 21910, 21943, 21975, 22007, 22039, 22072, 22104,
324
  22136, 22169, 22202, 22234, 22267, 22300, 22333, 22365, 22398, 22431, 22464, 22497, 22530, 22563, 22596, 22629,
325
  22662, 22696, 22729, 22763, 22796, 22830, 22863, 22897, 22930, 22964, 22998, 23032, 23066, 23099, 23133, 23167,
326
  23201, 23235, 23270, 23304, 23338, 23372, 23407, 23441, 23475, 23510, 23545, 23579, 23614, 23649, 23684, 23718,
327
  23753, 23788, 23823, 23858, 23894, 23929, 23964, 23999, 24034, 24070, 24105, 24141, 24176, 24212, 24247, 24283,
328
  24318, 24354, 24390, 24426, 24462, 24498, 24534, 24570, 24606, 24643, 24679, 24716, 24752, 24789, 24825, 24862,
329
  24898, 24935, 24972, 25009, 25046, 25082, 25119, 25156, 25193, 25230, 25268, 25305, 25342, 25379, 25417, 25454,
330
  25491, 25529, 25567, 25605, 25643, 25680, 25718, 25756, 25794, 25832, 25871, 25909, 25947, 25985, 26024, 26062,
331
  26100, 26139, 26178, 26216, 26255, 26294, 26333, 26371, 26410, 26449, 26488, 26527, 26567, 26606, 26645, 26684,
332
  26723, 26763, 26802, 26842, 26882, 26921, 26961, 27000, 27040, 27080, 27121, 27161, 27201, 27241, 27282, 27322,
333
  27362, 27403, 27443, 27484, 27525, 27565, 27606, 27646, 27687, 27728, 27769, 27810, 27852, 27893, 27934, 27975,
334
  28016, 28058, 28099, 28141, 28183, 28224, 28266, 28307, 28349, 28391, 28433, 28475, 28518, 28560, 28602, 28644,
335
  28686, 28729, 28772, 28814, 28857, 28900, 28943, 28985, 29028, 29071, 29114, 29157, 29201, 29244, 29287, 29330,
336
  29373, 29417, 29461, 29504, 29548, 29592, 29636, 29679, 29723, 29767, 29812, 29856, 29900, 29944, 29989, 30033,
337
  30077, 30122, 30167, 30211, 30256, 30301, 30346, 30390, 30435, 30480, 30526, 30571, 30617, 30662, 30707, 30753,
338
  30798, 30844, 30890, 30936, 30982, 31027, 31073, 31119, 31165, 31212, 31258, 31305, 31351, 31398, 31444, 31491,
339
  31537, 31584, 31631, 31678, 31725, 31772, 31819, 31866, 31913, 31961, 32008, 32056, 32104, 32151, 32199, 32246,
340
  32294, 32342, 32390, 32438, 32487, 32535, 32583, 32631, 32679, 32766, 32777, 32825, 32874, 32923, 32972, 33020,
341
  33069, 33118, 33168, 33217, 33267, 33316, 33365, 33415, 33464, 33514, 33564, 33614, 33664, 33714, 33764, 33814,
342
  33864, 33915, 33965, 34016, 34067, 34117, 34168, 34218, 34269, 34320, 34371, 34422, 34474, 34525, 34576, 34627,
343
  34678, 34730, 34782, 34834, 34886, 34937, 34989, 35041, 35093, 35146, 35198, 35251, 35303, 35356, 35408, 35461,
344
  35513, 35566, 35619, 35672, 35725, 35778, 35831, 35884, 35937, 35991, 36045, 36098, 36152, 36206, 36260, 36313,
345
  36367, 36422, 36476, 36531, 36585, 36640, 36694, 36749, 36803, 36858, 36913, 36968, 37023, 37078, 37133, 37188,
346
  37243, 37299, 37355, 37410, 37466, 37522, 37578, 37633, 37689, 37746, 37802, 37859, 37915, 37972, 38028, 38085,
347
  38141, 38198, 38255, 38312, 38369, 38426, 38483, 38540, 38597, 38655, 38713, 38771, 38829, 38886, 38944, 39002
348
};
349
350
static inline ushort sony_arw6_llvc3_lut_value(int code)
351
0
{
352
0
  if (code < 0)
353
0
    code = 0;
354
0
  if (code > 4095)
355
0
    code = 4095;
356
0
  return sony_arw6_llvc3_curve[code];
357
0
}
358
359
static size_t sony_arw6_checked_plane_samples(int rows, int cols)
360
0
{
361
0
  sony_arw6_require(rows >= 0 && cols >= 0);
362
0
  const uint64_t r = uint64_t(rows);
363
0
  const uint64_t c = uint64_t(cols);
364
0
  sony_arw6_require(c == 0 || r <= uint64_t(~size_t(0)) / c);
365
0
  const uint64_t samples = r * c;
366
0
  sony_arw6_require(samples <= uint64_t(SONY_ARW6_MAX_PLANE_SAMPLES));
367
0
  return size_t(samples);
368
0
}
369
370
struct SonyArw6Plane
371
{
372
  int rows;
373
  int cols;
374
  std::vector<int32_t> data;
375
376
0
  SonyArw6Plane() : rows(0), cols(0) {}
377
  SonyArw6Plane(int r, int c)
378
0
      : rows(r), cols(c), data(sony_arw6_checked_plane_samples(r, c))
379
0
  {
380
0
  }
381
382
0
  int32_t &at(int r, int c) { return data[size_t(r) * size_t(cols) + c]; }
383
  const int32_t &at(int r, int c) const
384
0
  {
385
0
    return data[size_t(r) * size_t(cols) + c];
386
0
  }
387
0
  int32_t *row(int r) { return &data[size_t(r) * size_t(cols)]; }
388
0
  const int32_t *row(int r) const { return &data[size_t(r) * size_t(cols)]; }
389
};
390
391
struct SonyArw6DirectoryEntry
392
{
393
  int group;
394
  int index;
395
  uint32_t start;
396
  uint32_t length;
397
};
398
399
struct SonyArw6StreamInfo
400
{
401
  int index;
402
  uint32_t offset;
403
  uint32_t length;
404
  int tile_x;
405
  int tile_y;
406
  int tile_w;
407
  int tile_h;
408
  int coded_width;
409
  int coded_half_height;
410
  int logical_height;
411
};
412
413
struct SonyArw6PacketRecord
414
{
415
  uint32_t byte_length;
416
  uchar selectors[3];
417
  uint32_t payload_offset;
418
};
419
420
struct SonyArw6Packet
421
{
422
  int type;
423
  int block_count;
424
  uint32_t control_bytes;
425
  uint32_t total_bytes;
426
  const uchar *data;
427
  uint32_t length;
428
  std::vector<SonyArw6PacketRecord> records;
429
};
430
431
static uint32_t sony_arw6_stream_payload_length(const uchar *stream,
432
                                                uint32_t stream_size)
433
44
{
434
44
  sony_arw6_require(stream_size >= 0x80);
435
44
  uint32_t payload = 0;
436
44
  const int offs[5] = {0, 3, 6, 9, 12};
437
264
  for (int g = 0; g < 5; g++)
438
220
    payload +=
439
220
        ((sony_arw6_be32(stream + 0x10 + offs[g]) >> 4) & 0x0ffffff0);
440
44
  sony_arw6_require(payload <= stream_size - 0x80);
441
44
  return 0x80 + payload;
442
44
}
443
444
static bool sony_arw6_parse_stream_header(const uchar *stream,
445
                                          uint32_t stream_size,
446
                                          SonyArw6StreamInfo &info)
447
519
{
448
519
  if (stream_size < 0x80 ||
449
519
      (memcmp(stream, "A000", 4) && memcmp(stream, "0000", 4)))
450
375
    return false;
451
144
  const uint16_t word_c = sony_arw6_be16(stream + 0x0c);
452
144
  const uint16_t word_e = sony_arw6_be16(stream + 0x0e);
453
144
  const int decoded_bits = (word_c >> 4) & 0x3f;
454
144
  const int components = word_e >> 13;
455
144
  const int mode = (word_e >> 10) & 0x03;
456
144
  if (decoded_bits != 16 || components != 3 || mode != 3)
457
67
    return false;
458
77
  info.coded_width = sony_arw6_be16(stream + 8);
459
77
  info.coded_half_height = sony_arw6_be16(stream + 0x0a);
460
77
  info.logical_height = info.coded_half_height * 2;
461
77
  if (info.coded_width <= 0 || info.coded_half_height <= 0 ||
462
71
      (info.coded_width & 15))
463
12
    return false;
464
65
  return true;
465
77
}
466
467
static std::vector<SonyArw6StreamInfo>
468
sony_arw6_find_streams(const std::vector<uchar> &strip, int full_width,
469
                       int full_height)
470
64
{
471
64
  std::vector<SonyArw6StreamInfo> streams;
472
64
  if (full_width <= 0 || full_height <= 0)
473
0
    return streams;
474
64
  const uint32_t strip_size = uint32_t(strip.size());
475
64
  const uchar *base = strip_size ? &strip[0] : 0;
476
64
  if (!base || strip_size < SONY_ARW6_STREAM_OFFSET + 0x80)
477
0
    return streams;
478
479
64
  const uint32_t count = sony_arw6_le32(base);
480
64
  if (count >= 1 && count <= 16)
481
62
  {
482
93
    for (uint32_t index = 0; index < count; index++)
483
69
    {
484
69
      const uint32_t entry = 0x08 + index * 0x18;
485
69
      if (entry + 0x18 > strip_size)
486
0
      {
487
0
        streams.clear();
488
0
        break;
489
0
      }
490
69
      const uint32_t table_offset = sony_arw6_le32(base + entry);
491
69
      const int tile_x = int(sony_arw6_le32(base + entry + 0x08));
492
69
      const int tile_y = int(sony_arw6_le32(base + entry + 0x0c));
493
69
      const int entry_tile_w = int(sony_arw6_le32(base + entry + 0x10));
494
69
      const int entry_tile_h = int(sony_arw6_le32(base + entry + 0x14));
495
69
      const uint32_t start = table_offset ? table_offset : SONY_ARW6_STREAM_OFFSET;
496
69
      if (start > strip_size - 0x80)
497
9
      {
498
9
        streams.clear();
499
9
        break;
500
9
      }
501
60
      const uint32_t search_end =
502
60
          std::min(strip_size - 0x80,
503
60
                   start > UINT32_MAX - uint32_t(0x1000)
504
60
                       ? UINT32_MAX
505
60
                       : start + uint32_t(0x1000));
506
60
      bool found = false;
507
60
      SonyArw6StreamInfo info;
508
487
      for (uint32_t candidate = start; candidate <= search_end;
509
427
           candidate += 0x10)
510
458
      {
511
458
        if (!sony_arw6_parse_stream_header(base + candidate,
512
458
                                           strip_size - candidate, info))
513
417
          continue;
514
41
        uint32_t length =
515
41
            sony_arw6_stream_payload_length(base + candidate,
516
41
                                            strip_size - candidate);
517
41
        if (length <= 0x80 || candidate + length > strip_size)
518
2
          continue;
519
39
        info.index = int(index);
520
39
        info.offset = candidate;
521
39
        info.length = length;
522
39
        info.tile_x = tile_x;
523
39
        info.tile_y = tile_y;
524
39
        info.tile_w = entry_tile_w ? entry_tile_w : info.coded_width;
525
39
        info.tile_h = entry_tile_h ? entry_tile_h : info.logical_height;
526
39
        if (info.tile_x < 0 || info.tile_y < 0 ||
527
33
            info.tile_w != info.coded_width ||
528
32
            info.tile_h != info.logical_height ||
529
30
            info.tile_x > full_width - info.tile_w ||
530
29
            info.tile_y > full_height - info.tile_h)
531
8
          continue;
532
31
        streams.push_back(info);
533
31
        found = true;
534
31
        break;
535
39
      }
536
60
      if (!found)
537
29
      {
538
29
        streams.clear();
539
29
        break;
540
29
      }
541
60
    }
542
62
    if (streams.size() == count)
543
22
      return streams;
544
62
  }
545
546
42
  SonyArw6StreamInfo info;
547
42
  if (sony_arw6_parse_stream_header(base + SONY_ARW6_STREAM_OFFSET,
548
42
                                    strip_size - SONY_ARW6_STREAM_OFFSET,
549
42
                                    info))
550
3
  {
551
3
    info.index = 0;
552
3
    info.offset = SONY_ARW6_STREAM_OFFSET;
553
3
    info.length =
554
3
        sony_arw6_stream_payload_length(base + info.offset,
555
3
                                        strip_size - info.offset);
556
3
    info.tile_x = 0;
557
3
    info.tile_y = 0;
558
3
    info.tile_w = info.coded_width;
559
3
    info.tile_h = info.logical_height;
560
3
    if (info.coded_width == full_width && info.logical_height == full_height)
561
0
      streams.push_back(info);
562
3
  }
563
42
  return streams;
564
64
}
565
566
class SonyArw6HeaderBits
567
{
568
  const uchar *data_;
569
  uint32_t size_;
570
  uint32_t bitpos_;
571
572
public:
573
  SonyArw6HeaderBits(const uchar *data, uint32_t size)
574
0
      : data_(data), size_(size), bitpos_(0)
575
0
  {
576
0
  }
577
578
  uint32_t read(int nbits)
579
0
  {
580
0
    sony_arw6_require(nbits >= 0 && nbits <= 32);
581
0
    sony_arw6_require(uint64_t(bitpos_) + uint32_t(nbits) <=
582
0
                      uint64_t(size_) * 8U);
583
0
    uint32_t out = 0;
584
0
    for (int i = 0; i < nbits; i++)
585
0
    {
586
0
      const uchar byte = data_[bitpos_ >> 3];
587
0
      const uint32_t bit = (byte >> (7 - (bitpos_ & 7))) & 1;
588
0
      out = (out << 1) | bit;
589
0
      bitpos_++;
590
0
    }
591
0
    return out;
592
0
  }
593
};
594
595
class SonyArw6NativeBits
596
{
597
  const uchar *data_;
598
  uint32_t size_;
599
  int64_t ptr_;
600
  uint64_t cur_;
601
  int bit_;
602
  int words_left_;
603
604
public:
605
  int status;
606
607
  SonyArw6NativeBits(const uchar *packet, uint32_t packet_len,
608
                     uint32_t payload_offset, uint32_t byte_length)
609
0
      : data_(packet), size_(packet_len), ptr_(int64_t(payload_offset) - 8),
610
0
        cur_(0), bit_(0),
611
0
        words_left_(((((int)byte_length + 7) / 8) + 1) & ~1), status(0)
612
0
  {
613
0
  }
614
615
  void load_next_word()
616
0
  {
617
0
    words_left_--;
618
0
    if (words_left_ < 0)
619
0
    {
620
0
      status = 2;
621
0
      return;
622
0
    }
623
0
    ptr_ += 8;
624
0
    uint64_t x = 0;
625
0
    for (int i = 0; i < 8; i++)
626
0
    {
627
0
      x <<= 8;
628
0
      const int64_t pos = ptr_ + i;
629
0
      if (pos >= 0 && pos < int64_t(size_))
630
0
        x |= data_[pos];
631
0
    }
632
0
    cur_ = x;
633
0
    bit_ = 64;
634
0
  }
635
636
  uint32_t read_bits(int nbits)
637
0
  {
638
0
    sony_arw6_require(nbits >= 0 && nbits <= 32);
639
0
    uint32_t out = 0;
640
0
    int remaining = nbits;
641
0
    while (remaining > 0)
642
0
    {
643
0
      if (bit_ <= 0)
644
0
      {
645
0
        load_next_word();
646
0
        if (status)
647
0
          return out << remaining;
648
0
      }
649
0
      const int take = std::min(remaining, bit_);
650
0
      bit_ -= take;
651
0
      out = (out << take) |
652
0
            uint32_t((cur_ >> bit_) & ((uint64_t(1) << take) - 1));
653
0
      remaining -= take;
654
0
    }
655
0
    return out;
656
0
  }
657
658
  int read_unary_zeros_plus_one()
659
0
  {
660
0
    int zeros = 0;
661
0
    while (!status)
662
0
    {
663
0
      const uint32_t bit = read_bits(1);
664
0
      if (status)
665
0
        break;
666
0
      if (bit)
667
0
        return zeros + 1;
668
0
      zeros++;
669
0
    }
670
0
    return zeros + 1;
671
0
  }
672
};
673
674
static int sony_arw6_update_width(SonyArw6NativeBits &br, int wstate)
675
0
{
676
0
  if (br.read_bits(1) == 0 || br.status)
677
0
    return wstate;
678
0
  if (br.read_bits(1) == 0 || br.status)
679
0
    return wstate + br.read_unary_zeros_plus_one();
680
681
0
  for (int zeros = 0; zeros < std::max(0, wstate - 1); zeros++)
682
0
  {
683
0
    if (br.read_bits(1))
684
0
      return wstate - (zeros + 1);
685
0
  }
686
0
  return 0;
687
0
}
688
689
static int sony_arw6_read_zero_run(SonyArw6NativeBits &br, int remaining)
690
0
{
691
0
  if (remaining <= 1)
692
0
    return remaining;
693
0
  int max_prefix = 0;
694
0
  for (int x = remaining - 1; x; x >>= 1)
695
0
    max_prefix++;
696
0
  int zeros = 0;
697
0
  while (zeros < max_prefix)
698
0
  {
699
0
    if (br.read_bits(1))
700
0
      break;
701
0
    if (br.status)
702
0
      return remaining;
703
0
    zeros++;
704
0
  }
705
0
  if (zeros >= max_prefix)
706
0
    return remaining;
707
0
  const int base = 1 << zeros;
708
0
  if (base >= remaining)
709
0
    return remaining;
710
0
  const int extra = zeros ? int(br.read_bits(zeros)) : 0;
711
0
  return std::min(base + extra, remaining);
712
0
}
713
714
static void sony_arw6_decode_coeff_group(SonyArw6NativeBits &br, int nbits,
715
                                         int shift, int32_t vals[4])
716
0
{
717
0
  for (int i = 0; i < 4; i++)
718
0
  {
719
0
    uint32_t x = nbits > 0 ? br.read_bits(nbits) : 0;
720
0
    int32_t v = int32_t(x);
721
0
    if (shift > 0 && x > 0)
722
0
      v = int32_t(((uint64_t(2) * x + 1) << (shift - 1)) - (x & 1));
723
0
    vals[i] = v;
724
0
  }
725
0
}
726
727
static void sony_arw6_apply_signs(SonyArw6NativeBits &br, int32_t vals[4])
728
0
{
729
0
  for (int i = 0; i < 4; i++)
730
0
    if (vals[i] > 0)
731
0
    {
732
0
      const uint32_t bit = br.read_bits(1);
733
0
      vals[i] = vals[i] - 2 * vals[i] * int32_t(bit);
734
0
    }
735
0
}
736
737
static void sony_arw6_decode_component(SonyArw6NativeBits &br, int groups,
738
                                       int shift, int32_t *dst, int row_width)
739
0
{
740
0
  int width_state = sony_arw6_update_width(br, 0);
741
0
  int gi = 0;
742
0
  int out = 0;
743
0
  while (gi < groups)
744
0
  {
745
0
    if (br.status || width_state > 0x13)
746
0
    {
747
0
      br.status = br.status ? br.status : 1;
748
0
      while (out < row_width)
749
0
        dst[out++] = 0;
750
0
      return;
751
0
    }
752
0
    if (width_state == 0)
753
0
    {
754
0
      const int run = sony_arw6_read_zero_run(br, groups - gi);
755
0
      for (int r = 0; r < run; r++)
756
0
        for (int k = 0; k < 4 && out < row_width; k++)
757
0
          dst[out++] = 0;
758
0
      gi += run;
759
0
      if (gi >= groups)
760
0
        break;
761
0
      width_state = br.read_unary_zeros_plus_one();
762
0
      continue;
763
0
    }
764
765
0
    int32_t vals[4];
766
0
    sony_arw6_decode_coeff_group(br, width_state, shift, vals);
767
0
    const int next_width =
768
0
        gi + 1 < groups ? sony_arw6_update_width(br, width_state) : width_state;
769
0
    sony_arw6_apply_signs(br, vals);
770
0
    for (int k = 0; k < 4 && out < row_width; k++)
771
0
      dst[out++] = vals[k];
772
0
    width_state = next_width;
773
0
    gi++;
774
0
  }
775
0
  while (out < row_width)
776
0
    dst[out++] = 0;
777
0
}
778
779
static int sony_arw6_row_multiplier(int group)
780
0
{
781
0
  if (group == 4)
782
0
    return 8;
783
0
  if (group == 0)
784
0
    return 1;
785
0
  return 1 << (group - 1);
786
0
}
787
788
static int sony_arw6_infer_packet_width(int group, int packet_type,
789
                                        int mosaic_width)
790
0
{
791
0
  sony_arw6_require(mosaic_width > 0 && (mosaic_width & 15) == 0);
792
0
  if (packet_type == 1)
793
0
    return group == 4 ? mosaic_width / 2 : mosaic_width / 16;
794
0
  if (packet_type == 3 && group >= 1 && group <= 3)
795
0
    return mosaic_width / (1 << (5 - group));
796
0
  throw LIBRAW_EXCEPTION_IO_CORRUPT;
797
0
}
798
799
static int sony_arw6_expected_packet_rows(int group, int coded_height)
800
0
{
801
0
  sony_arw6_require(group >= 0 && group <= 4);
802
0
  sony_arw6_require(coded_height > 0 && coded_height <= 0x7fff);
803
0
  const int padded_height = sony_arw6_align_up(coded_height, 16);
804
0
  return (padded_height / 16 + 1) * sony_arw6_row_multiplier(group);
805
0
}
806
807
static std::vector<SonyArw6DirectoryEntry>
808
sony_arw6_parse_directory(const uchar *stream, uint32_t stream_size)
809
21
{
810
21
  sony_arw6_require(stream_size >= 0x80);
811
21
  std::vector<SonyArw6DirectoryEntry> entries;
812
21
  uint32_t group_lengths[5];
813
21
  const int offs[5] = {0, 3, 6, 9, 12};
814
126
  for (int g = 0; g < 5; g++)
815
105
    group_lengths[g] =
816
105
        ((sony_arw6_be32(stream + 0x10 + offs[g]) >> 4) & 0x0ffffff0);
817
818
21
  uint32_t pos = 0x30;
819
21
  uint32_t base = 0;
820
92
  for (int group = 0; group < 5; group++)
821
71
  {
822
71
    sony_arw6_require(pos + 0x10 <= stream_size);
823
71
    const int n_entries = stream[pos] & 0x0f;
824
71
    sony_arw6_require(n_entries >= 0 && n_entries <= 9);
825
71
    uint32_t values[9] = {0};
826
406
    for (int i = 0; i < 5; i++)
827
335
      values[i] = (sony_arw6_be32(stream + pos + offs[i]) & 0x00ffffff) << 4;
828
71
    uint32_t consumed = 0x10;
829
71
    if (n_entries >= 5)
830
9
    {
831
9
      sony_arw6_require(pos + 0x20 <= stream_size);
832
41
      for (int i = 0; i < 4; i++)
833
32
        values[5 + i] =
834
32
            (sony_arw6_be32(stream + pos + 0x10 + offs[i]) & 0x00ffffff)
835
32
            << 4;
836
9
      consumed = 0x20;
837
9
    }
838
839
71
    uint32_t local = 0;
840
152
    for (int index = 0; index < n_entries; index++)
841
81
    {
842
81
      SonyArw6DirectoryEntry e;
843
81
      e.group = group;
844
81
      e.index = index;
845
81
      sony_arw6_require(base <= UINT32_MAX - local &&
846
81
                        0x80 <= UINT32_MAX - base - local);
847
81
      e.start = 0x80 + base + local;
848
81
      e.length = values[index];
849
81
      sony_arw6_require(e.start <= stream_size &&
850
81
                        e.length <= stream_size - e.start);
851
81
      entries.push_back(e);
852
81
      sony_arw6_require(local <= UINT32_MAX - e.length);
853
81
      local += e.length;
854
81
    }
855
71
    sony_arw6_require(local <= group_lengths[group]);
856
71
    sony_arw6_require(base <= UINT32_MAX - group_lengths[group]);
857
71
    base += group_lengths[group];
858
71
    pos += consumed;
859
71
  }
860
21
  return entries;
861
21
}
862
863
static SonyArw6DirectoryEntry
864
sony_arw6_find_entry(const std::vector<SonyArw6DirectoryEntry> &entries,
865
                     int group, int index)
866
6
{
867
18
  for (size_t i = 0; i < entries.size(); i++)
868
13
    if (entries[i].group == group && entries[i].index == index)
869
1
      return entries[i];
870
5
  throw LIBRAW_EXCEPTION_IO_CORRUPT;
871
6
}
872
873
static SonyArw6Packet sony_arw6_parse_packet(const uchar *data, uint32_t length)
874
1
{
875
1
  sony_arw6_require(length >= 16);
876
1
  SonyArw6HeaderBits br(data, length);
877
1
  const uint32_t control_count = br.read(16);
878
1
  const uint32_t extra_count = br.read(24);
879
1
  const uint32_t tag4 = br.read(4);
880
1
  const uint32_t reserved4 = br.read(4);
881
1
  const uint32_t type2 = br.read(2);
882
1
  const uint32_t control_words = br.read(6);
883
1
  const uint32_t block_count = br.read(16);
884
1
  const uint32_t width_marker = br.read(8);
885
1
  const uint32_t reserved8 = br.read(8);
886
1
  for (int i = 0; i < 5; i++)
887
0
    sony_arw6_require(br.read(8) == 0);
888
889
1
  sony_arw6_require(tag4 == 4 && reserved4 == 0 && control_words == 0);
890
1
  sony_arw6_require(type2 == 1 || type2 == 3);
891
1
  sony_arw6_require(block_count > 0 && block_count <= 30000);
892
1
  sony_arw6_require(width_marker == 0x10 && reserved8 == 0);
893
894
1
  SonyArw6Packet packet;
895
1
  packet.type = int(type2);
896
1
  packet.block_count = int(block_count);
897
1
  const uint64_t control_bytes = (uint64_t(control_count) + 1U) << 4;
898
1
  const uint64_t total_bytes =
899
1
      (uint64_t(control_count) + 1U + extra_count) << 4;
900
1
  sony_arw6_require(control_bytes <= UINT32_MAX &&
901
0
                    total_bytes <= UINT32_MAX);
902
1
  packet.control_bytes = uint32_t(control_bytes);
903
1
  packet.total_bytes = uint32_t(total_bytes);
904
1
  packet.data = data;
905
1
  packet.length = length;
906
1
  sony_arw6_require(packet.total_bytes == length);
907
1
  sony_arw6_require(packet.control_bytes <= length);
908
909
1
  uint32_t cursor = packet.control_bytes;
910
1
  packet.records.reserve(block_count);
911
1
  for (uint32_t i = 0; i < block_count; i++)
912
0
  {
913
0
    SonyArw6PacketRecord rec;
914
0
    rec.byte_length = br.read(16);
915
0
    memset(rec.selectors, 0, sizeof(rec.selectors));
916
0
    for (uint32_t j = 0; j < type2; j++)
917
0
      rec.selectors[j] = uchar(br.read(4));
918
0
    rec.payload_offset = cursor;
919
0
    sony_arw6_require(rec.byte_length <= length &&
920
0
                      cursor <= length - rec.byte_length);
921
0
    cursor += rec.byte_length;
922
0
    packet.records.push_back(rec);
923
0
  }
924
1
  sony_arw6_require(cursor <= packet.total_bytes);
925
1
  return packet;
926
1
}
927
928
static std::vector<SonyArw6Plane>
929
sony_arw6_decode_packet_arrays(const uchar *stream, uint32_t stream_size,
930
                               const std::vector<SonyArw6DirectoryEntry> &dir,
931
                               int mosaic_width, int coded_height, int group,
932
                               int index)
933
6
{
934
6
  const SonyArw6DirectoryEntry e = sony_arw6_find_entry(dir, group, index);
935
6
  const SonyArw6Packet p = sony_arw6_parse_packet(stream + e.start, e.length);
936
6
  const int components = p.type == 1 ? 1 : 3;
937
6
  const int row_width = sony_arw6_infer_packet_width(group, p.type, mosaic_width);
938
6
  const int row_multiplier = sony_arw6_row_multiplier(group);
939
6
  const int rows = p.block_count * row_multiplier;
940
6
  const int expected_rows = sony_arw6_expected_packet_rows(group, coded_height);
941
6
  const int groups_per_row = (row_width + 3) / 4;
942
6
  sony_arw6_require(row_width > 0 && groups_per_row > 0 && rows > 0);
943
6
  if (rows != expected_rows)
944
0
    sony_arw6_require((coded_height & 15) &&
945
0
                      expected_rows - rows == row_multiplier);
946
6
  sony_arw6_require(size_t(p.block_count) <=
947
6
                    (stream_size / 16U + 1U)); /* weak corruption guard */
948
949
6
  std::vector<SonyArw6Plane> planes;
950
6
  planes.reserve(components);
951
6
  for (int c = 0; c < components; c++)
952
0
    planes.push_back(SonyArw6Plane(expected_rows, row_width));
953
954
6
  for (int ri = 0; ri < p.block_count; ri++)
955
0
  {
956
0
    const SonyArw6PacketRecord &rec = p.records[ri];
957
0
    SonyArw6NativeBits bits(p.data, p.length, rec.payload_offset,
958
0
                            rec.byte_length);
959
0
    for (int ci = 0; ci < components; ci++)
960
0
    {
961
0
      const int shift = ci < p.type ? rec.selectors[ci] : 0;
962
0
      for (int rm = 0; rm < row_multiplier; rm++)
963
0
      {
964
0
        int32_t *dst = planes[ci].row(ri * row_multiplier + rm);
965
0
        if (rec.byte_length <= 0)
966
0
          memset(dst, 0, sizeof(int32_t) * size_t(row_width));
967
0
        else
968
0
          sony_arw6_decode_component(bits, groups_per_row, shift, dst, row_width);
969
0
      }
970
0
    }
971
0
  }
972
6
  return planes;
973
6
}
974
975
static SonyArw6Plane sony_arw6_integrate_type1(const SonyArw6Plane &coeffs,
976
                                               int rows, int dc_offset)
977
0
{
978
0
  sony_arw6_require(rows > 0 && rows <= coeffs.rows);
979
0
  SonyArw6Plane out(rows, coeffs.cols);
980
0
  for (int y = 0; y < rows; y++)
981
0
  {
982
0
    int32_t acc = sony_arw6_sign16(coeffs.at(y, 0)) * 2;
983
0
    out.at(y, 0) = acc / 2 + dc_offset;
984
0
    for (int x = 1; x < coeffs.cols; x++)
985
0
    {
986
0
      acc += sony_arw6_sign16(coeffs.at(y, x)) * 2;
987
0
      out.at(y, x) = acc / 2 + dc_offset;
988
0
    }
989
0
  }
990
0
  return out;
991
0
}
992
993
static SonyArw6Plane sony_arw6_head_rows(const SonyArw6Plane &src, int rows)
994
0
{
995
0
  sony_arw6_require(rows >= 0 && rows <= src.rows);
996
0
  SonyArw6Plane out(rows, src.cols);
997
0
  if (rows)
998
0
    memcpy(&out.data[0], &src.data[0],
999
0
           sizeof(int32_t) * size_t(rows) * size_t(src.cols));
1000
0
  return out;
1001
0
}
1002
1003
static SonyArw6Plane sony_arw6_inv53_axis0(const SonyArw6Plane &low,
1004
                                           const SonyArw6Plane &high)
1005
0
{
1006
0
  sony_arw6_require(low.rows == high.rows && low.cols == high.cols);
1007
0
  SonyArw6Plane lo2(low.rows, low.cols);
1008
0
  for (int y = 0; y < low.rows; y++)
1009
0
    for (int x = 0; x < low.cols; x++)
1010
0
    {
1011
0
      const int32_t hp = high.at(y ? y - 1 : 0, x);
1012
0
      const int32_t hc = high.at(y, x);
1013
0
      lo2.at(y, x) = low.at(y, x) - sony_arw6_floor_shift(hp + hc + 2, 2);
1014
0
    }
1015
1016
0
  SonyArw6Plane out(low.rows * 2, low.cols);
1017
0
  for (int y = 0; y < low.rows; y++)
1018
0
    for (int x = 0; x < low.cols; x++)
1019
0
    {
1020
0
      const int32_t ln = lo2.at(y + 1 < low.rows ? y + 1 : y, x);
1021
0
      const int32_t hi2 =
1022
0
          high.at(y, x) + sony_arw6_floor_shift(lo2.at(y, x) + ln, 1);
1023
0
      out.at(y * 2, x) = lo2.at(y, x);
1024
0
      out.at(y * 2 + 1, x) = hi2;
1025
0
    }
1026
0
  return out;
1027
0
}
1028
1029
static SonyArw6Plane sony_arw6_inv53_axis1(const SonyArw6Plane &low,
1030
                                           const SonyArw6Plane &high)
1031
0
{
1032
0
  sony_arw6_require(low.rows == high.rows && low.cols == high.cols);
1033
0
  SonyArw6Plane lo2(low.rows, low.cols);
1034
0
  for (int y = 0; y < low.rows; y++)
1035
0
    for (int x = 0; x < low.cols; x++)
1036
0
    {
1037
0
      const int32_t hp = high.at(y, x ? x - 1 : 0);
1038
0
      const int32_t hc = high.at(y, x);
1039
0
      lo2.at(y, x) = low.at(y, x) - sony_arw6_floor_shift(hp + hc + 2, 2);
1040
0
    }
1041
1042
0
  SonyArw6Plane out(low.rows, low.cols * 2);
1043
0
  for (int y = 0; y < low.rows; y++)
1044
0
    for (int x = 0; x < low.cols; x++)
1045
0
    {
1046
0
      const int32_t ln = lo2.at(y, x + 1 < low.cols ? x + 1 : x);
1047
0
      const int32_t hi2 =
1048
0
          high.at(y, x) + sony_arw6_floor_shift(lo2.at(y, x) + ln, 1);
1049
0
      out.at(y, x * 2) = lo2.at(y, x);
1050
0
      out.at(y, x * 2 + 1) = hi2;
1051
0
    }
1052
0
  return out;
1053
0
}
1054
1055
static SonyArw6Plane sony_arw6_inv53_axis0_high_leading(
1056
    const SonyArw6Plane &low, const SonyArw6Plane &high)
1057
0
{
1058
0
  sony_arw6_require(low.cols == high.cols);
1059
0
  sony_arw6_require(high.rows == low.rows || high.rows == low.rows + 1);
1060
0
  SonyArw6Plane lo2(low.rows, low.cols);
1061
0
  SonyArw6Plane hi2(high.rows, high.cols);
1062
1063
0
  if (high.rows == low.rows + 1)
1064
0
  {
1065
0
    for (int y = 0; y < low.rows; y++)
1066
0
      for (int x = 0; x < low.cols; x++)
1067
0
        lo2.at(y, x) =
1068
0
            low.at(y, x) -
1069
0
            sony_arw6_floor_shift(high.at(y, x) + high.at(y + 1, x) + 2, 2);
1070
1071
0
    for (int x = 0; x < high.cols; x++)
1072
0
    {
1073
0
      hi2.at(0, x) = high.at(0, x) + lo2.at(0, x);
1074
0
      hi2.at(high.rows - 1, x) = high.at(high.rows - 1, x) +
1075
0
                                 lo2.at(low.rows - 1, x);
1076
0
    }
1077
0
    for (int y = 1; y < high.rows - 1; y++)
1078
0
      for (int x = 0; x < high.cols; x++)
1079
0
        hi2.at(y, x) = high.at(y, x) +
1080
0
                       sony_arw6_floor_shift(lo2.at(y - 1, x) +
1081
0
                                                 lo2.at(y, x),
1082
0
                                             1);
1083
1084
0
    SonyArw6Plane out(low.rows * 2 + 1, low.cols);
1085
0
    for (int y = 0; y < low.rows; y++)
1086
0
    {
1087
0
      memcpy(out.row(y * 2), hi2.row(y), sizeof(int32_t) * size_t(low.cols));
1088
0
      memcpy(out.row(y * 2 + 1), lo2.row(y),
1089
0
             sizeof(int32_t) * size_t(low.cols));
1090
0
    }
1091
0
    memcpy(out.row(out.rows - 1), hi2.row(hi2.rows - 1),
1092
0
           sizeof(int32_t) * size_t(low.cols));
1093
0
    return out;
1094
0
  }
1095
1096
0
  for (int y = 0; y < low.rows; y++)
1097
0
    for (int x = 0; x < low.cols; x++)
1098
0
    {
1099
0
      const int next_y = y + 1 < high.rows ? y + 1 : y;
1100
0
      lo2.at(y, x) =
1101
0
          low.at(y, x) -
1102
0
          sony_arw6_floor_shift(high.at(y, x) + high.at(next_y, x) + 2, 2);
1103
0
    }
1104
0
  for (int x = 0; x < high.cols; x++)
1105
0
    hi2.at(0, x) = high.at(0, x) + lo2.at(0, x);
1106
0
  for (int y = 1; y < high.rows; y++)
1107
0
    for (int x = 0; x < high.cols; x++)
1108
0
      hi2.at(y, x) =
1109
0
          high.at(y, x) +
1110
0
          sony_arw6_floor_shift(lo2.at(y - 1, x) + lo2.at(y, x), 1);
1111
1112
0
  SonyArw6Plane out(low.rows * 2, low.cols);
1113
0
  for (int y = 0; y < low.rows; y++)
1114
0
  {
1115
0
    memcpy(out.row(y * 2), hi2.row(y), sizeof(int32_t) * size_t(low.cols));
1116
0
    memcpy(out.row(y * 2 + 1), lo2.row(y),
1117
0
           sizeof(int32_t) * size_t(low.cols));
1118
0
  }
1119
0
  return out;
1120
0
}
1121
1122
static SonyArw6Plane sony_arw6_synthesize_level(const SonyArw6Plane &ll,
1123
                                                const SonyArw6Plane &sub0,
1124
                                                const SonyArw6Plane &sub1,
1125
                                                const SonyArw6Plane &sub2)
1126
0
{
1127
0
  const int h = ll.rows;
1128
0
  const int w = ll.cols;
1129
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1130
0
  sony_arw6_require(sub0.rows >= h + 1 && sub1.rows >= h + 1 &&
1131
0
                    sub2.rows >= h + 1);
1132
0
  SonyArw6Plane lh(h, w), hh(h, w);
1133
0
  for (int y = 0; y < h - 1; y++)
1134
0
    for (int x = 0; x < w; x++)
1135
0
    {
1136
0
      lh.at(y, x) = sub1.at(y + 1, x);
1137
0
      hh.at(y, x) = sub2.at(y + 1, x);
1138
0
    }
1139
0
  for (int x = 0; x < w; x++)
1140
0
  {
1141
0
    lh.at(h - 1, x) = sub0.at(h, x);
1142
0
    hh.at(h - 1, x) = sub1.at(h, x);
1143
0
  }
1144
0
  SonyArw6Plane low_horizontal = sony_arw6_inv53_axis0(ll, lh);
1145
0
  SonyArw6Plane high_horizontal =
1146
0
      sony_arw6_inv53_axis0(sony_arw6_head_rows(sub0, h), hh);
1147
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1148
0
}
1149
1150
static SonyArw6Plane sony_arw6_synthesize_guard_group1(
1151
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1152
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2)
1153
0
{
1154
0
  const int h = ll.rows;
1155
0
  const int w = ll.cols;
1156
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1157
0
  sony_arw6_require(sub0.rows >= h + 2 && sub1.rows >= h + 2 &&
1158
0
                    sub2.rows >= h + 2);
1159
1160
0
  SonyArw6Plane lh(h + 1, w), hh(h + 1, w), hl(h, w);
1161
0
  for (int y = 0; y < h; y++)
1162
0
    for (int x = 0; x < w; x++)
1163
0
    {
1164
0
      hl.at(y, x) = sub0.at(y + 1, x);
1165
0
      lh.at(y, x) = sub1.at(y + 1, x);
1166
0
      hh.at(y, x) = sub2.at(y + 1, x);
1167
0
    }
1168
0
  for (int x = 0; x < w; x++)
1169
0
  {
1170
0
    lh.at(h, x) = sub0.at(h + 1, x);
1171
0
    hh.at(h, x) = sub1.at(h + 1, x);
1172
0
  }
1173
1174
0
  SonyArw6Plane low_horizontal =
1175
0
      sony_arw6_inv53_axis0_high_leading(ll, lh);
1176
0
  SonyArw6Plane high_horizontal =
1177
0
      sony_arw6_inv53_axis0_high_leading(hl, hh);
1178
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1179
0
}
1180
1181
static SonyArw6Plane sony_arw6_synthesize_guard_group2(
1182
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1183
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2,
1184
    bool odd_edge_mode)
1185
0
{
1186
0
  const int h = ll.rows;
1187
0
  const int w = ll.cols;
1188
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1189
0
  sony_arw6_require(sub0.rows >= h + 3 && sub1.rows >= h + 1 &&
1190
0
                    sub2.rows >= h + 1);
1191
1192
0
  SonyArw6Plane hl(h, w), lh(h, w), hh(h, w);
1193
0
  for (int x = 0; x < w; x++)
1194
0
  {
1195
0
    lh.at(0, x) = sub0.at(0, x);
1196
0
    hh.at(0, x) = sony_arw6_edge_detail(sub0.at(1, x), odd_edge_mode);
1197
0
  }
1198
0
  for (int y = 0; y < h; y++)
1199
0
    for (int x = 0; x < w; x++)
1200
0
      hl.at(y, x) = sub0.at(y + 2, x);
1201
0
  for (int y = 1; y < h; y++)
1202
0
    for (int x = 0; x < w; x++)
1203
0
    {
1204
0
      lh.at(y, x) = sub1.at(y + 1, x);
1205
0
      hh.at(y, x) = sub2.at(y + 1, x);
1206
0
    }
1207
1208
0
  SonyArw6Plane low_horizontal =
1209
0
      sony_arw6_inv53_axis0_high_leading(ll, lh);
1210
0
  SonyArw6Plane high_horizontal =
1211
0
      sony_arw6_inv53_axis0_high_leading(hl, hh);
1212
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1213
0
}
1214
1215
static SonyArw6Plane sony_arw6_synthesize_guard_group3(
1216
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1217
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2,
1218
    bool odd_edge_mode)
1219
0
{
1220
0
  const int h = ll.rows;
1221
0
  const int w = ll.cols;
1222
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1223
0
  sony_arw6_require(sub0.rows >= h + 5 && sub1.rows >= h + 2 &&
1224
0
                    sub2.rows >= h + 2);
1225
1226
0
  SonyArw6Plane hl(h, w), lh(h, w), hh(h, w);
1227
0
  for (int x = 0; x < w; x++)
1228
0
  {
1229
0
    hl.at(0, x) = sub0.at(0, x);
1230
0
    lh.at(0, x) = sub0.at(1, x);
1231
0
    hh.at(0, x) = sony_arw6_edge_detail(sub0.at(2, x), odd_edge_mode);
1232
0
    lh.at(h - 1, x) = sub0.at(h + 3, x);
1233
0
    hh.at(h - 1, x) =
1234
0
        sony_arw6_edge_detail(sub0.at(h + 4, x), odd_edge_mode);
1235
0
  }
1236
0
  for (int y = 1; y < h; y++)
1237
0
    for (int x = 0; x < w; x++)
1238
0
      hl.at(y, x) = sub0.at(y + 3, x);
1239
0
  for (int y = 1; y < h - 1; y++)
1240
0
    for (int x = 0; x < w; x++)
1241
0
    {
1242
0
      lh.at(y, x) = sub1.at(y + 3, x);
1243
0
      hh.at(y, x) = sub2.at(y + 3, x);
1244
0
    }
1245
1246
0
  SonyArw6Plane low_horizontal = sony_arw6_inv53_axis0(ll, lh);
1247
0
  SonyArw6Plane high_horizontal = sony_arw6_inv53_axis0(hl, hh);
1248
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1249
0
}
1250
1251
static SonyArw6Plane sony_arw6_synthesize_level_stride(
1252
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1253
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2, int edge_rows,
1254
    bool odd_edge_mode)
1255
0
{
1256
0
  if (edge_rows == 0)
1257
0
    return sony_arw6_synthesize_level(ll, sub0, sub1, sub2);
1258
1259
0
  const int h = ll.rows;
1260
0
  const int w = ll.cols;
1261
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1262
0
  sony_arw6_require(sub0.rows >= h + edge_rows * 2 &&
1263
0
                    sub1.rows >= h + edge_rows &&
1264
0
                    sub2.rows >= h);
1265
1266
0
  SonyArw6Plane hl(h, w), lh(h, w), hh(h, w);
1267
0
  for (int y = 0; y < edge_rows; y++)
1268
0
    for (int x = 0; x < w; x++)
1269
0
    {
1270
0
      hl.at(y, x) = sub0.at(y, x);
1271
0
      lh.at(y, x) = sub0.at(y + edge_rows, x);
1272
0
      hh.at(y, x) = sony_arw6_edge_detail(sub1.at(y, x), odd_edge_mode);
1273
0
    }
1274
0
  for (int y = edge_rows; y < h - edge_rows; y++)
1275
0
    for (int x = 0; x < w; x++)
1276
0
    {
1277
0
      hl.at(y, x) = sub0.at(y + edge_rows, x);
1278
0
      lh.at(y, x) = sub1.at(y + edge_rows, x);
1279
0
      hh.at(y, x) = sub2.at(y + edge_rows, x);
1280
0
    }
1281
0
  for (int y = h - edge_rows; y < h; y++)
1282
0
  {
1283
0
    const int k = y - (h - edge_rows);
1284
0
    for (int x = 0; x < w; x++)
1285
0
    {
1286
0
      hl.at(y, x) = sub0.at(h + k, x);
1287
0
      lh.at(y, x) = sub0.at(h + edge_rows + k, x);
1288
0
      hh.at(y, x) = sony_arw6_edge_detail(sub1.at(h + k, x), odd_edge_mode);
1289
0
    }
1290
0
  }
1291
1292
0
  SonyArw6Plane low_horizontal = sony_arw6_inv53_axis0(ll, lh);
1293
0
  SonyArw6Plane high_horizontal = sony_arw6_inv53_axis0(hl, hh);
1294
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1295
0
}
1296
1297
static SonyArw6Plane sony_arw6_final_green(const SonyArw6Plane &ll,
1298
                                           const SonyArw6Plane &detail,
1299
                                           int top_rows)
1300
0
{
1301
0
  const int h = ll.rows;
1302
0
  const int w = ll.cols;
1303
0
  sony_arw6_require(top_rows >= 0 && top_rows <= 8);
1304
0
  sony_arw6_require(detail.cols == w &&
1305
0
                    detail.rows >= 8 + std::max(0, h - top_rows));
1306
1307
0
  SonyArw6Plane selected(h, w);
1308
0
  for (int y = 0; y < h; y++)
1309
0
  {
1310
0
    const int src_y = y < top_rows ? y : y + 8 - top_rows;
1311
0
    for (int x = 0; x < w; x++)
1312
0
      selected.at(y, x) = detail.at(src_y, x);
1313
0
  }
1314
1315
0
  SonyArw6Plane odd_green(h, w);
1316
0
  for (int y = 0; y < h; y++)
1317
0
  {
1318
0
    const int prev_y = y ? y - 1 : y;
1319
0
    for (int x = 0; x < w; x++)
1320
0
    {
1321
0
      int32_t pred;
1322
0
      if (x + 1 < w)
1323
0
        pred = sony_arw6_floor_shift(
1324
0
            selected.at(y, x + 1) + selected.at(prev_y, x) +
1325
0
                selected.at(y, x) + selected.at(prev_y, x + 1),
1326
0
            2);
1327
0
      else
1328
0
        pred = sony_arw6_floor_shift(
1329
0
            (selected.at(prev_y, x) + selected.at(y, x)) * 2, 2);
1330
0
      odd_green.at(y, x) =
1331
0
          sony_arw6_floor_shift(int64_t(2) * ll.at(y, x) - pred, 1);
1332
0
    }
1333
0
  }
1334
1335
0
  SonyArw6Plane even_green(h, w);
1336
0
  for (int y = 0; y < h; y++)
1337
0
  {
1338
0
    const int next_y = y + 1 < h ? y + 1 : y;
1339
0
    even_green.at(y, 0) =
1340
0
        selected.at(y, 0) +
1341
0
        sony_arw6_floor_shift((odd_green.at(y, 0) + odd_green.at(next_y, 0)) *
1342
0
                                  int64_t(2),
1343
0
                              2);
1344
0
    for (int x = 1; x < w; x++)
1345
0
      even_green.at(y, x) =
1346
0
          selected.at(y, x) +
1347
0
          sony_arw6_floor_shift(odd_green.at(y, x - 1) +
1348
0
                                    odd_green.at(next_y, x - 1) +
1349
0
                                    odd_green.at(next_y, x) +
1350
0
                                    odd_green.at(y, x),
1351
0
                                2);
1352
0
  }
1353
1354
0
  SonyArw6Plane out(h, w * 2);
1355
0
  for (int y = 0; y < h; y++)
1356
0
    for (int x = 0; x < w; x++)
1357
0
    {
1358
0
      out.at(y, x * 2) = even_green.at(y, x);
1359
0
      out.at(y, x * 2 + 1) = odd_green.at(y, x);
1360
0
    }
1361
0
  return out;
1362
0
}
1363
1364
static inline ushort sony_arw6_sample_from_signed(int32_t x)
1365
0
{
1366
0
  int code = x + SONY_ARW6_INTERNAL_BIAS;
1367
0
  if (code < 0)
1368
0
    code = 0;
1369
0
  if (code > 4095)
1370
0
    code = 4095;
1371
0
  return sony_arw6_llvc3_lut_value(code);
1372
0
}
1373
1374
static inline int32_t sony_arw6_clamp_signed_code(int32_t x)
1375
0
{
1376
0
  int code = x + SONY_ARW6_INTERNAL_BIAS;
1377
0
  if (code < 0)
1378
0
    code = 0;
1379
0
  if (code > 4095)
1380
0
    code = 4095;
1381
0
  return code - SONY_ARW6_INTERNAL_BIAS;
1382
0
}
1383
1384
struct SonyArw6DecodedTile
1385
{
1386
  SonyArw6Plane green;
1387
  SonyArw6Plane red_residual;
1388
  SonyArw6Plane blue_residual;
1389
  SonyArw6Plane full_green;
1390
};
1391
1392
static SonyArw6DecodedTile sony_arw6_decode_stream_tile(const uchar *stream,
1393
                                                        uint32_t stream_size)
1394
21
{
1395
21
  SonyArw6StreamInfo header;
1396
21
  sony_arw6_require(sony_arw6_parse_stream_header(stream, stream_size, header));
1397
21
  const std::vector<SonyArw6DirectoryEntry> dir =
1398
21
      sony_arw6_parse_directory(stream, stream_size);
1399
21
  const int coded_height = header.logical_height;
1400
21
  sony_arw6_require(header.coded_width > 0 && coded_height > 0);
1401
21
  const int padded_height = sony_arw6_align_up(coded_height, 16);
1402
21
  const bool guarded_height = coded_height != padded_height;
1403
21
  const int low_rows = padded_height / 16;
1404
21
  const int low_start = guarded_height ? 1 : 0;
1405
21
  const int low_count = low_rows - low_start;
1406
1407
21
  std::vector<SonyArw6Plane> g0 =
1408
21
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1409
21
                                     header.coded_width, coded_height, 0, 0);
1410
21
  SonyArw6Plane green =
1411
21
      sony_arw6_integrate_type1(g0[0], low_start + low_count, 0);
1412
21
  if (low_start)
1413
0
  {
1414
0
    SonyArw6Plane cropped(low_count, green.cols);
1415
0
    for (int y = 0; y < low_count; y++)
1416
0
      memcpy(cropped.row(y), green.row(y + low_start),
1417
0
             sizeof(int32_t) * size_t(green.cols));
1418
0
    green = cropped;
1419
0
  }
1420
1421
21
  std::vector<SonyArw6Plane> r0 =
1422
21
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1423
21
                                     header.coded_width, coded_height, 0, 1);
1424
21
  SonyArw6Plane red_residual =
1425
21
      sony_arw6_integrate_type1(r0[0], low_start + low_count, 0);
1426
21
  if (low_start)
1427
0
  {
1428
0
    SonyArw6Plane cropped(low_count, red_residual.cols);
1429
0
    for (int y = 0; y < low_count; y++)
1430
0
      memcpy(cropped.row(y), red_residual.row(y + low_start),
1431
0
             sizeof(int32_t) * size_t(red_residual.cols));
1432
0
    red_residual = cropped;
1433
0
  }
1434
1435
21
  std::vector<SonyArw6Plane> b0 =
1436
21
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1437
21
                                     header.coded_width, coded_height, 0, 2);
1438
21
  SonyArw6Plane blue_residual =
1439
21
      sony_arw6_integrate_type1(b0[0], low_start + low_count, 0);
1440
21
  if (low_start)
1441
0
  {
1442
0
    SonyArw6Plane cropped(low_count, blue_residual.cols);
1443
0
    for (int y = 0; y < low_count; y++)
1444
0
      memcpy(cropped.row(y), blue_residual.row(y + low_start),
1445
0
             sizeof(int32_t) * size_t(blue_residual.cols));
1446
0
    blue_residual = cropped;
1447
0
  }
1448
1449
21
  for (int group = 1; group <= 3; group++)
1450
0
  {
1451
0
    const int edge_rows = group == 1 ? 0 : (group == 2 ? 1 : 2);
1452
0
    const bool color_odd_edge = group == 3;
1453
1454
0
    std::vector<SonyArw6Plane> planes =
1455
0
        sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1456
0
                                       header.coded_width, coded_height, group,
1457
0
                                       0);
1458
0
    if (guarded_height)
1459
0
    {
1460
0
      if (group == 1)
1461
0
        green = sony_arw6_synthesize_guard_group1(green, planes[0],
1462
0
                                                  planes[1], planes[2]);
1463
0
      else if (group == 2)
1464
0
        green = sony_arw6_synthesize_guard_group2(green, planes[0],
1465
0
                                                  planes[1], planes[2], false);
1466
0
      else
1467
0
        green = sony_arw6_synthesize_guard_group3(green, planes[0],
1468
0
                                                  planes[1], planes[2], false);
1469
0
    }
1470
0
    else
1471
0
      green = sony_arw6_synthesize_level_stride(green, planes[0], planes[1],
1472
0
                                                planes[2], edge_rows, false);
1473
1474
0
    planes = sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1475
0
                                            header.coded_width, coded_height,
1476
0
                                            group, 1);
1477
0
    if (guarded_height)
1478
0
    {
1479
0
      if (group == 1)
1480
0
        red_residual = sony_arw6_synthesize_guard_group1(
1481
0
            red_residual, planes[0], planes[1], planes[2]);
1482
0
      else if (group == 2)
1483
0
        red_residual = sony_arw6_synthesize_guard_group2(
1484
0
            red_residual, planes[0], planes[1], planes[2], false);
1485
0
      else
1486
0
        red_residual = sony_arw6_synthesize_guard_group3(
1487
0
            red_residual, planes[0], planes[1], planes[2], color_odd_edge);
1488
0
    }
1489
0
    else
1490
0
      red_residual = sony_arw6_synthesize_level_stride(
1491
0
          red_residual, planes[0], planes[1], planes[2], edge_rows,
1492
0
          color_odd_edge);
1493
1494
0
    planes = sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1495
0
                                            header.coded_width, coded_height,
1496
0
                                            group, 2);
1497
0
    if (guarded_height)
1498
0
    {
1499
0
      if (group == 1)
1500
0
        blue_residual = sony_arw6_synthesize_guard_group1(
1501
0
            blue_residual, planes[0], planes[1], planes[2]);
1502
0
      else if (group == 2)
1503
0
        blue_residual = sony_arw6_synthesize_guard_group2(
1504
0
            blue_residual, planes[0], planes[1], planes[2], false);
1505
0
      else
1506
0
        blue_residual = sony_arw6_synthesize_guard_group3(
1507
0
            blue_residual, planes[0], planes[1], planes[2], color_odd_edge);
1508
0
    }
1509
0
    else
1510
0
      blue_residual = sony_arw6_synthesize_level_stride(
1511
0
          blue_residual, planes[0], planes[1], planes[2], edge_rows,
1512
0
          color_odd_edge);
1513
0
  }
1514
1515
21
  std::vector<SonyArw6Plane> g4 =
1516
21
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1517
21
                                     header.coded_width, coded_height, 4, 0);
1518
21
  SonyArw6Plane full_green =
1519
21
      sony_arw6_final_green(green, g4[0], guarded_height ? 2 : 4);
1520
1521
21
  SonyArw6DecodedTile out;
1522
21
  out.green = green;
1523
21
  out.red_residual = red_residual;
1524
21
  out.blue_residual = blue_residual;
1525
21
  out.full_green = full_green;
1526
21
  return out;
1527
21
}
1528
1529
} // namespace
1530
1531
void LibRaw::sony_arw6_load_raw()
1532
72
{
1533
72
  if (raw_width <= 0 || raw_height <= 0 || (raw_width & 15) ||
1534
71
      (raw_height & 1))
1535
2
    throw LIBRAW_EXCEPTION_IO_BADFILE;
1536
70
  if (!raw_image)
1537
0
    throw LIBRAW_EXCEPTION_IO_BADFILE;
1538
70
  if (data_size < SONY_ARW6_STREAM_OFFSET + 0x80 ||
1539
69
      data_size > 1024LL * 1024LL * 1024LL)
1540
1
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
1541
69
  if (data_size > INT64(imgdata.rawparams.max_raw_memory_mb) *
1542
69
                      INT64(1024 * 1024))
1543
3
    throw LIBRAW_EXCEPTION_ALLOC;
1544
1545
66
  std::vector<uchar> strip(size_t(data_size));
1546
66
  ifp->seek(data_offset, SEEK_SET);
1547
66
  const int readed = ifp->read(&strip[0], 1, size_t(data_size));
1548
66
  if (readed != data_size)
1549
2
    throw LIBRAW_EXCEPTION_IO_EOF;
1550
1551
64
  const std::vector<SonyArw6StreamInfo> streams =
1552
64
      sony_arw6_find_streams(strip, raw_width, raw_height);
1553
64
  sony_arw6_require(!streams.empty());
1554
1555
64
  INT64 max_tile_pixels = 0;
1556
86
  for (size_t si = 0; si < streams.size(); si++)
1557
22
  {
1558
22
    const SonyArw6StreamInfo &s = streams[si];
1559
22
    max_tile_pixels =
1560
22
        std::max(max_tile_pixels, INT64(s.coded_width) * s.logical_height);
1561
22
  }
1562
64
  const INT64 raw_bytes = INT64(raw_width) * raw_height *
1563
64
                          INT64(sizeof(raw_image[0]));
1564
64
  const INT64 working_bytes =
1565
64
      raw_bytes + data_size +
1566
64
      max_tile_pixels * SONY_ARW6_WORKING_BYTES_PER_TILE_PIXEL;
1567
64
  if (working_bytes > sony_arw6_memory_limit_bytes(
1568
64
                          imgdata.rawparams.max_raw_memory_mb))
1569
1
    throw LIBRAW_EXCEPTION_ALLOC;
1570
1571
84
  for (size_t si = 0; si < streams.size(); si++)
1572
21
  {
1573
21
    checkCancel();
1574
21
    const SonyArw6StreamInfo &s = streams[si];
1575
21
    sony_arw6_require(s.offset <= strip.size() &&
1576
21
                      s.length <= strip.size() - s.offset);
1577
21
    sony_arw6_require(s.tile_x >= 0 && s.tile_y >= 0 &&
1578
21
                      s.tile_w == s.coded_width &&
1579
21
                      s.tile_h == s.logical_height &&
1580
21
                      s.tile_x + s.tile_w <= raw_width &&
1581
21
                      s.tile_y + s.tile_h <= raw_height);
1582
1583
21
    SonyArw6DecodedTile tile =
1584
21
        sony_arw6_decode_stream_tile(&strip[s.offset], s.length);
1585
1586
21
    const int half_h = s.logical_height / 2;
1587
21
    const int half_w = s.coded_width / 2;
1588
21
    sony_arw6_require(tile.full_green.rows >= half_h &&
1589
0
                      tile.full_green.cols == s.coded_width);
1590
21
    sony_arw6_require(tile.green.rows >= half_h &&
1591
0
                      tile.green.cols == half_w);
1592
21
    sony_arw6_require(tile.red_residual.rows >= half_h &&
1593
0
                      tile.red_residual.cols == half_w);
1594
21
    sony_arw6_require(tile.blue_residual.rows >= half_h &&
1595
0
                      tile.blue_residual.cols == half_w);
1596
1597
21
    for (int y = 0; y < half_h; y++)
1598
0
    {
1599
0
      checkCancel();
1600
0
      ushort *row0 =
1601
0
          raw_image + size_t(s.tile_y + y * 2) * raw_width + s.tile_x;
1602
0
      ushort *row1 =
1603
0
          raw_image + size_t(s.tile_y + y * 2 + 1) * raw_width + s.tile_x;
1604
0
      for (int x = 0; x < half_w; x++)
1605
0
      {
1606
0
        const int32_t g0 =
1607
0
            sony_arw6_clamp_signed_code(tile.full_green.at(y, x * 2));
1608
0
        const int32_t g1 =
1609
0
            sony_arw6_clamp_signed_code(tile.full_green.at(y, x * 2 + 1));
1610
0
        const int32_t gavg = sony_arw6_floor_shift(int64_t(g0) + g1, 1);
1611
0
        row0[x * 2] = sony_arw6_sample_from_signed(
1612
0
            gavg + 2 * tile.red_residual.at(y, x));
1613
0
        row0[x * 2 + 1] =
1614
0
            sony_arw6_sample_from_signed(tile.full_green.at(y, x * 2 + 1));
1615
0
        row1[x * 2] =
1616
0
            sony_arw6_sample_from_signed(tile.full_green.at(y, x * 2));
1617
0
        row1[x * 2 + 1] = sony_arw6_sample_from_signed(
1618
0
            gavg + 2 * tile.blue_residual.at(y, x));
1619
0
      }
1620
0
    }
1621
21
  }
1622
63
}