Beispiel #1
0
 def doWhile(self):
     if (self.a & 0x80000000) == 0:
         if self.counter == 0:
             self.readbyte()
         self.a = BinaryOperation.bit32ShiftL(self.a, 1)
         self.c = BinaryOperation.bit32ShiftL(self.c, 1)
         self.counter -= 1
     else:
         return False
    def get24Bits(self):
        while self.bufferLength < 24:
            self.buffer = ((BinaryOperation.bit32ShiftL(self.buffer, 8)
                            ) | (self._reader.readbyte() & 0xff))
            self.bufferLength += 8
            self.noOfbytesRead += 1

        return (BinaryOperation.bit32ShiftR(self.buffer, int(self.bufferLength - 24)
                                            )) & 0xffffff
Beispiel #3
0
    def start(self):
        self.buffer0 = self._reader.readbyte()
        self.buffer1 = self._reader.readbyte()

        self.c = BinaryOperation.bit32ShiftL(self.buffer0 ^ 0xff, 16)
        self.readbyte()
        self.c = BinaryOperation.bit32ShiftL(self.c, 7)
        self.counter -= 7
        self.a = 0x80000000
Beispiel #4
0
    def decodeIntBit(self, stats: ArithmeticDecoderStats):
        bit = int()

        bit = self.decodeBit(self.previous, stats)
        if self.previous < 0x100:
            self.previous = BinaryOperation.bit32ShiftL(self.previous, 1) | bit
        else:
            self.previous = (BinaryOperation.bit32ShiftL(self.previous, 1)
                             | bit) & 0x1ff | 0x100

        return bit
Beispiel #5
0
    def decodeIAID(self, codeLen: int, stats: ArithmeticDecoderStats) -> int:
        self.previous = 1
        for i in range(codeLen):
            bit: int = self.decodeBit(self.previous, stats)
            self.previous = BinaryOperation.bit32ShiftL(self.previous, 1) | bit

        return self.previous - (1 << int(codeLen))
Beispiel #6
0
    def decodeInt(self, stats: ArithmeticDecoderStats) -> DecodeIntResult():
        value = int()

        self.previous = 1
        s = self.decodeIntBit(stats)
        if self.decodeIntBit(stats) != 0:
            if self.decodeIntBit(stats) != 0:
                if self.decodeIntBit(stats) != 0:
                    if self.decodeIntBit(stats) != 0:
                        if self.decodeIntBit(stats) != 0:
                            value = 0
                            for i in range(32):
                                value = BinaryOperation.bit32ShiftL(
                                    value, 1) | self.decodeIntBit(stats)
                            value += 4436
                        else:
                            value = 0
                            for i in range(12):
                                value = BinaryOperation.bit32ShiftL(
                                    value, 1) | self.decodeIntBit(stats)
                            value += 340
                    else:
                        value = 0
                        for i in range(8):
                            value = BinaryOperation.bit32ShiftL(
                                value, 1) | self.decodeIntBit(stats)
                        value += 84
                else:
                    value = 0
                    for i in range(6):
                        value = BinaryOperation.bit32ShiftL(
                            value, 1) | self.decodeIntBit(stats)
                    value += 20
            else:
                value = self.decodeIntBit(stats)
                value = BinaryOperation.bit32ShiftL(
                    value, 1) | self.decodeIntBit(stats)
                value = BinaryOperation.bit32ShiftL(
                    value, 1) | self.decodeIntBit(stats)
                value = BinaryOperation.bit32ShiftL(
                    value, 1) | self.decodeIntBit(stats)
                value += 4
        else:
            value = self.decodeIntBit(stats)
            value = BinaryOperation.bit32ShiftL(value,
                                                1) | self.decodeIntBit(stats)

        decodedInt = int()

        if s != 0:
            if value == 0:
                return DecodeIntResult(self, int(value), False)
            decodedInt = int(-value)
        else:
            decodedInt = int(value)

        return DecodeIntResult(self, decodedInt, True)
    def getBlackCode(self):
        tuple0: int = int()
        tuple1: int = int()
        code: int = int()

        if self.bufferLength == 0:
            self.buffer = (self._reader.readbyte() & 0xff)
            self.bufferLength = 8
            self.noOfbytesRead += 1

        while True:

            if (self.bufferLength >= 6 and
                    ((BinaryOperation.bit32ShiftR(self.buffer, int(self.bufferLength - 6))) & 0x3f) == 0):

                code = self.returnCode(13)
                tuple0 = self.blackTable1[int(code & 0x7f)][0]
                tuple1 = self.blackTable1[int(code & 0x7f)][1]

            elif (self.bufferLength >= 4 and
                  ((BinaryOperation.bit32ShiftR(self.buffer, int(self.bufferLength - 4))) & 0x0f) == 0):

                code = self.returnCode(12)
                lookup = int((code & 0xff) - 64)
                tuple0, tuple1 = self.returnTuples(lookup, self.blackTable2)

            else:
                code = self.returnCode(6)
                lookup = int(code & 0x3f)
                tuple0, tuple1 = self.returnTuples(lookup, self.blackTable2)

            if 0 < tuple0 <= int(self.bufferLength):
                self.bufferLength -= tuple0
                return tuple0

            if self.bufferLength >= 13:
                break

            self.buffer = ((BinaryOperation.bit32ShiftL(self.buffer, 8)) | (self._reader.readbyte() & 0xff))
            self.bufferLength += 8
            self.noOfbytesRead += 1

        self.bufferLength -= 1
        return 1
