Exemplo n.º 1
0
        public void Start()
        {
            buffer0 = reader.Readbyte();
            buffer1 = reader.Readbyte();

            c = BinaryOperation.Bit32ShiftL((buffer0 ^ 0xff), 16);
            Readbyte();
            c        = BinaryOperation.Bit32ShiftL(c, 7);
            counter -= 7;
            a        = 0x80000000l;
        }
Exemplo n.º 2
0
        public long DecodeIAID(long codeLen, ArithmeticDecoderStats stats)
        {
            previous = 1;
            for (long i = 0; i < codeLen; i++)
            {
                int bit = DecodeBit(previous, stats);
                previous = BinaryOperation.Bit32ShiftL(previous, 1) | bit;
            }

            return(previous - (1 << (int)codeLen));
        }
        public long Get24Bits()
        {
            while (bufferLength < 24)
            {
                buffer        = ((BinaryOperation.Bit32ShiftL(buffer, 8)) | (reader.Readbyte() & 0xff));
                bufferLength += 8;
                noOfbytesRead++;
            }

            return((BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 24))) & 0xffffff);
        }
        public int Get2DCode()
        {
            int tuple0, tuple1;

            if (bufferLength == 0)
            {
                buffer = (reader.Readbyte() & 0xff);

                bufferLength = 8;

                noOfbytesRead++;

                int lookup = (int)((BinaryOperation.Bit32ShiftR(buffer, 1)) & 0x7f);

                tuple0 = twoDimensionalTable1[lookup, 0];
                tuple1 = twoDimensionalTable1[lookup, 1];
            }
            else if (bufferLength == 8)
            {
                int lookup = (int)((BinaryOperation.Bit32ShiftR(buffer, 1)) & 0x7f);
                tuple0 = twoDimensionalTable1[lookup, 0];
                tuple1 = twoDimensionalTable1[lookup, 1];
            }
            else
            {
                int lookup = (int)((BinaryOperation.Bit32ShiftL(buffer, (int)(7 - bufferLength))) & 0x7f);

                tuple0 = twoDimensionalTable1[lookup, 0];
                tuple1 = twoDimensionalTable1[lookup, 1];
                if (tuple0 < 0 || tuple0 > (int)bufferLength)
                {
                    int right = (reader.Readbyte() & 0xff);

                    long left = (BinaryOperation.Bit32ShiftL(buffer, 8));

                    buffer        = left | right;
                    bufferLength += 8;
                    noOfbytesRead++;

                    int look = (int)(BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 7)) & 0x7f);

                    tuple0 = twoDimensionalTable1[look, 0];
                    tuple1 = twoDimensionalTable1[look, 1];
                }
            }
            if (tuple0 < 0)
            {
                return(0);
            }
            bufferLength -= tuple0;

            return(tuple1);
        }
Exemplo n.º 5
0
        private void HandleSegmentNumber(SegmentHeader segmentHeader)
        {
            short[] segmentbytes = new short[4];
            reader.Readbyte(segmentbytes);

            int segmentNumber = BinaryOperation.GetInt32(segmentbytes);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("SegmentNumber = " + segmentNumber);
            }
            segmentHeader.SetSegmentNumber(segmentNumber);
        }
Exemplo n.º 6
0
        private int DecodeIntBit(ArithmeticDecoderStats stats)
        {
            int bit = DecodeBit(previous, stats);

            if (previous < 0x100)
            {
                previous = BinaryOperation.Bit32ShiftL(previous, 1) | bit;
            }
            else
            {
                previous = (((BinaryOperation.Bit32ShiftL(previous, 1)) | bit) & 0x1ff) | 0x100;
            }
            return(bit);
        }
Exemplo n.º 7
0
        private void HandleSegmentDataLength(SegmentHeader segmentHeader)
        {
            short[] buf = new short[4];
            reader.Readbyte(buf);

            int dateLength = BinaryOperation.GetInt32(buf);

            segmentHeader.SetDataLength(dateLength);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("dateLength = " + dateLength);
            }
        }
