From af15b57b30fcf442262f3a797d169d6bc3c1a44a Mon Sep 17 00:00:00 2001 From: Martin Kretzschmar Date: Mon, 17 May 2004 18:12:38 +0000 Subject: Import of Xpdf 3.00 --- (limited to 'pdf/xpdf/JBIG2Stream.cc') diff --git a/pdf/xpdf/JBIG2Stream.cc b/pdf/xpdf/JBIG2Stream.cc index f852d26..c1bf4f7 100644 --- a/pdf/xpdf/JBIG2Stream.cc +++ b/pdf/xpdf/JBIG2Stream.cc @@ -15,6 +15,7 @@ #include #include "GList.h" #include "Error.h" +#include "JArithmeticDecoder.h" #include "JBIG2Stream.h" //~ share these tables @@ -26,327 +27,6 @@ static int contextSize[4] = { 16, 13, 10, 10 }; static int refContextSize[2] = { 13, 10 }; //------------------------------------------------------------------------ -// JBIG2ArithmeticDecoderStats -//------------------------------------------------------------------------ - -class JBIG2ArithmeticDecoderStats { -public: - - JBIG2ArithmeticDecoderStats(int contextSizeA); - ~JBIG2ArithmeticDecoderStats(); - JBIG2ArithmeticDecoderStats *copy(); - void reset(); - int getContextSize() { return contextSize; } - void copyFrom(JBIG2ArithmeticDecoderStats *stats); - -private: - - Guchar *cxTab; // cxTab[cx] = (i[cx] << 1) + mps[cx] - int contextSize; - - friend class JBIG2ArithmeticDecoder; -}; - -JBIG2ArithmeticDecoderStats::JBIG2ArithmeticDecoderStats(int contextSizeA) { - contextSize = contextSizeA; - cxTab = (Guchar *)gmalloc((1 << contextSize) * sizeof(Guchar)); - reset(); -} - -JBIG2ArithmeticDecoderStats::~JBIG2ArithmeticDecoderStats() { - gfree(cxTab); -} - -JBIG2ArithmeticDecoderStats *JBIG2ArithmeticDecoderStats::copy() { - JBIG2ArithmeticDecoderStats *stats; - - stats = new JBIG2ArithmeticDecoderStats(contextSize); - memcpy(stats->cxTab, cxTab, 1 << contextSize); - return stats; -} - -void JBIG2ArithmeticDecoderStats::reset() { - memset(cxTab, 0, 1 << contextSize); -} - -void JBIG2ArithmeticDecoderStats::copyFrom( - JBIG2ArithmeticDecoderStats *stats) { - memcpy(cxTab, stats->cxTab, 1 << contextSize); -} - -//------------------------------------------------------------------------ -// JBIG2ArithmeticDecoder -//------------------------------------------------------------------------ - -class JBIG2ArithmeticDecoder { -public: - - JBIG2ArithmeticDecoder(); - ~JBIG2ArithmeticDecoder(); - void setStream(Stream *strA) { str = strA; } - void start(); - int decodeBit(Guint context, JBIG2ArithmeticDecoderStats *stats); - int decodeByte(Guint context, JBIG2ArithmeticDecoderStats *stats); - - // Returns false for OOB, otherwise sets * and returns true. - GBool decodeInt(int *x, JBIG2ArithmeticDecoderStats *stats); - - Guint decodeIAID(Guint codeLen, - JBIG2ArithmeticDecoderStats *stats); - -private: - - int decodeIntBit(JBIG2ArithmeticDecoderStats *stats); - void byteIn(); - - static Guint qeTab[47]; - static int nmpsTab[47]; - static int nlpsTab[47]; - static int switchTab[47]; - - Guint buf0, buf1; - Guint c, a; - int ct; - - Guint prev; // for the integer decoder - - Stream *str; -}; - -Guint JBIG2ArithmeticDecoder::qeTab[47] = { - 0x56010000, 0x34010000, 0x18010000, 0x0AC10000, - 0x05210000, 0x02210000, 0x56010000, 0x54010000, - 0x48010000, 0x38010000, 0x30010000, 0x24010000, - 0x1C010000, 0x16010000, 0x56010000, 0x54010000, - 0x51010000, 0x48010000, 0x38010000, 0x34010000, - 0x30010000, 0x28010000, 0x24010000, 0x22010000, - 0x1C010000, 0x18010000, 0x16010000, 0x14010000, - 0x12010000, 0x11010000, 0x0AC10000, 0x09C10000, - 0x08A10000, 0x05210000, 0x04410000, 0x02A10000, - 0x02210000, 0x01410000, 0x01110000, 0x00850000, - 0x00490000, 0x00250000, 0x00150000, 0x00090000, - 0x00050000, 0x00010000, 0x56010000 -}; - -int JBIG2ArithmeticDecoder::nmpsTab[47] = { - 1, 2, 3, 4, 5, 38, 7, 8, 9, 10, 11, 12, 13, 29, 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, 45, 46 -}; - -int JBIG2ArithmeticDecoder::nlpsTab[47] = { - 1, 6, 9, 12, 29, 33, 6, 14, 14, 14, 17, 18, 20, 21, 14, 14, - 15, 16, 17, 18, 19, 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, 46 -}; - -int JBIG2ArithmeticDecoder::switchTab[47] = { - 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -JBIG2ArithmeticDecoder::JBIG2ArithmeticDecoder() { - str = NULL; -} - -JBIG2ArithmeticDecoder::~JBIG2ArithmeticDecoder() { -} - -void JBIG2ArithmeticDecoder::start() { - buf0 = (Guint)str->getChar() & 0xff; - buf1 = (Guint)str->getChar() & 0xff; - - // INITDEC - c = (buf0 ^ 0xff) << 16; - byteIn(); - c <<= 7; - ct -= 7; - a = 0x80000000; -} - -int JBIG2ArithmeticDecoder::decodeBit(Guint context, - JBIG2ArithmeticDecoderStats *stats) { - int bit; - Guint qe; - int iCX, mpsCX; - - iCX = stats->cxTab[context] >> 1; - mpsCX = stats->cxTab[context] & 1; - qe = qeTab[iCX]; - a -= qe; - if (c < a) { - if (a & 0x80000000) { - bit = mpsCX; - } else { - // MPS_EXCHANGE - if (a < qe) { - bit = 1 - mpsCX; - if (switchTab[iCX]) { - stats->cxTab[context] = (nlpsTab[iCX] << 1) | (1 - mpsCX); - } else { - stats->cxTab[context] = (nlpsTab[iCX] << 1) | mpsCX; - } - } else { - bit = mpsCX; - stats->cxTab[context] = (nmpsTab[iCX] << 1) | mpsCX; - } - // RENORMD - do { - if (ct == 0) { - byteIn(); - } - a <<= 1; - c <<= 1; - --ct; - } while (!(a & 0x80000000)); - } - } else { - c -= a; - // LPS_EXCHANGE - if (a < qe) { - bit = mpsCX; - stats->cxTab[context] = (nmpsTab[iCX] << 1) | mpsCX; - } else { - bit = 1 - mpsCX; - if (switchTab[iCX]) { - stats->cxTab[context] = (nlpsTab[iCX] << 1) | (1 - mpsCX); - } else { - stats->cxTab[context] = (nlpsTab[iCX] << 1) | mpsCX; - } - } - a = qe; - // RENORMD - do { - if (ct == 0) { - byteIn(); - } - a <<= 1; - c <<= 1; - --ct; - } while (!(a & 0x80000000)); - } - return bit; -} - -int JBIG2ArithmeticDecoder::decodeByte(Guint context, - JBIG2ArithmeticDecoderStats *stats) { - int byte; - int i; - - byte = 0; - for (i = 0; i < 8; ++i) { - byte = (byte << 1) | decodeBit(context, stats); - } - return byte; -} - -GBool JBIG2ArithmeticDecoder::decodeInt(int *x, - JBIG2ArithmeticDecoderStats *stats) { - int s; - Guint v; - int i; - - prev = 1; - s = decodeIntBit(stats); - if (decodeIntBit(stats)) { - if (decodeIntBit(stats)) { - if (decodeIntBit(stats)) { - if (decodeIntBit(stats)) { - if (decodeIntBit(stats)) { - v = 0; - for (i = 0; i < 32; ++i) { - v = (v << 1) | decodeIntBit(stats); - } - v += 4436; - } else { - v = 0; - for (i = 0; i < 12; ++i) { - v = (v << 1) | decodeIntBit(stats); - } - v += 340; - } - } else { - v = 0; - for (i = 0; i < 8; ++i) { - v = (v << 1) | decodeIntBit(stats); - } - v += 84; - } - } else { - v = 0; - for (i = 0; i < 6; ++i) { - v = (v << 1) | decodeIntBit(stats); - } - v += 20; - } - } else { - v = decodeIntBit(stats); - v = (v << 1) | decodeIntBit(stats); - v = (v << 1) | decodeIntBit(stats); - v = (v << 1) | decodeIntBit(stats); - v += 4; - } - } else { - v = decodeIntBit(stats); - v = (v << 1) | decodeIntBit(stats); - } - - if (s) { - if (v == 0) { - return gFalse; - } - *x = -(int)v; - } else { - *x = (int)v; - } - return gTrue; -} - -int JBIG2ArithmeticDecoder::decodeIntBit(JBIG2ArithmeticDecoderStats *stats) { - int bit; - - bit = decodeBit(prev, stats); - if (prev < 0x100) { - prev = (prev << 1) | bit; - } else { - prev = (((prev << 1) | bit) & 0x1ff) | 0x100; - } - return bit; -} - -Guint JBIG2ArithmeticDecoder::decodeIAID(Guint codeLen, - JBIG2ArithmeticDecoderStats *stats) { - Guint i; - int bit; - - prev = 1; - for (i = 0; i < codeLen; ++i) { - bit = decodeBit(prev, stats); - prev = (prev << 1) | bit; - } - return prev - (1 << codeLen); -} - -void JBIG2ArithmeticDecoder::byteIn() { - if (buf0 == 0xff) { - if (buf1 > 0x8f) { - ct = 8; - } else { - buf0 = buf1; - buf1 = (Guint)str->getChar() & 0xff; - c = c + 0xfe00 - (buf0 << 9); - ct = 7; - } - } else { - buf0 = buf1; - buf1 = (Guint)str->getChar() & 0xff; - c = c + 0xff00 - (buf0 << 8); - ct = 8; - } -} - -//------------------------------------------------------------------------ // JBIG2HuffmanTable //------------------------------------------------------------------------ @@ -1292,21 +972,21 @@ public: Guint getSize() { return size; } void setBitmap(Guint idx, JBIG2Bitmap *bitmap) { bitmaps[idx] = bitmap; } JBIG2Bitmap *getBitmap(Guint idx) { return bitmaps[idx]; } - void setGenericRegionStats(JBIG2ArithmeticDecoderStats *stats) + void setGenericRegionStats(JArithmeticDecoderStats *stats) { genericRegionStats = stats; } - void setRefinementRegionStats(JBIG2ArithmeticDecoderStats *stats) + void setRefinementRegionStats(JArithmeticDecoderStats *stats) { refinementRegionStats = stats; } - JBIG2ArithmeticDecoderStats *getGenericRegionStats() + JArithmeticDecoderStats *getGenericRegionStats() { return genericRegionStats; } - JBIG2ArithmeticDecoderStats *getRefinementRegionStats() + JArithmeticDecoderStats *getRefinementRegionStats() { return refinementRegionStats; } private: Guint size; JBIG2Bitmap **bitmaps; - JBIG2ArithmeticDecoderStats *genericRegionStats; - JBIG2ArithmeticDecoderStats *refinementRegionStats; + JArithmeticDecoderStats *genericRegionStats; + JArithmeticDecoderStats *refinementRegionStats; }; JBIG2SymbolDict::JBIG2SymbolDict(Guint segNumA, Guint sizeA): @@ -1405,23 +1085,23 @@ JBIG2Stream::JBIG2Stream(Stream *strA, Object *globalsStream): { pageBitmap = NULL; - arithDecoder = new JBIG2ArithmeticDecoder(); - genericRegionStats = new JBIG2ArithmeticDecoderStats(1); - refinementRegionStats = new JBIG2ArithmeticDecoderStats(1); - iadhStats = new JBIG2ArithmeticDecoderStats(9); - iadwStats = new JBIG2ArithmeticDecoderStats(9); - iaexStats = new JBIG2ArithmeticDecoderStats(9); - iaaiStats = new JBIG2ArithmeticDecoderStats(9); - iadtStats = new JBIG2ArithmeticDecoderStats(9); - iaitStats = new JBIG2ArithmeticDecoderStats(9); - iafsStats = new JBIG2ArithmeticDecoderStats(9); - iadsStats = new JBIG2ArithmeticDecoderStats(9); - iardxStats = new JBIG2ArithmeticDecoderStats(9); - iardyStats = new JBIG2ArithmeticDecoderStats(9); - iardwStats = new JBIG2ArithmeticDecoderStats(9); - iardhStats = new JBIG2ArithmeticDecoderStats(9); - iariStats = new JBIG2ArithmeticDecoderStats(9); - iaidStats = new JBIG2ArithmeticDecoderStats(1); + arithDecoder = new JArithmeticDecoder(); + genericRegionStats = new JArithmeticDecoderStats(1 << 1); + refinementRegionStats = new JArithmeticDecoderStats(1 << 1); + iadhStats = new JArithmeticDecoderStats(1 << 9); + iadwStats = new JArithmeticDecoderStats(1 << 9); + iaexStats = new JArithmeticDecoderStats(1 << 9); + iaaiStats = new JArithmeticDecoderStats(1 << 9); + iadtStats = new JArithmeticDecoderStats(1 << 9); + iaitStats = new JArithmeticDecoderStats(1 << 9); + iafsStats = new JArithmeticDecoderStats(1 << 9); + iadsStats = new JArithmeticDecoderStats(1 << 9); + iardxStats = new JArithmeticDecoderStats(1 << 9); + iardyStats = new JArithmeticDecoderStats(1 << 9); + iardwStats = new JArithmeticDecoderStats(1 << 9); + iardhStats = new JArithmeticDecoderStats(1 << 9); + iariStats = new JArithmeticDecoderStats(1 << 9); + iaidStats = new JArithmeticDecoderStats(1 << 1); huffDecoder = new JBIG2HuffmanDecoder(); mmrDecoder = new JBIG2MMRDecoder(); @@ -1511,7 +1191,7 @@ int JBIG2Stream::lookChar() { return EOF; } -GString *JBIG2Stream::getPSFilter(char *indent) { +GString *JBIG2Stream::getPSFilter(int psLevel, char *indent) { return NULL; } @@ -3528,7 +3208,7 @@ void JBIG2Stream::discardSegment(Guint segNum) { } void JBIG2Stream::resetGenericStats(Guint templ, - JBIG2ArithmeticDecoderStats *prevStats) { + JArithmeticDecoderStats *prevStats) { int size; size = contextSize[templ]; @@ -3544,14 +3224,13 @@ void JBIG2Stream::resetGenericStats(Guint templ, genericRegionStats->reset(); } else { delete genericRegionStats; - genericRegionStats = new JBIG2ArithmeticDecoderStats(size); + genericRegionStats = new JArithmeticDecoderStats(1 << size); } } } -void JBIG2Stream::resetRefinementStats( - Guint templ, - JBIG2ArithmeticDecoderStats *prevStats) { +void JBIG2Stream::resetRefinementStats(Guint templ, + JArithmeticDecoderStats *prevStats) { int size; size = refContextSize[templ]; @@ -3567,7 +3246,7 @@ void JBIG2Stream::resetRefinementStats( refinementRegionStats->reset(); } else { delete refinementRegionStats; - refinementRegionStats = new JBIG2ArithmeticDecoderStats(size); + refinementRegionStats = new JArithmeticDecoderStats(1 << size); } } } @@ -3590,7 +3269,7 @@ void JBIG2Stream::resetIntStats(int symCodeLen) { iaidStats->reset(); } else { delete iaidStats; - iaidStats = new JBIG2ArithmeticDecoderStats(symCodeLen + 1); + iaidStats = new JArithmeticDecoderStats(1 << (symCodeLen + 1)); } } -- cgit v0.9.1