Beispiel #8
0
 def readbyte(self):
     if self.buffer0 == 0xff:
         if self.buffer1 > 0x8f:
             self.counter = 8
         else:
             self.buffer0 = self.buffer1
             self.buffer1 = self._reader.readbyte()
             self.c = self.c + 0xfe00 - BinaryOperation.bit32ShiftL(
                 self.buffer0, 9)
             self.counter = 8
    def get2DCode(self) -> int:
        tuple0: int = int()
        tuple1: int = int()

        if self.bufferLength == 0:
            self.buffer = (self._reader.readbyte() & 0xff)
            self.bufferLength = 8
            self.noOfbytesRead += 1

            lookup: int = int((BinaryOperation.bit32ShiftR(self.buffer, 1)) & 0x7f)
            tuple0 = self.twoDimensionalTable1[lookup][0]
            tuple1 = self.twoDimensionalTable1[lookup][1]

        elif self.bufferLength == 8:
            lookup = int((BinaryOperation.bit32ShiftR(self.buffer, 1)) & 0x7f)
            tuple0 = self.twoDimensionalTable1[lookup][0]
            tuple1 = self.twoDimensionalTable1[lookup][1]

        else:
            lookup = int((BinaryOperation.bit32ShiftL(self.buffer, int(7 - self.bufferLength))) & 0x7f)
            tuple0 = self.twoDimensionalTable1[lookup][0]
            tuple1 = self.twoDimensionalTable1[lookup][1]

            if tuple0 < 0 or tuple0 > int(self.bufferLength):
                right: int = (self._reader.readbyte() & 0xff)
                left: int = (BinaryOperation.bit32ShiftL(self.buffer, 8))

                self.buffer = left | right
                self.bufferLength += 8
                self.noOfbytesRead += 1

                lookup = int((BinaryOperation.bit32ShiftL(self.buffer, int(7 - self.bufferLength))) & 0x7f)
                tuple0 = self.twoDimensionalTable1[lookup][0]
                tuple1 = self.twoDimensionalTable1[lookup][1]

        if tuple0 < 0:
            return 0

        self.bufferLength -= tuple0

        return tuple1
