31 std::vector<JBig2ArithCtx>* gbContext,
32 std::vector<JBig2ArithCtx>* grContext) {
47 uint32_t SBSYMCODELENA = 0;
53 std::vector<std::unique_ptr<CJBig2_Image>> SDNEWSYMS(SDNUMNEWSYMS);
54 uint32_t HCHEIGHT = 0;
55 uint32_t NSYMSDECODED = 0;
57 std::unique_ptr<CJBig2_Image> BS;
59 IADH->Decode(pArithDecoder, &HCDH);
60 HCHEIGHT = HCHEIGHT + HCDH;
64 uint32_t SYMWIDTH = 0;
67 if (!IADW->Decode(pArithDecoder, &DW))
73 SYMWIDTH = SYMWIDTH + DW;
77 if (HCHEIGHT == 0 || SYMWIDTH == 0) {
88 pGRD->USESKIP =
false;
89 pGRD->GBAT[0] =
SDAT[0];
90 pGRD->GBAT[1] =
SDAT[1];
91 pGRD->GBAT[2] =
SDAT[2];
92 pGRD->GBAT[3] =
SDAT[3];
93 pGRD->GBAT[4] =
SDAT[4];
94 pGRD->GBAT[5] =
SDAT[5];
95 pGRD->GBAT[6] =
SDAT[6];
96 pGRD->GBAT[7] =
SDAT[7];
97 BS = pGRD->DecodeArith(pArithDecoder, gbContext->data());
101 uint32_t REFAGGNINST;
102 IAAI->Decode(pArithDecoder, (
int*)&REFAGGNINST);
103 if (REFAGGNINST > 1) {
114 pDecoder->SBHUFF =
SDHUFF;
115 pDecoder->SBREFINE =
true;
116 pDecoder->SBW = SYMWIDTH;
117 pDecoder->SBH = HCHEIGHT;
118 pDecoder->SBNUMINSTANCES = REFAGGNINST;
119 pDecoder->SBSTRIPS = 1;
122 while ((uint32_t)(1 << nTmp) < pDecoder->SBNUMSYMS) {
125 uint8_t SBSYMCODELEN = (uint8_t)nTmp;
126 pDecoder->SBSYMCODELEN = SBSYMCODELEN;
127 std::vector<CJBig2_Image*> SBSYMS;
128 SBSYMS.resize(pDecoder->SBNUMSYMS);
130 for (size_t i = 0; i < NSYMSDECODED; ++i)
132 pDecoder->SBSYMS = SBSYMS.data();
133 pDecoder->SBDEFPIXEL =
false;
135 pDecoder->TRANSPOSED =
false;
137 pDecoder->SBDSOFFSET = 0;
138 pDecoder->SBHUFFFS = SBHUFFFS.get();
139 pDecoder->SBHUFFDS = SBHUFFDS.get();
140 pDecoder->SBHUFFDT = SBHUFFDT.get();
141 pDecoder->SBHUFFRDW = SBHUFFRDW.get();
142 pDecoder->SBHUFFRDH = SBHUFFRDH.get();
143 pDecoder->SBHUFFRDX = SBHUFFRDX.get();
144 pDecoder->SBHUFFRDY = SBHUFFRDY.get();
145 pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
147 pDecoder->SBRAT[0] =
SDRAT[0];
148 pDecoder->SBRAT[1] =
SDRAT[1];
149 pDecoder->SBRAT[2] =
SDRAT[2];
150 pDecoder->SBRAT[3] =
SDRAT[3];
152 ids.IADT = IADT.get();
153 ids.IAFS = IAFS.get();
154 ids.IADS = IADS.get();
155 ids.IAIT = IAIT.get();
156 ids.IARI = IARI.get();
157 ids.IARDW = IARDW.get();
158 ids.IARDH = IARDH.get();
159 ids.IARDX = IARDX.get();
160 ids.IARDY = IARDY.get();
161 ids.IAID = IAID.get();
162 BS = pDecoder->DecodeArith(pArithDecoder, grContext->data(), &ids);
165 }
else if (REFAGGNINST == 1) {
168 IAID->Decode(pArithDecoder, &IDI);
169 if (IDI >= SBNUMSYMS)
178 IARDX->Decode(pArithDecoder, &RDXI);
179 IARDY->Decode(pArithDecoder, &RDYI);
182 pGRRD->GRW = SYMWIDTH;
183 pGRRD->GRH = HCHEIGHT;
185 pGRRD->GRREFERENCE = sbsyms_idi;
186 pGRRD->GRREFERENCEDX = RDXI;
187 pGRRD->GRREFERENCEDY = RDYI;
188 pGRRD->TPGRON =
false;
189 pGRRD->GRAT[0] =
SDRAT[0];
190 pGRRD->GRAT[1] =
SDRAT[1];
191 pGRRD->GRAT[2] =
SDRAT[2];
192 pGRRD->GRAT[3] =
SDRAT[3];
193 BS = pGRRD->Decode(pArithDecoder, grContext->data());
198 SDNEWSYMS[NSYMSDECODED] =
std::move(BS);
203 std::vector<
bool> EXFLAGS;
205 bool CUREXFLAG =
false;
206 uint32_t EXINDEX = 0;
207 uint32_t num_ex_syms = 0;
209 uint32_t EXRUNLENGTH;
210 IAEX->Decode(pArithDecoder, (
int*)&EXRUNLENGTH);
211 FX_SAFE_UINT32 new_ex_size = EXINDEX;
212 new_ex_size += EXRUNLENGTH;
213 if (!new_ex_size.IsValid() ||
219 num_ex_syms += EXRUNLENGTH;
220 std::fill_n(EXFLAGS.begin() + EXINDEX, EXRUNLENGTH, CUREXFLAG);
221 EXINDEX = new_ex_size.ValueOrDie();
222 CUREXFLAG = !CUREXFLAG;
227 std::unique_ptr<CJBig2_SymbolDict> pDict =
228 std::make_unique<CJBig2_SymbolDict>();
245 std::vector<JBig2ArithCtx>* gbContext,
246 std::vector<JBig2ArithCtx>* grContext) {
249 std::vector<std::unique_ptr<CJBig2_Image>> SDNEWSYMS(SDNUMNEWSYMS);
250 std::vector<uint32_t> SDNEWSYMWIDTHS;
253 uint32_t HCHEIGHT = 0;
254 uint32_t NSYMSDECODED = 0;
255 std::unique_ptr<CJBig2_Image> BS;
258 if (pHuffmanDecoder->DecodeAValue(SDHUFFDH, &HCDH) != 0)
261 HCHEIGHT = HCHEIGHT + HCDH;
265 uint32_t SYMWIDTH = 0;
266 uint32_t TOTWIDTH = 0;
267 uint32_t HCFIRSTSYM = NSYMSDECODED;
270 int32_t nVal = pHuffmanDecoder->DecodeAValue(SDHUFFDW, &DW);
278 SYMWIDTH = SYMWIDTH + DW;
282 TOTWIDTH += SYMWIDTH;
283 if (HCHEIGHT == 0 || SYMWIDTH == 0) {
288 uint32_t REFAGGNINST;
289 if (pHuffmanDecoder->DecodeAValue(SDHUFFAGGINST, (
int*)&REFAGGNINST) !=
294 if (REFAGGNINST > 1) {
305 pDecoder->SBHUFF =
SDHUFF;
306 pDecoder->SBREFINE =
true;
307 pDecoder->SBW = SYMWIDTH;
308 pDecoder->SBH = HCHEIGHT;
309 pDecoder->SBNUMINSTANCES = REFAGGNINST;
310 pDecoder->SBSTRIPS = 1;
312 std::vector<JBig2HuffmanCode> SBSYMCODES(pDecoder->SBNUMSYMS);
314 while (
static_cast<uint32_t>(1 << nTmp) < pDecoder->SBNUMSYMS)
316 for (uint32_t i = 0; i < pDecoder->SBNUMSYMS; ++i) {
317 SBSYMCODES[i].codelen = nTmp;
318 SBSYMCODES[i].code = i;
320 pDecoder->SBSYMCODES =
std::move(SBSYMCODES);
321 std::vector<CJBig2_Image*> SBSYMS;
322 SBSYMS.resize(pDecoder->SBNUMSYMS);
324 for (size_t i = 0; i < NSYMSDECODED; ++i)
326 pDecoder->SBSYMS = SBSYMS.data();
327 pDecoder->SBDEFPIXEL =
false;
329 pDecoder->TRANSPOSED =
false;
331 pDecoder->SBDSOFFSET = 0;
332 pDecoder->SBHUFFFS = SBHUFFFS.get();
333 pDecoder->SBHUFFDS = SBHUFFDS.get();
334 pDecoder->SBHUFFDT = SBHUFFDT.get();
335 pDecoder->SBHUFFRDW = SBHUFFRDW.get();
336 pDecoder->SBHUFFRDH = SBHUFFRDH.get();
337 pDecoder->SBHUFFRDX = SBHUFFRDX.get();
338 pDecoder->SBHUFFRDY = SBHUFFRDY.get();
339 pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
341 pDecoder->SBRAT[0] =
SDRAT[0];
342 pDecoder->SBRAT[1] =
SDRAT[1];
343 pDecoder->SBRAT[2] =
SDRAT[2];
344 pDecoder->SBRAT[3] =
SDRAT[3];
345 BS = pDecoder->DecodeHuffman(pStream, grContext->data());
349 }
else if (REFAGGNINST == 1) {
352 while ((uint32_t)(1 << nTmp) < SBNUMSYMS) {
355 uint8_t SBSYMCODELEN = (uint8_t)nTmp;
362 uVal = (uVal << 1) | nTmp;
363 if (uVal >= SBNUMSYMS)
366 IDI = SBSYMCODELEN == 0 ? uVal : SBNUMSYMS;
379 if ((pHuffmanDecoder->DecodeAValue(SBHUFFRDX.get(), &RDXI) != 0) ||
380 (pHuffmanDecoder->DecodeAValue(SBHUFFRDX.get(), &RDYI) != 0) ||
381 (pHuffmanDecoder->DecodeAValue(SBHUFFRSIZE.get(), &nVal) != 0)) {
389 pGRRD->GRW = SYMWIDTH;
390 pGRRD->GRH = HCHEIGHT;
392 pGRRD->GRREFERENCE = sbsyms_idi;
393 pGRRD->GRREFERENCEDX = RDXI;
394 pGRRD->GRREFERENCEDY = RDYI;
395 pGRRD->TPGRON =
false;
396 pGRRD->GRAT[0] =
SDRAT[0];
397 pGRRD->GRAT[1] =
SDRAT[1];
398 pGRRD->GRAT[2] =
SDRAT[2];
399 pGRRD->GRAT[3] =
SDRAT[3];
401 BS = pGRRD->Decode(pArithDecoder.get(), grContext->data());
410 SDNEWSYMS[NSYMSDECODED] =
std::move(BS);
413 SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
418 if (pHuffmanDecoder->DecodeAValue(SDHUFFBMSIZE, (int32_t*)&BMSIZE) != 0) {
422 std::unique_ptr<CJBig2_Image> BHC;
429 const uint32_t stride = (TOTWIDTH + 7) / 8;
430 FX_SAFE_UINT32 safe_image_size = stride;
431 safe_image_size *= HCHEIGHT;
432 if (!safe_image_size.IsValid() ||
438 for (uint32_t i = 0; i < HCHEIGHT; ++i) {
446 pGRD->GBW = TOTWIDTH;
447 pGRD->GBH = HCHEIGHT;
448 pGRD->StartDecodeMMR(&BHC, pStream);
455 for (uint32_t i = HCFIRSTSYM; i < NSYMSDECODED; ++i) {
456 SDNEWSYMS[i] = BHC->SubImage(nTmp, 0, SDNEWSYMWIDTHS[i], HCHEIGHT);
457 nTmp += SDNEWSYMWIDTHS[i];
462 std::unique_ptr<CJBig2_HuffmanTable> pTable =
463 std::make_unique<CJBig2_HuffmanTable>(1);
464 std::vector<
bool> EXFLAGS;
466 bool CUREXFLAG =
false;
467 uint32_t EXINDEX = 0;
468 uint32_t num_ex_syms = 0;
470 uint32_t EXRUNLENGTH;
471 if (pHuffmanDecoder->DecodeAValue(pTable.get(), (
int*)&EXRUNLENGTH) != 0)
474 FX_SAFE_UINT32 new_ex_size = EXINDEX;
475 new_ex_size += EXRUNLENGTH;
476 if (!new_ex_size.IsValid() ||
482 num_ex_syms += EXRUNLENGTH;
483 std::fill_n(EXFLAGS.begin() + EXINDEX, EXRUNLENGTH, CUREXFLAG);
484 EXINDEX = new_ex_size.ValueOrDie();
485 CUREXFLAG = !CUREXFLAG;