Exemplo n.º 8
0
        private void HandleReferedToSegmentNumbers(SegmentHeader segmentHeader)
        {
            int referredToSegmentCount = segmentHeader.GetReferredToSegmentCount();

            int[] referredToSegments = new int[referredToSegmentCount];

            int segmentNumber = segmentHeader.GetSegmentNumber();

            if (segmentNumber <= 256)
            {
                for (int i = 0; i < referredToSegmentCount; i++)
                {
                    referredToSegments[i] = reader.Readbyte();
                }
            }
            else if (segmentNumber <= 65536)
            {
                short[] buf = new short[2];
                for (int i = 0; i < referredToSegmentCount; i++)
                {
                    reader.Readbyte(buf);
                    referredToSegments[i] = BinaryOperation.GetInt16(buf);
                }
            }
            else
            {
                short[] buf = new short[4];
                for (int i = 0; i < referredToSegmentCount; i++)
                {
                    reader.Readbyte(buf);
                    referredToSegments[i] = BinaryOperation.GetInt32(buf);
                }
            }

            segmentHeader.SetReferredToSegments(referredToSegments);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("referredToSegments = ");
                for (int i = 0; i < referredToSegments.Length; i++)
                {
                    Console.WriteLine(referredToSegments[i] + " ");
                }
                Console.WriteLine("");
            }
        }
        private void ReadTextRegionFlags()
        {
            /** extract text region Segment flags */
            short[] textRegionFlagsField = new short[2];
            decoder.Readbyte(textRegionFlagsField);

            int flags = BinaryOperation.GetInt16(textRegionFlagsField);

            textRegionFlags.SetFlags(flags);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("text region Segment flags = " + flags);
            }

            bool sbHuff = textRegionFlags.GetFlagValue(TextRegionFlags.SB_HUFF) != 0;

            if (sbHuff)
            {
                /** extract text region Segment Huffman flags */
                short[] textRegionHuffmanFlagsField = new short[2];
                decoder.Readbyte(textRegionHuffmanFlagsField);

                flags = BinaryOperation.GetInt16(textRegionHuffmanFlagsField);
                textRegionHuffmanFlags.SetFlags(flags);

                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine("text region segment Huffman flags = " + flags);
                }
            }

            bool sbRefine    = textRegionFlags.GetFlagValue(TextRegionFlags.SB_REFINE) != 0;
            int  sbrTemplate = textRegionFlags.GetFlagValue(TextRegionFlags.SB_R_TEMPLATE);

            if (sbRefine && sbrTemplate == 0)
            {
                symbolRegionAdaptiveTemplateX[0] = ReadATValue();
                symbolRegionAdaptiveTemplateY[0] = ReadATValue();
                symbolRegionAdaptiveTemplateX[1] = ReadATValue();
                symbolRegionAdaptiveTemplateY[1] = ReadATValue();
            }
        }
        public override void ReadSegment()
        {
            short[] buff = new short[4];
            decoder.Readbyte(buff);
            regionBitmapWidth = BinaryOperation.GetInt32(buff);

            buff = new short[4];
            decoder.Readbyte(buff);
            regionBitmapHeight = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Bitmap size = " + regionBitmapWidth + 'x' + regionBitmapHeight);
            }

            buff = new short[4];
            decoder.Readbyte(buff);
            regionBitmapXLocation = BinaryOperation.GetInt32(buff);

            buff = new short[4];
            decoder.Readbyte(buff);
            regionBitmapYLocation = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Bitmap location = " + regionBitmapXLocation + ',' + regionBitmapYLocation);
            }

            /** extract region Segment flags */
            short regionFlagsField = decoder.Readbyte();

            regionFlags.SetFlags(regionFlagsField);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("region Segment flags = " + regionFlagsField);
            }
        }
Exemplo n.º 11
0
 private void Readbyte()
 {
     if (buffer0 == 0xff)
     {
         if (buffer1 > 0x8f)
         {
             counter = 8;
         }
         else
         {
             buffer0 = buffer1;
             buffer1 = reader.Readbyte();
             c       = c + 0xfe00 - (BinaryOperation.Bit32ShiftL(buffer0, 9));
             counter = 7;
         }
     }
     else
     {
         buffer0 = buffer1;
         buffer1 = reader.Readbyte();
         c       = c + 0xff00 - (BinaryOperation.Bit32ShiftL(buffer0, 8));
         counter = 8;
     }
 }
Exemplo n.º 12
0
        private void HandlePageAssociation(SegmentHeader segmentHeader)
        {
            int pageAssociation;

            bool isPageAssociationSizeSet = segmentHeader.IsPageAssociationSizeSet();

            if (isPageAssociationSizeSet)
            { // field is 4 bytes long
                short[] buf = new short[4];
                reader.Readbyte(buf);
                pageAssociation = BinaryOperation.GetInt32(buf);
            }
            else
            { // field is 1 byte long
                pageAssociation = reader.Readbyte();
            }

            segmentHeader.SetPageAssociation(pageAssociation);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("pageAssociation = " + pageAssociation);
            }
        }
