public void ReadSegment() { if (JBIG2StreamDecoder.debug) { Console.WriteLine("==== Reading Generic Refinement Region ===="); } base.ReadSegment(); /** read text region segment flags */ ReadGenericRegionFlags(); short[] genericRegionAdaptiveTemplateX = new short[2]; short[] genericRegionAdaptiveTemplateY = new short[2]; int template = refinementRegionFlags.GetFlagValue(RefinementRegionFlags.GR_TEMPLATE); if (template == 0) { genericRegionAdaptiveTemplateX[0] = ReadATValue(); genericRegionAdaptiveTemplateY[0] = ReadATValue(); genericRegionAdaptiveTemplateX[1] = ReadATValue(); genericRegionAdaptiveTemplateY[1] = ReadATValue(); } if (noOfReferedToSegments == 0 || inlineImage) { PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.GetPageBitmap(); if (pageSegment.GetPageBitmapHeight() == -1 && regionBitmapYLocation + regionBitmapHeight > pageBitmap.GetHeight()) { pageBitmap.Expand(regionBitmapYLocation + regionBitmapHeight, pageSegment.GetPageInformationFlags().GetFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE)); } } if (noOfReferedToSegments > 1) { if (JBIG2StreamDecoder.debug) { Console.WriteLine("Bad reference in JBIG2 generic refinement Segment"); } return; } JBIG2Bitmap referedToBitmap; if (noOfReferedToSegments == 1) { referedToBitmap = decoder.FindBitmap(referedToSegments[0]); } else { PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.GetPageBitmap(); referedToBitmap = pageBitmap.GetSlice(regionBitmapXLocation, regionBitmapYLocation, regionBitmapWidth, regionBitmapHeight); } arithmeticDecoder.ResetRefinementStats(template, null); arithmeticDecoder.Start(); bool typicalPredictionGenericRefinementOn = refinementRegionFlags.GetFlagValue(RefinementRegionFlags.TPGDON) != 0; JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.ReadGenericRefinementRegion(template, typicalPredictionGenericRefinementOn, referedToBitmap, 0, 0, genericRegionAdaptiveTemplateX, genericRegionAdaptiveTemplateY); if (inlineImage) { PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.GetPageBitmap(); int extCombOp = regionFlags.GetFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR); pageBitmap.Combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, extCombOp); } else { bitmap.SetBitmapNumber(GetSegmentHeader().GetSegmentNumber()); decoder.AppendBitmap(bitmap); } }
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); } }
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() { if (JBIG2StreamDecoder.debug) { Console.WriteLine("==== Reading Immediate Generic Region ===="); } base.ReadSegment(); /** read text region Segment flags */ ReadGenericRegionFlags(); bool useMMR = genericRegionFlags.GetFlagValue(GenericRegionFlags.MMR) != 0; int template = genericRegionFlags.GetFlagValue(GenericRegionFlags.GB_TEMPLATE); short[] genericBAdaptiveTemplateX = new short[4]; short[] genericBAdaptiveTemplateY = new short[4]; if (!useMMR) { if (template == 0) { genericBAdaptiveTemplateX[0] = ReadATValue(); genericBAdaptiveTemplateY[0] = ReadATValue(); genericBAdaptiveTemplateX[1] = ReadATValue(); genericBAdaptiveTemplateY[1] = ReadATValue(); genericBAdaptiveTemplateX[2] = ReadATValue(); genericBAdaptiveTemplateY[2] = ReadATValue(); genericBAdaptiveTemplateX[3] = ReadATValue(); genericBAdaptiveTemplateY[3] = ReadATValue(); } else { genericBAdaptiveTemplateX[0] = ReadATValue(); genericBAdaptiveTemplateY[0] = ReadATValue(); } arithmeticDecoder.ResetGenericStats(template, null); arithmeticDecoder.Start(); } bool typicalPredictionGenericDecodingOn = genericRegionFlags.GetFlagValue(GenericRegionFlags.TPGDON) != 0; int length = segmentHeader.GetSegmentDataLength(); if (length == -1) { /** * length of data is unknown, so it needs to be determined through examination of the data. * See 7.2.7 - Segment data length of the JBIG2 specification. */ unknownLength = true; short match1; short match2; if (useMMR) { // look for 0x00 0x00 (0, 0) match1 = 0; match2 = 0; } else { // look for 0xFF 0xAC (255, 172) match1 = 255; match2 = 172; } int bytesRead = 0; while (true) { short bite1 = decoder.Readbyte(); bytesRead++; if (bite1 == match1) { short bite2 = decoder.Readbyte(); bytesRead++; if (bite2 == match2) { length = bytesRead - 2; break; } } } decoder.MovePointer(-bytesRead); } JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.Clear(0); bitmap.ReadBitmap(useMMR, template, typicalPredictionGenericDecodingOn, false, null, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, useMMR ? 0 : length - 18); if (inlineImage) { PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.GetPageBitmap(); int extCombOp = regionFlags.GetFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR); if (pageSegment.GetPageBitmapHeight() == -1 && regionBitmapYLocation + regionBitmapHeight > pageBitmap.GetHeight()) { pageBitmap.Expand(regionBitmapYLocation + regionBitmapHeight, pageSegment.GetPageInformationFlags().GetFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE)); } pageBitmap.Combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, extCombOp); } else { bitmap.SetBitmapNumber(GetSegmentHeader().GetSegmentNumber()); decoder.AppendBitmap(bitmap); } if (unknownLength) { decoder.MovePointer(4); } }