Beispiel #10
0
    def decodeBit(self, context: int, stats: ArithmeticDecoderStats) -> int:
        iCX: int = BinaryOperation.bit8Shift(
            stats.getContextCodingTableValue(int(context)), 1,
            BinaryOperation.RIGHT_SHIFT)
        mpsCX: int = stats.getContextCodingTableValue(int(context) & 1)
        qe: int = self.qeTable[iCX]

        self.a -= qe

        bit = int()

        if (self.a & 0x80000000) != 0:
            bit = mpsCX
        else:
            if self.a < qe:
                if self.a < qe:
                    bit = 1 - mpsCX
                    if self.switchTable[iCX] != 0:
                        stats.setContextCodingTableValue(
                            int(context), self.nlpsTable[iCX] << 1 | 1 - mpsCX)
                    else:
                        stats.setContextCodingTableValue(
                            int(context), self.nlpsTable[iCX] << 1 | mpsCX)
                else:
                    bit = mpsCX
                    stats.setContextCodingTableValue(
                        int(context), self.nmpsTable[iCX] << 1 | mpsCX)

                    while True:
                        if self.doWhile(): break
            else:
                self.c -= self.a

                if self.a < qe:
                    bit = mpsCX
                    stats.setContextCodingTableValue(
                        int(context), self.nmpsTable[iCX] << 1 | mpsCX)
                else:
                    bit = 1 - mpsCX
                    if self.switchTable[iCX] != 0:
                        stats.setContextCodingTableValue(
                            int(context), self.nmpsTable[iCX] << 1 | 1 - mpsCX)
                    else:
                        stats.setContextCodingTableValue(
                            int(context), self.nmpsTable[iCX] << 1 | mpsCX)

                self.a = qe

                while True:
                    if self.doWhile(): break

        return bit
Beispiel #11
0
    def getWhiteCode(self):
        tuple0: int = int()
        tuple1: int = int()
        code: int = int()

        if self.bufferLength == 0:
            self.buffer = (self._reader.readbyte() & 0xff)
            self.bufferLength = 8
            self.noOfbytesRead += 1

        while True:
            if (self.bufferLength >= 7 and
                    ((BinaryOperation.bit32ShiftR(self.buffer,
                                                  int(self.bufferLength - 7))) & 0x7f) == 0):

                code = self.returnCode(12)
                tuple0 = self.whiteTable1[int(code & 0x1f)][0]
                tuple1 = self.whiteTable1[int(code & 0x1f)][1]

            else:
                code = self.returnCode(9)

                lookup = int(code & 0x1ff)

                tuple0, tuple1 = self.returnTuples(lookup, self.whiteTable2)

                if 0 < tuple0 <= int(self.bufferLength):
                    self.bufferLength -= tuple0
                    return tuple1

                if self.bufferLength >= 12:
                    break

                self.buffer = ((BinaryOperation.bit32ShiftL(self.buffer, 8)) | self._reader.readbyte() & 0xff)
                self.bufferLength += 8
                self.noOfbytesRead += 1

        self.bufferLength -= 1
        return 1
Beispiel #12
0
 def returnCode(self, number: int) -> int:
     if self.bufferLength <= number:
         return BinaryOperation.bit32ShiftL(self.buffer, int(number - self.bufferLength))
     else:
         return BinaryOperation.bit32ShiftR(self.buffer, int(self.bufferLength - number))