Exemplo n.º 13
0
 private int GetNoOfPages()
 {
     short[] noOfPages = new short[4];
     reader.Readbyte(noOfPages);
     return(BinaryOperation.GetInt32(noOfPages));
 }
        public int GetBlackCode()
        {
            int  tuple0, tuple1;
            long code;

            if (bufferLength == 0)
            {
                buffer       = (reader.Readbyte() & 0xff);
                bufferLength = 8;
                noOfbytesRead++;
            }
            while (true)
            {
                if (bufferLength >= 6 && ((BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 6))) & 0x3f) == 0)
                {
                    if (bufferLength <= 13)
                    {
                        code = BinaryOperation.Bit32ShiftL(buffer, (int)(13 - bufferLength));
                    }
                    else
                    {
                        code = BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 13));
                    }
                    tuple0 = blackTable1[(int)(code & 0x7f), 0];
                    tuple1 = blackTable1[(int)(code & 0x7f), 1];
                }
                else if (bufferLength >= 4 && ((buffer >> ((int)bufferLength - 4)) & 0x0f) == 0)
                {
                    if (bufferLength <= 12)
                    {
                        code = BinaryOperation.Bit32ShiftL(buffer, (int)(12 - bufferLength));
                    }
                    else
                    {
                        code = BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 12));
                    }

                    int lookup = (int)((code & 0xff) - 64);
                    if (lookup >= 0)
                    {
                        tuple0 = blackTable2[lookup, 0];
                        tuple1 = blackTable2[lookup, 1];
                    }
                    else
                    {
                        tuple0 = blackTable1[blackTable1.Length + lookup, 0];
                        tuple1 = blackTable1[blackTable1.Length + lookup, 1];
                    }
                }
                else
                {
                    if (bufferLength <= 6)
                    {
                        code = BinaryOperation.Bit32ShiftL(buffer, (int)(6 - bufferLength));
                    }
                    else
                    {
                        code = BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 6));
                    }

                    int lookup = (int)(code & 0x3f);
                    if (lookup >= 0)
                    {
                        tuple0 = blackTable3[lookup, 0];
                        tuple1 = blackTable3[lookup, 1];
                    }
                    else
                    {
                        tuple0 = blackTable2[blackTable2.Length + lookup, 0];
                        tuple1 = blackTable2[blackTable2.Length + lookup, 1];
                    }
                }
                if (tuple0 > 0 && tuple0 <= (int)bufferLength)
                {
                    bufferLength -= tuple0;
                    return(tuple1);
                }
                if (bufferLength >= 13)
                {
                    break;
                }
                buffer        = ((BinaryOperation.Bit32ShiftL(buffer, 8)) | (reader.Readbyte() & 0xff));
                bufferLength += 8;
                noOfbytesRead++;
            }

            bufferLength--;
            return(1);
        }
        public int GetWhiteCode()
        {
            int  tuple0, tuple1;
            long code;

            if (bufferLength == 0)
            {
                buffer       = (reader.Readbyte() & 0xff);
                bufferLength = 8;
                noOfbytesRead++;
            }
            while (true)
            {
                if (bufferLength >= 7 && ((BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 7))) & 0x7f) == 0)
                {
                    if (bufferLength <= 12)
                    {
                        code = BinaryOperation.Bit32ShiftL(buffer, (int)(12 - bufferLength));
                    }
                    else
                    {
                        code = BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 12));
                    }

                    tuple0 = whiteTable1[(int)(code & 0x1f), 0];
                    tuple1 = whiteTable1[(int)(code & 0x1f), 1];
                }
                else
                {
                    if (bufferLength <= 9)
                    {
                        code = BinaryOperation.Bit32ShiftL(buffer, (int)(9 - bufferLength));
                    }
                    else
                    {
                        code = BinaryOperation.Bit32ShiftR(buffer, (int)(bufferLength - 9));
                    }

                    int lookup = (int)(code & 0x1ff);
                    if (lookup >= 0)
                    {
                        tuple0 = whiteTable2[lookup, 0];
                        tuple1 = whiteTable2[lookup, 1];
                    }
                    else
                    {
                        tuple0 = whiteTable2[whiteTable2.Length + lookup, 0];
                        tuple1 = whiteTable2[whiteTable2.Length + lookup, 1];
                    }
                }
                if (tuple0 > 0 && tuple0 <= (int)bufferLength)
                {
                    bufferLength -= tuple0;
                    return(tuple1);
                }
                if (bufferLength >= 12)
                {
                    break;
                }
                buffer        = ((BinaryOperation.Bit32ShiftL(buffer, 8)) | reader.Readbyte() & 0xff);
                bufferLength += 8;
                noOfbytesRead++;
            }

            bufferLength--;

            return(1);
        }
