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(); } }
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); } }
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() { 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); } }