Beispiel #13
0
    def readBitmap(self, useMMR: bool, template: int,
                   typicalPredictionGenericDecodingOn: bool, useSkip: bool,
                   skipBitmap, adaptiveTemplateX: list,
                   adaptiveTemplateY: list,
                   mmrDataLength: int) -> None:  # skipBitmap: JBIG2Bitmap

        if useMMR:
            self._mmrDecoder.reset()

            referenceLine: list = [0] * (self._width + 2)
            codingLine: list = [0] * (self._width + 2)
            codingLine[0] = codingLine[1] = self._width

            for row in range(self._height):

                i: int = int()
                while codingLine[i] < self._width:
                    referenceLine[i] = codingLine[i]
                    i += 1
                codingLine[i] = codingLine[i + 1] = self._width

                referenceI: int = int()
                codingI: int = int()
                a0: int = int()

                while True:
                    tmp = self.DoWhile(a0, referenceLine, referenceI, codingI,
                                       codingLine)
                    if not tmp[0]:
                        break
                    else:
                        codingLine, codingI = tmp[1], tmp[2]
                codingLine[codingI + 1] = self._width

                j: int = int()
                while codingLine[j] < self._width:
                    for col in range(codingLine[j], codingLine[j + 1]):
                        self.setPixel(col, row, 1)
                    j += 2
            if mmrDataLength >= 0:
                self._mmrDecoder.skipTo(mmrDataLength)
            else:
                if self._mmrDecoder.get24Bits() != 0x001001:
                    if self.debug:
                        print("Missing EOFB in JBIG2 MMR bitmap data")

        else:
            cxPtr0: BitmapPointer = BitmapPointer(self)
            cxPtr1: BitmapPointer = BitmapPointer(self)

            atPtr0: BitmapPointer = BitmapPointer(self)
            atPtr1: BitmapPointer = BitmapPointer(self)
            atPtr2: BitmapPointer = BitmapPointer(self)
            atPtr3: BitmapPointer = BitmapPointer(self)

            ltpCX: int = 0  # long

            if typicalPredictionGenericDecodingOn:
                if template == 0:
                    ltpCX = 0x3953
                elif template == 1:
                    ltpCX = 0x079a
                elif template == 2:
                    ltpCX = 0x0e3
                elif template == 3:
                    ltpCX = 0x18a

            ltp: bool = bool()

            # long values
            cx = int()
            cx0 = int()
            cx1 = int()
            cx2 = int()
            #

            for row in range(self._height):
                if typicalPredictionGenericDecodingOn:
                    bit = self._arithmeticDecoder.decodeBit(
                        ltpCX, self._arithmeticDecoder.genericRegionStats)
                    if bit != 0:
                        ltp = not ltp
                    if ltp:
                        self.duplicateRow(row, row - 1)
                        continue

                pixel: int = int()

                if template == 0:
                    cxPtr0.setPointer(0, row - 2)
                    cx0 = (cxPtr0.nextPixel() << 1)
                    cx0 |= cxPtr0.nextPixel()

                    cxPtr1.setPointer(0, row - 1)
                    cx1 = (cxPtr1.nextPixel() << 2)
                    cx1 |= (cxPtr1.nextPixel() << 1)
                    cx1 |= (cxPtr1.nextPixel())

                    cx2 = int()
                    atPtr0.setPointer(adaptiveTemplateX[0],
                                      row + adaptiveTemplateY[0])
                    atPtr1.setPointer(adaptiveTemplateX[1],
                                      row + adaptiveTemplateY[1])
                    atPtr2.setPointer(adaptiveTemplateX[2],
                                      row + adaptiveTemplateY[2])
                    atPtr3.setPointer(adaptiveTemplateX[3],
                                      row + adaptiveTemplateY[3])
                    for col in range(self._width):
                        cx = (BinaryOperation.bit32ShiftL(cx0, 13)) | (BinaryOperation.bit32ShiftL(cx1, 8)) | \
                             (BinaryOperation.bit32ShiftL(cx2, 4)) | (atPtr0.nextPixel() << 3) | \
                             (atPtr1.nextPixel() << 2) | (atPtr2.nextPixel() << 1) | atPtr3.nextPixel()

                        if useSkip and skipBitmap.getPixel(col, row) != 0:
                            pixel = int()
                        else:
                            pixel = self._arithmeticDecoder.decodeBit(
                                cx, self._arithmeticDecoder.genericRegionStats)
                            if pixel != 0:
                                self.data.set(row * self._width + col)

                        cx0 = ((BinaryOperation.bit32ShiftL(cx0, 1))
                               | cxPtr0.nextPixel()) & 0x07
                        cx1 = ((BinaryOperation.bit32ShiftL(cx1, 1))
                               | cxPtr1.nextPixel()) & 0x1f
                        cx2 = ((BinaryOperation.bit32ShiftL(cx2, 1))
                               | pixel) & 0x0f

                elif template == 1:
                    cxPtr0.setPointer(0, row - 2)
                    cx0 = (cxPtr0.nextPixel() << 2)
                    cx0 |= (cxPtr0.nextPixel() << 1)
                    cx0 |= cxPtr0.nextPixel()

                    cxPtr1.setPointer(0, row - 1)
                    cx1 = (cxPtr1.nextPixel() << 2)
                    cx1 |= (cxPtr1.nextPixel() << 1)
                    cx1 |= (cxPtr1.nextPixel())

                    cx2 = int()
                    atPtr0.setPointer(adaptiveTemplateX[0],
                                      row + adaptiveTemplateY[0])
                    for col in range(self._width):
                        cx = (BinaryOperation.bit32ShiftL(
                            cx0, 9)) | (BinaryOperation.bit32ShiftL(
                                cx1, 4)) | (BinaryOperation.bit32ShiftL(
                                    cx2, 1)) | atPtr0.nextPixel()
                        if useSkip and skipBitmap.getPixel(col, row) != 0:
                            pixel = 0
                        else:
                            pixel = self._arithmeticDecoder.decodeBit(
                                (cx,
                                 self._arithmeticDecoder.genericRegionStats))
                            if pixel != 0:
                                self.data.set(row * self._width + col)
                        cx0 = ((BinaryOperation.bit32ShiftL(cx0, 1))
                               | cxPtr0.nextPixel()) & 0x0f
                        cx1 = ((BinaryOperation.bit32ShiftL(cx1, 1))
                               | cxPtr1.nextPixel()) & 0x1f
                        cx2 = ((BinaryOperation.bit32ShiftL(cx2, 1))
                               | pixel) & 0x07

                elif template == 2:
                    cxPtr0.setPointer(0, row - 2)
                    cx0 = (cxPtr0.nextPixel() << 1)
                    cx0 |= (cxPtr0.nextPixel())

                    cxPtr1.setPointer(0, row - 1)
                    cx1 = (cxPtr1.nextPixel() << 1)
                    cx1 |= (cxPtr1.nextPixel())

                    cx2 = 0

                    atPtr0.setPointer(adaptiveTemplateX[0],
                                      row + adaptiveTemplateY[0])

                    for col in range(self._width):
                        cx = (BinaryOperation.bit32ShiftL(
                            cx0, 7)) | (BinaryOperation.bit32ShiftL(
                                cx1, 3)) | (BinaryOperation.bit32ShiftL(
                                    cx2, 1)) | atPtr0.nextPixel()
                        if useSkip and skipBitmap.getPixel(col, row) != 0:
                            pixel = 0
                        else:
                            pixel = self._arithmeticDecoder.decodeBit(
                                (cx,
                                 self._arithmeticDecoder.genericRegionStats))
                            if pixel != 0:
                                self.data.set(row * self._width + col)

                        cx0 = ((BinaryOperation.bit32ShiftL(cx0, 1))
                               | cxPtr0.nextPixel()) & 0x07
                        cx1 = ((BinaryOperation.bit32ShiftL(cx1, 1))
                               | cxPtr1.nextPixel()) & 0x0f
                        cx2 = ((BinaryOperation.bit32ShiftL(cx2, 1))
                               | pixel) & 0x03

                elif template == 3:
                    cxPtr1.setPointer(0, row - 1)
                    cx1 = (cxPtr1.nextPixel() << 1)
                    cx1 |= (cxPtr1.nextPixel())
                    cx2 = 0

                    atPtr0.setPointer(adaptiveTemplateX[0],
                                      row + adaptiveTemplateY[0])

                    for col in range(self._width):
                        cx = (BinaryOperation.bit32ShiftL(
                            cx1, 5)) | (BinaryOperation.bit32ShiftL(
                                cx2, 1)) | atPtr0.nextPixel()
                        if useSkip and skipBitmap.getPixel(col, row) != 0:
                            pixel = 0
                        else:
                            pixel = self._arithmeticDecoder.decodeBit(
                                cx, self._arithmeticDecoder.genericRegionStats)
                            if pixel != 0:
                                self.data.set(row * self._width + col)

                        cx1 = ((BinaryOperation.bit32ShiftL(cx1, 1))
                               | cxPtr1.nextPixel()) & 0x1f
                        cx2 = ((BinaryOperation.bit32ShiftL(cx2, 1))
                               | pixel) & 0x0f