Exemplo n.º 16
0
        public override void ReadSegment()
        {
            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("==== Reading Page Information Dictionary ====");
            }

            short[] buff = new short[4];
            decoder.Readbyte(buff);
            pageBitmapWidth = BinaryOperation.GetInt32(buff);

            buff = new short[4];
            decoder.Readbyte(buff);
            pageBitmapHeight = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Bitmap size = " + pageBitmapWidth + 'x' + pageBitmapHeight);
            }

            buff = new short[4];
            decoder.Readbyte(buff);
            xResolution = BinaryOperation.GetInt32(buff);

            buff = new short[4];
            decoder.Readbyte(buff);
            yResolution = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Resolution = " + xResolution + 'x' + yResolution);
            }

            /** extract page information flags */
            short pageInformationFlagsField = decoder.Readbyte();

            pageInformationFlags.SetFlags(pageInformationFlagsField);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("symbolDictionaryFlags = " + pageInformationFlagsField);
            }

            buff = new short[2];
            decoder.Readbyte(buff);
            pageStriping = BinaryOperation.GetInt16(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Page Striping = " + pageStriping);
            }

            int defPix = pageInformationFlags.GetFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE);

            int height;

            if (pageBitmapHeight == -1)
            {
                height = pageStriping & 0x7fff;
            }
            else
            {
                height = pageBitmapHeight;
            }

            pageBitmap = new JBIG2Bitmap(pageBitmapWidth, height, arithmeticDecoder, huffmanDecoder, mmrDecoder);
            pageBitmap.Clear(defPix);
        }
        public override void ReadSegment()
        {
            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("==== Reading Text Region ====");
            }

            base.ReadSegment();

            /** read text region Segment flags */
            ReadTextRegionFlags();

            short[] buff = new short[4];
            decoder.Readbyte(buff);
            long noOfSymbolInstances = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("noOfSymbolInstances = " + noOfSymbolInstances);
            }

            int noOfReferredToSegments = segmentHeader.GetReferredToSegmentCount();

            int[] referredToSegments = segmentHeader.GetReferredToSegments();

            //List codeTables = new ArrayList();
            List <Segment> segmentsReferenced = new List <Segment>();
            long           noOfSymbols        = 0;

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("noOfReferredToSegments = " + noOfReferredToSegments);
            }
            int i;             // i = 0;

            for (i = 0; i < noOfReferredToSegments; i++)
            {
                Segment seg  = decoder.FindSegment(referredToSegments[i]);
                int     type = seg.GetSegmentHeader().GetSegmentType();

                if (type == Segment.SYMBOL_DICTIONARY)
                {
                    segmentsReferenced.Add(seg);
                    noOfSymbols += ((SymbolDictionarySegment)seg).GetNoOfExportedSymbols();
                }
                else if (type == Segment.TABLES)
                {
                    //codeTables.add(seg);
                }
            }

            long symbolCodeLength = 0;
            int  count            = 1;

            while (count < noOfSymbols)
            {
                symbolCodeLength++;
                count <<= 1;
            }

            int currentSymbol = 0;

            JBIG2Bitmap[] symbols = new JBIG2Bitmap[noOfSymbols];
            foreach (Segment it in segmentsReferenced)
            {
                if (it.GetSegmentHeader().GetSegmentType() == Segment.SYMBOL_DICTIONARY)
                {
                    JBIG2Bitmap[] bitmaps = ((SymbolDictionarySegment)it).GetBitmaps();
                    for (int j = 0; j < bitmaps.Length; j++)
                    {
                        symbols[currentSymbol] = bitmaps[j];
                        currentSymbol++;
                    }
                }
            }

            long[,] huffmanFSTable = null;
            long[,] huffmanDSTable = null;
            long[,] huffmanDTTable = null;

            long[,] huffmanRDWTable = null;
            long[,] huffmanRDHTable = null;

            long[,] huffmanRDXTable   = null;
            long[,] huffmanRDYTable   = null;
            long[,] huffmanRSizeTable = null;

            bool sbHuffman = textRegionFlags.GetFlagValue(TextRegionFlags.SB_HUFF) != 0;

            i = 0;
            if (sbHuffman)
            {
                int sbHuffFS = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_FS);
                if (sbHuffFS == 0)
                {
                    huffmanFSTable = HuffmanDecoder.huffmanTableF;
                }
                else if (sbHuffFS == 1)
                {
                    huffmanFSTable = HuffmanDecoder.huffmanTableG;
                }
                else
                {
                }

                int sbHuffDS = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_DS);
                if (sbHuffDS == 0)
                {
                    huffmanDSTable = HuffmanDecoder.huffmanTableH;
                }
                else if (sbHuffDS == 1)
                {
                    huffmanDSTable = HuffmanDecoder.huffmanTableI;
                }
                else if (sbHuffDS == 2)
                {
                    huffmanDSTable = HuffmanDecoder.huffmanTableJ;
                }
                else
                {
                }

                int sbHuffDT = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_DT);
                if (sbHuffDT == 0)
                {
                    huffmanDTTable = HuffmanDecoder.huffmanTableK;
                }
                else if (sbHuffDT == 1)
                {
                    huffmanDTTable = HuffmanDecoder.huffmanTableL;
                }
                else if (sbHuffDT == 2)
                {
                    huffmanDTTable = HuffmanDecoder.huffmanTableM;
                }
                else
                {
                }

                int sbHuffRDW = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDW);
                if (sbHuffRDW == 0)
                {
                    huffmanRDWTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDW == 1)
                {
                    huffmanRDWTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRDH = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDH);
                if (sbHuffRDH == 0)
                {
                    huffmanRDHTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDH == 1)
                {
                    huffmanRDHTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRDX = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDX);
                if (sbHuffRDX == 0)
                {
                    huffmanRDXTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDX == 1)
                {
                    huffmanRDXTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRDY = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDY);
                if (sbHuffRDY == 0)
                {
                    huffmanRDYTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDY == 1)
                {
                    huffmanRDYTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRSize = textRegionHuffmanFlags.GetFlagValue(TextRegionHuffmanFlags.SB_HUFF_RSIZE);
                if (sbHuffRSize == 0)
                {
                    huffmanRSizeTable = HuffmanDecoder.huffmanTableA;
                }
                else
                {
                }
            }

            long[][] runLengthTable  = new long[36][];
            long[][] symbolCodeTable = new long[noOfSymbols + 1][];
            if (sbHuffman)
            {
                decoder.ConsumeRemainingBits();

                for (i = 0; i < 32; i++)
                {
                    runLengthTable[i] = new long[] { i, decoder.ReadBits(4), 0, 0 };
                }

                runLengthTable[32] = new long[] { 0x103, decoder.ReadBits(4), 2, 0 };

                runLengthTable[33] = new long[] { 0x203, decoder.ReadBits(4), 3, 0 };

                runLengthTable[34] = new long[] { 0x20b, decoder.ReadBits(4), 7, 0 };

                runLengthTable[35] = new long[] { 0, 0, HuffmanDecoder.jbig2HuffmanEOT };

                runLengthTable = HuffmanDecoder.BuildTable(runLengthTable, 35);

                for (i = 0; i < noOfSymbols; i++)
                {
                    symbolCodeTable[i] = new long[] { i, 0, 0, 0 };
                }

                i = 0;
                while (i < noOfSymbols)
                {
                    long j = huffmanDecoder.DecodeInt(runLengthTable).IntResult();
                    if (j > 0x200)
                    {
                        for (j -= 0x200; j != 0 && i < noOfSymbols; j--)
                        {
                            symbolCodeTable[i++][1] = 0;
                        }
                    }
                    else if (j > 0x100)
                    {
                        for (j -= 0x100; j != 0 && i < noOfSymbols; j--)
                        {
                            symbolCodeTable[i][1] = symbolCodeTable[i - 1][1];
                            i++;
                        }
                    }
                    else
                    {
                        symbolCodeTable[i++][1] = j;
                    }
                }

                symbolCodeTable[noOfSymbols][1] = 0;
                symbolCodeTable[noOfSymbols][2] = HuffmanDecoder.jbig2HuffmanEOT;
                symbolCodeTable = HuffmanDecoder.BuildTable(symbolCodeTable, (int)noOfSymbols);

                decoder.ConsumeRemainingBits();
            }
            else
            {
                symbolCodeTable = null;
                arithmeticDecoder.ResetIntStats((int)symbolCodeLength);
                arithmeticDecoder.Start();
            }

            bool symbolRefine        = textRegionFlags.GetFlagValue(TextRegionFlags.SB_REFINE) != 0;
            long logStrips           = textRegionFlags.GetFlagValue(TextRegionFlags.LOG_SB_STRIPES);
            int  defaultPixel        = textRegionFlags.GetFlagValue(TextRegionFlags.SB_DEF_PIXEL);
            int  combinationOperator = textRegionFlags.GetFlagValue(TextRegionFlags.SB_COMB_OP);
            bool transposed          = textRegionFlags.GetFlagValue(TextRegionFlags.TRANSPOSED) != 0;
            int  referenceCorner     = textRegionFlags.GetFlagValue(TextRegionFlags.REF_CORNER);
            int  sOffset             = textRegionFlags.GetFlagValue(TextRegionFlags.SB_DS_OFFSET);
            int  template            = textRegionFlags.GetFlagValue(TextRegionFlags.SB_R_TEMPLATE);

            if (symbolRefine)
            {
                arithmeticDecoder.ResetRefinementStats(template, null);
            }

            JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);

            bitmap.ReadTextRegion2(sbHuffman, symbolRefine, noOfSymbolInstances, logStrips, noOfSymbols, symbolCodeTable, symbolCodeLength, symbols, defaultPixel, combinationOperator, transposed, referenceCorner, sOffset, huffmanFSTable, huffmanDSTable, huffmanDTTable, huffmanRDWTable, huffmanRDHTable, huffmanRDXTable, huffmanRDYTable, huffmanRSizeTable, template, symbolRegionAdaptiveTemplateX, symbolRegionAdaptiveTemplateY, decoder);

            if (inlineImage)
            {
                PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.GetPageBitmap();

                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine(pageBitmap + " " + bitmap);
                }

                int externalCombinationOperator = regionFlags.GetFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR);
                pageBitmap.Combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, externalCombinationOperator);
            }
            else
            {
                bitmap.SetBitmapNumber(GetSegmentHeader().GetSegmentNumber());
                decoder.AppendBitmap(bitmap);
            }

            decoder.ConsumeRemainingBits();
        }
        public override void ReadSegment()
        {
            base.ReadSegment();

            /** read text region Segment flags */
            ReadHalftoneRegionFlags();

            short[] buf = new short[4];
            decoder.Readbyte(buf);
            int gridWidth = BinaryOperation.GetInt32(buf);

            buf = new short[4];
            decoder.Readbyte(buf);
            int gridHeight = BinaryOperation.GetInt32(buf);

            buf = new short[4];
            decoder.Readbyte(buf);
            int gridX = BinaryOperation.GetInt32(buf);

            buf = new short[4];
            decoder.Readbyte(buf);
            int gridY = BinaryOperation.GetInt32(buf);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("grid pos and size = " + gridX + ',' + gridY + ' ' + gridWidth + ',' + gridHeight);
            }

            buf = new short[2];
            decoder.Readbyte(buf);
            int stepX = BinaryOperation.GetInt16(buf);

            buf = new short[2];
            decoder.Readbyte(buf);
            int stepY = BinaryOperation.GetInt16(buf);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("step size = " + stepX + ',' + stepY);
            }

            int[] referedToSegments = segmentHeader.GetReferredToSegments();
            if (referedToSegments.Length != 1)
            {
                Console.WriteLine("Error in halftone Segment. refSegs should == 1");
            }

            Segment segment = decoder.FindSegment(referedToSegments[0]);

            if (segment.GetSegmentHeader().GetSegmentType() != Segment.PATTERN_DICTIONARY)
            {
                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine("Error in halftone Segment. bad symbol dictionary reference");
                }
            }

            PatternDictionarySegment patternDictionarySegment = (PatternDictionarySegment)segment;

            int bitsPerValue = 0, i = 1;

            while (i < patternDictionarySegment.GetSize())
            {
                bitsPerValue++;
                i <<= 1;
            }

            JBIG2Bitmap bitmap        = patternDictionarySegment.GetBitmaps()[0];
            long        patternWidth  = bitmap.GetWidth();
            long        patternHeight = bitmap.GetHeight();

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("pattern size = " + patternWidth + ',' + patternHeight);
            }

            bool useMMR   = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_MMR) != 0;
            int  template = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_TEMPLATE);

            if (!useMMR)
            {
                arithmeticDecoder.ResetGenericStats(template, null);
                arithmeticDecoder.Start();
            }

            int halftoneDefaultPixel = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_DEF_PIXEL);

            bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);
            bitmap.Clear(halftoneDefaultPixel);

            bool enableSkip = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_ENABLE_SKIP) != 0;

            JBIG2Bitmap skipBitmap = null;

            if (enableSkip)
            {
                skipBitmap = new JBIG2Bitmap(gridWidth, gridHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);
                skipBitmap.Clear(0);
                for (int y = 0; y < gridHeight; y++)
                {
                    for (int x = 0; x < gridWidth; x++)
                    {
                        int xx = gridX + y * stepY + x * stepX;
                        int yy = gridY + y * stepX - x * stepY;

                        if (((xx + patternWidth) >> 8) <= 0 || (xx >> 8) >= regionBitmapWidth || ((yy + patternHeight) >> 8) <= 0 || (yy >> 8) >= regionBitmapHeight)
                        {
                            skipBitmap.SetPixel(y, x, 1);
                        }
                    }
                }
            }

            int[] grayScaleImage = new int[gridWidth * gridHeight];

            short[] genericBAdaptiveTemplateX = new short[4], genericBAdaptiveTemplateY = new short[4];

            genericBAdaptiveTemplateX[0] = (short)(template <= 1 ? 3 : 2);
            genericBAdaptiveTemplateY[0] = -1;
            genericBAdaptiveTemplateX[1] = -3;
            genericBAdaptiveTemplateY[1] = -1;
            genericBAdaptiveTemplateX[2] = 2;
            genericBAdaptiveTemplateY[2] = -2;
            genericBAdaptiveTemplateX[3] = -2;
            genericBAdaptiveTemplateY[3] = -2;

            JBIG2Bitmap grayBitmap;

            for (int j = bitsPerValue - 1; j >= 0; --j)
            {
                grayBitmap = new JBIG2Bitmap(gridWidth, gridHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);

                grayBitmap.ReadBitmap(useMMR, template, false, enableSkip, skipBitmap, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, -1);

                i = 0;
                for (int row = 0; row < gridHeight; row++)
                {
                    for (int col = 0; col < gridWidth; col++)
                    {
                        int bit = grayBitmap.GetPixel(col, row) ^ grayScaleImage[i] & 1;
                        grayScaleImage[i] = (grayScaleImage[i] << 1) | bit;
                        i++;
                    }
                }
            }

            int combinationOperator = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_COMB_OP);

            i = 0;
            for (int col = 0; col < gridHeight; col++)
            {
                int xx = gridX + col * stepY;
                int yy = gridY + col * stepX;
                for (int row = 0; row < gridWidth; row++)
                {
                    if (!(enableSkip && skipBitmap.GetPixel(col, row) == 1))
                    {
                        JBIG2Bitmap patternBitmap = patternDictionarySegment.GetBitmaps()[grayScaleImage[i]];
                        bitmap.Combine(patternBitmap, xx >> 8, yy >> 8, combinationOperator);
                    }

                    xx += stepX;
                    yy -= stepY;

                    i++;
                }
            }

            if (inlineImage)
            {
                PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.GetPageBitmap();

                int externalCombinationOperator = regionFlags.GetFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR);
                pageBitmap.Combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, externalCombinationOperator);
            }
            else
            {
                bitmap.SetBitmapNumber(GetSegmentHeader().GetSegmentNumber());
                decoder.AppendBitmap(bitmap);
            }
        }
