+ // clean up the Huffman decoder
+@@ -2159,6 +2236,10 @@
+
+ // allocate the bitmap
+ bitmap = new JBIG2Bitmap(0, w, h);
++ if (!bitmap->isOk()) {
++ delete bitmap;
++ return NULL;
++ }
+ if (defPixel) {
+ bitmap->clearToOne();
+ } else {
+@@ -2235,73 +2316,84 @@
+ ri = 0;
+ }
+ if (ri) {
++ GBool decodeSuccess;
+ if (huff) {
+- huffDecoder->decodeInt(&rdw, huffRDWTable);
+- huffDecoder->decodeInt(&rdh, huffRDHTable);
+- huffDecoder->decodeInt(&rdx, huffRDXTable);
+- huffDecoder->decodeInt(&rdy, huffRDYTable);
+- huffDecoder->decodeInt(&bmSize, huffRSizeTable);
++ decodeSuccess = huffDecoder->decodeInt(&rdw, huffRDWTable);
++ decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&rdh, huffRDHTable);
++ decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&rdx, huffRDXTable);
++ decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&rdy, huffRDYTable);
++ decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&bmSize, huffRSizeTable);
+ huffDecoder->reset();
+ arithDecoder->start();
+ } else {
+- arithDecoder->decodeInt(&rdw, iardwStats);
+- arithDecoder->decodeInt(&rdh, iardhStats);
+- arithDecoder->decodeInt(&rdx, iardxStats);
+- arithDecoder->decodeInt(&rdy, iardyStats);
++ decodeSuccess = arithDecoder->decodeInt(&rdw, iardwStats);
++ decodeSuccess = decodeSuccess && arithDecoder->decodeInt(&rdh, iardhStats);
++ decodeSuccess = decodeSuccess && arithDecoder->decodeInt(&rdx, iardxStats);
++ decodeSuccess = decodeSuccess && arithDecoder->decodeInt(&rdy, iardyStats);
++ }
++
++ if (decodeSuccess && syms[symID])
++ {
++ refDX = ((rdw >= 0) ? rdw : rdw - 1) / 2 + rdx;
++ refDY = ((rdh >= 0) ? rdh : rdh - 1) / 2 + rdy;
++
++ symbolBitmap =
++ readGenericRefinementRegion(rdw + syms[symID]->getWidth(),
++ rdh + syms[symID]->getHeight(),
++ templ, gFalse, syms[symID],
++ refDX, refDY, atx, aty);
+ }
+- refDX = ((rdw >= 0) ? rdw : rdw - 1) / 2 + rdx;
+- refDY = ((rdh >= 0) ? rdh : rdh - 1) / 2 + rdy;
+-
+- symbolBitmap =
+- readGenericRefinementRegion(rdw + syms[symID]->getWidth(),
+- rdh + syms[symID]->getHeight(),
+- templ, gFalse, syms[symID],
+- refDX, refDY, atx, aty);
+ //~ do we need to use the bmSize value here (in Huffman mode)?
+ } else {
+ symbolBitmap = syms[symID];
+ }
+
+- // combine the symbol bitmap into the region bitmap
+- //~ something is wrong here - refCorner shouldn't degenerate into
+- //~ two cases
+- bw = symbolBitmap->getWidth() - 1;
+- bh = symbolBitmap->getHeight() - 1;
+- if (transposed) {
+- switch (refCorner) {
+- case 0: // bottom left
+- bitmap->combine(symbolBitmap, tt, s, combOp);
+- break;
+- case 1: // top left
+- bitmap->combine(symbolBitmap, tt, s, combOp);
+- break;
+- case 2: // bottom right
+- bitmap->combine(symbolBitmap, tt - bw, s, combOp);
+- break;
+- case 3: // top right
+- bitmap->combine(symbolBitmap, tt - bw, s, combOp);
+- break;
++ if (symbolBitmap) {
++ // combine the symbol bitmap into the region bitmap
++ //~ something is wrong here - refCorner shouldn't degenerate into
++ //~ two cases
++ bw = symbolBitmap->getWidth() - 1;
++ bh = symbolBitmap->getHeight() - 1;
++ if (transposed) {
++ switch (refCorner) {
++ case 0: // bottom left
++ bitmap->combine(symbolBitmap, tt, s, combOp);
++ break;
++ case 1: // top left
++ bitmap->combine(symbolBitmap, tt, s, combOp);
++ break;
++ case 2: // bottom right
++ bitmap->combine(symbolBitmap, tt - bw, s, combOp);
++ break;
++ case 3: // top right
++ bitmap->combine(symbolBitmap, tt - bw, s, combOp);
++ break;
++ }
++ s += bh;
++ } else {
++ switch (refCorner) {
++ case 0: // bottom left
++ bitmap->combine(symbolBitmap, s, tt - bh, combOp);
++ break;
++ case 1: // top left
++ bitmap->combine(symbolBitmap, s, tt, combOp);
++ break;
++ case 2: // bottom right
++ bitmap->combine(symbolBitmap, s, tt - bh, combOp);
++ break;
++ case 3: // top right
++ bitmap->combine(symbolBitmap, s, tt, combOp);
++ break;
++ }
++ s += bw;
+ }
+- s += bh;
+- } else {
+- switch (refCorner) {
+- case 0: // bottom left
+- bitmap->combine(symbolBitmap, s, tt - bh, combOp);
+- break;
+- case 1: // top left
+- bitmap->combine(symbolBitmap, s, tt, combOp);
+- break;
+- case 2: // bottom right
+- bitmap->combine(symbolBitmap, s, tt - bh, combOp);
+- break;
+- case 3: // top right
+- bitmap->combine(symbolBitmap, s, tt, combOp);
+- break;
++ if (ri) {
++ delete symbolBitmap;
+ }
+- s += bw;
+- }
+- if (ri) {
+- delete symbolBitmap;
++ } else {
++ // NULL symbolBitmap only happens on error
++ delete bitmap;
++ return NULL;
+ }
+ }
+
+@@ -2431,11 +2523,12 @@
+ error(getPos(), "Bad symbol dictionary reference in JBIG2 halftone segment");
+ return;