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