Exemplo n.º 19
0
        public DecodeIntResult DecodeInt(ArithmeticDecoderStats stats)
        {
            long value;

            previous = 1;
            int s = DecodeIntBit(stats);

            if (DecodeIntBit(stats) != 0)
            {
                if (DecodeIntBit(stats) != 0)
                {
                    if (DecodeIntBit(stats) != 0)
                    {
                        if (DecodeIntBit(stats) != 0)
                        {
                            if (DecodeIntBit(stats) != 0)
                            {
                                value = 0;
                                for (int i = 0; i < 32; i++)
                                {
                                    value = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                                }
                                value += 4436;
                            }
                            else
                            {
                                value = 0;
                                for (int i = 0; i < 12; i++)
                                {
                                    value = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                                }
                                value += 340;
                            }
                        }
                        else
                        {
                            value = 0;
                            for (int i = 0; i < 8; i++)
                            {
                                value = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                            }
                            value += 84;
                        }
                    }
                    else
                    {
                        value = 0;
                        for (int i = 0; i < 6; i++)
                        {
                            value = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                        }
                        value += 20;
                    }
                }
                else
                {
                    value  = DecodeIntBit(stats);
                    value  = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                    value  = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                    value  = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
                    value += 4;
                }
            }
            else
            {
                value = DecodeIntBit(stats);
                value = BinaryOperation.Bit32ShiftL(value, 1) | DecodeIntBit(stats);
            }

            int decodedInt;

            if (s != 0)
            {
                if (value == 0)
                {
                    return(new DecodeIntResult((int)value, false));
                }
                decodedInt = (int)-value;
            }
            else
            {
                decodedInt = (int)value;
            }

            return(new DecodeIntResult(decodedInt, true));
        }
