private void ReadSegments() { bool finished = false; while (!reader.IsFinished() && !finished) { SegmentHeader segmentHeader = new SegmentHeader(); ReadSegmentHeader(segmentHeader); // read the Segment data Segment segment = null; int segmentType = segmentHeader.GetSegmentType(); int[] referredToSegments = segmentHeader.GetReferredToSegments(); int noOfReferredToSegments = segmentHeader.GetReferredToSegmentCount(); switch (segmentType) { case Segment.SYMBOL_DICTIONARY: segment = new SymbolDictionarySegment(this); segment.SetSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_TEXT_REGION: segment = new TextRegionSegment(this, false); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_TEXT_REGION: segment = new TextRegionSegment(this, true); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_TEXT_REGION: segment = new TextRegionSegment(this, true); segment.SetSegmentHeader(segmentHeader); break; case Segment.PATTERN_DICTIONARY: segment = new PatternDictionarySegment(this); segment.SetSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_HALFTONE_REGION: segment = new HalftoneRegionSegment(this, false); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_HALFTONE_REGION: segment = new HalftoneRegionSegment(this, true); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_HALFTONE_REGION: segment = new HalftoneRegionSegment(this, true); segment.SetSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_GENERIC_REGION: segment = new GenericRegionSegment(this, false); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_GENERIC_REGION: segment = new GenericRegionSegment(this, true); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_GENERIC_REGION: segment = new GenericRegionSegment(this, true); segment.SetSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_GENERIC_REFINEMENT_REGION: segment = new RefinementRegionSegment(this, false, referredToSegments, noOfReferredToSegments); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_GENERIC_REFINEMENT_REGION: segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments); segment.SetSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_GENERIC_REFINEMENT_REGION: segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments); segment.SetSegmentHeader(segmentHeader); break; case Segment.PAGE_INFORMATION: segment = new PageInformationSegment(this); segment.SetSegmentHeader(segmentHeader); break; case Segment.END_OF_PAGE: continue; case Segment.END_OF_STRIPE: segment = new EndOfStripeSegment(this); segment.SetSegmentHeader(segmentHeader); break; case Segment.END_OF_FILE: finished = true; continue; case Segment.PROFILES: break; case Segment.TABLES: break; case Segment.EXTENSION: segment = new ExtensionSegment(this); segment.SetSegmentHeader(segmentHeader); break; default: break; } if (!randomAccessOrganisation) { segment.ReadSegment(); } segments.Add(segment); } if (randomAccessOrganisation) { foreach (Segment segment in segments) { segment.ReadSegment(); } } }
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); } }