Exemplo n.º 20
0
        public int DecodeBit(long context, ArithmeticDecoderStats stats)
        {
            int iCX   = BinaryOperation.Bit8Shift(stats.GetContextCodingTableValue((int)context), 1, BinaryOperation.RIGHT_SHIFT);
            int mpsCX = stats.GetContextCodingTableValue((int)context) & 1;
            int qe    = qeTable[iCX];

            a -= qe;

            int bit;

            if (c < a)
            {
                if ((a & 0x80000000) != 0)
                {
                    bit = mpsCX;
                }
                else
                {
                    if (a < qe)
                    {
                        bit = 1 - mpsCX;
                        if (switchTable[iCX] != 0)
                        {
                            stats.SetContextCodingTableValue((int)context, (nlpsTable[iCX] << 1) | (1 - mpsCX));
                        }
                        else
                        {
                            stats.SetContextCodingTableValue((int)context, (nlpsTable[iCX] << 1) | mpsCX);
                        }
                    }
                    else
                    {
                        bit = mpsCX;
                        stats.SetContextCodingTableValue((int)context, (nmpsTable[iCX] << 1) | mpsCX);
                    }
                    do
                    {
                        if (counter == 0)
                        {
                            Readbyte();
                        }

                        a = BinaryOperation.Bit32ShiftL(a, 1);
                        c = BinaryOperation.Bit32ShiftL(c, 1);

                        counter--;
                    } while ((a & 0x80000000) == 0);
                }
            }
            else
            {
                c -= a;

                if (a < qe)
                {
                    bit = mpsCX;
                    stats.SetContextCodingTableValue((int)context, (nmpsTable[iCX] << 1) | mpsCX);
                }
                else
                {
                    bit = 1 - mpsCX;
                    if (switchTable[iCX] != 0)
                    {
                        stats.SetContextCodingTableValue((int)context, (nlpsTable[iCX] << 1) | (1 - mpsCX));
                    }
                    else
                    {
                        stats.SetContextCodingTableValue((int)context, (nlpsTable[iCX] << 1) | mpsCX);
                    }
                }
                a = qe;

                do
                {
                    if (counter == 0)
                    {
                        Readbyte();
                    }

                    a = BinaryOperation.Bit32ShiftL(a, 1);
                    c = BinaryOperation.Bit32ShiftL(c, 1);

                    counter--;
                } while ((a & 0x80000000) == 0);
            }
            return(bit);
        }
Exemplo n.º 21
0
        private void HandleSegmentReferredToCountAndRententionFlags(SegmentHeader segmentHeader)
        {
            short referedToSegmentCountAndRetentionFlags = reader.Readbyte();

            int referredToSegmentCount = (referedToSegmentCountAndRetentionFlags & 224) >> 5; // 224

            // =
            // 11100000
            short[] retentionFlags = null;
            /** take off the first three bits of the first byte */
            short firstbyte = (short)(referedToSegmentCountAndRetentionFlags & 31); // 31 =

            // 00011111

            if (referredToSegmentCount <= 4)
            { // short form
                retentionFlags    = new short[1];
                retentionFlags[0] = firstbyte;
            }
            else if (referredToSegmentCount == 7)
            { // long form
                short[] longFormCountAndFlags = new short[4];
                /** add the first byte of the four */
                longFormCountAndFlags[0] = firstbyte;

                for (int i = 1; i < 4; i++)
                {
                    // add the next 3 bytes to the array
                    longFormCountAndFlags[i] = reader.Readbyte();
                }

                /** get the count of the referred to Segments */
                referredToSegmentCount = BinaryOperation.GetInt32(longFormCountAndFlags);

                /** calculate the number of bytes in this field */
                int noOfbytesInField = (int)Math.Ceiling(4 + ((referredToSegmentCount + 1) / 8d));
                // System.out.println("noOfbytesInField = " + noOfbytesInField);

                int noOfRententionFlagbytes = noOfbytesInField - 4;
                retentionFlags = new short[noOfRententionFlagbytes];
                reader.Readbyte(retentionFlags);
            }
            else
            { // error
              //throw new JBIG2Exception("Error, 3 bit Segment count field = " + referredToSegmentCount);
            }

            segmentHeader.SetReferredToSegmentCount(referredToSegmentCount);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("referredToSegmentCount = " + referredToSegmentCount);
            }

            segmentHeader.SetRententionFlags(retentionFlags);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("retentionFlags = ");
            }

            if (JBIG2StreamDecoder.debug)
            {
                for (int i = 0; i < retentionFlags.Length; i++)
                {
                    Console.WriteLine(retentionFlags[i] + " ");
                }
                Console.WriteLine("");
            }
        }
Exemplo n.º 22
0
        private void ReadSymbolDictionaryFlags()
        {
            /** extract symbol dictionary flags */
            short[] symbolDictionaryFlagsField = new short[2];
            decoder.Readbyte(symbolDictionaryFlagsField);

            int flags = BinaryOperation.GetInt16(symbolDictionaryFlagsField);

            symbolDictionaryFlags.SetFlags(flags);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("symbolDictionaryFlags = " + flags);
            }

            // symbol dictionary AT flags
            int sdHuff     = symbolDictionaryFlags.GetFlagValue(SymbolDictionaryFlags.SD_HUFF);
            int sdTemplate = symbolDictionaryFlags.GetFlagValue(SymbolDictionaryFlags.SD_TEMPLATE);

            if (sdHuff == 0)
            {
                if (sdTemplate == 0)
                {
                    symbolDictionaryAdaptiveTemplateX[0] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateY[0] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateX[1] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateY[1] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateX[2] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateY[2] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateX[3] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateY[3] = ReadATValue();
                }
                else
                {
                    symbolDictionaryAdaptiveTemplateX[0] = ReadATValue();
                    symbolDictionaryAdaptiveTemplateY[0] = ReadATValue();
                }
            }

            // symbol dictionary refinement AT flags
            int refAgg      = symbolDictionaryFlags.GetFlagValue(SymbolDictionaryFlags.SD_REF_AGG);
            int sdrTemplate = symbolDictionaryFlags.GetFlagValue(SymbolDictionaryFlags.SD_R_TEMPLATE);

            if (refAgg != 0 && sdrTemplate == 0)
            {
                symbolDictionaryRAdaptiveTemplateX[0] = ReadATValue();
                symbolDictionaryRAdaptiveTemplateY[0] = ReadATValue();
                symbolDictionaryRAdaptiveTemplateX[1] = ReadATValue();
                symbolDictionaryRAdaptiveTemplateY[1] = ReadATValue();
            }

            /** extract no of exported symbols */
            short[] noOfExportedSymbolsField = new short[4];
            decoder.Readbyte(noOfExportedSymbolsField);

            int noOfExportedSymbols = BinaryOperation.GetInt32(noOfExportedSymbolsField);

            this.noOfExportedSymbols = noOfExportedSymbols;

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("noOfExportedSymbols = " + noOfExportedSymbols);
            }

            /** extract no of new symbols */
            short[] noOfNewSymbolsField = new short[4];
            decoder.Readbyte(noOfNewSymbolsField);

            int noOfNewSymbols = BinaryOperation.GetInt32(noOfNewSymbolsField);

            this.noOfNewSymbols = noOfNewSymbols;

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("noOfNewSymbols = " + noOfNewSymbols);
            }
        }
Exemplo n.º 23
0
        public override void ReadSegment()
        {
            /** read text region Segment flags */
            ReadPatternDictionaryFlags();

            width  = decoder.Readbyte();
            height = decoder.Readbyte();

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("pattern dictionary size = " + width + " , " + height);
            }

            short[] buf = new short[4];
            decoder.Readbyte(buf);
            grayMax = BinaryOperation.GetInt32(buf);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("grey max = " + grayMax);
            }

            bool useMMR   = patternDictionaryFlags.GetFlagValue(PatternDictionaryFlags.HD_MMR) == 1;
            int  template = patternDictionaryFlags.GetFlagValue(PatternDictionaryFlags.HD_TEMPLATE);

            if (!useMMR)
            {
                arithmeticDecoder.ResetGenericStats(template, null);
                arithmeticDecoder.Start();
            }

            short[] genericBAdaptiveTemplateX = new short[4], genericBAdaptiveTemplateY = new short[4];

            genericBAdaptiveTemplateX[0] = (short)-width;
            genericBAdaptiveTemplateY[0] = 0;
            genericBAdaptiveTemplateX[1] = -3;
            genericBAdaptiveTemplateY[1] = -1;
            genericBAdaptiveTemplateX[2] = 2;
            genericBAdaptiveTemplateY[2] = -2;
            genericBAdaptiveTemplateX[3] = -2;
            genericBAdaptiveTemplateY[3] = -2;

            size = grayMax + 1;

            JBIG2Bitmap bitmap = new JBIG2Bitmap(size * width, height, arithmeticDecoder, huffmanDecoder, mmrDecoder);

            bitmap.Clear(0);
            bitmap.ReadBitmap(useMMR, template, false, false, null, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, segmentHeader.GetSegmentDataLength() - 7);

            JBIG2Bitmap[] bitmaps = new JBIG2Bitmap[size];

            int x = 0;

            for (int i = 0; i < size; i++)
            {
                bitmaps[i] = bitmap.GetSlice(x, 0, width, height);
                x         += width;
            }

            this.bitmaps = bitmaps;
        }