public Segment(JBIG2StreamDecoder streamDecoder) { this.decoder = streamDecoder; huffmanDecoder = decoder.getHuffmanDecoder(); arithmeticDecoder = decoder.getArithmeticDecoder(); mmrDecoder = decoder.getMMRDecoder(); }
public byte[] decodeJBIG2(byte[] data, ImageFormat format = ImageFormat.TIFF, int NewWidth = 0, int NewHeight = 0) { reader = new Big2StreamReader(data); resetDecoder(); bool validFile = checkHeader(); if (JBIG2StreamDecoder.debug) { Console.WriteLine("validFile = " + validFile); } if (!validFile) { /** * Assume this is a stream from a PDF so there is no file header, * end of page segments, or end of file segments. Organisation must * be sequential, and the number of pages is assumed to be 1. */ noOfPagesKnown = true; randomAccessOrganisation = false; noOfPages = 1; /** check to see if there is any global data to be read */ if (globalData != null) { /** set the reader to read from the global data */ reader = new Big2StreamReader(globalData); huffmanDecoder = new HuffmanDecoder(reader); mmrDecoder = new MMRDecoder(reader); arithmeticDecoder = new ArithmeticDecoder(reader); /** read in the global data segments */ readSegments(); /** set the reader back to the main data */ reader = new Big2StreamReader(data); } else { /** * There's no global data, so move the file pointer back to the * start of the stream */ reader.movePointer(-8); } } else { /** * We have the file header, so assume it is a valid stand-alone * file. */ if (JBIG2StreamDecoder.debug) { Console.WriteLine("==== File Header ===="); } setFileHeaderFlags(); if (JBIG2StreamDecoder.debug) { Console.WriteLine("randomAccessOrganisation = " + randomAccessOrganisation); Console.WriteLine("noOfPagesKnown = " + noOfPagesKnown); } if (noOfPagesKnown) { noOfPages = getNoOfPages(); if (JBIG2StreamDecoder.debug) { Console.WriteLine("noOfPages = " + noOfPages); } } } huffmanDecoder = new HuffmanDecoder(reader); mmrDecoder = new MMRDecoder(reader); arithmeticDecoder = new ArithmeticDecoder(reader); /** read in the main segment data */ readSegments(); //Create Image var rawimage = findPageSegement(1).getPageBitmap(); int width = (int)rawimage.getWidth(); int height = (int)rawimage.getHeight(); var dataStream = rawimage.getData(true); var newarray = new byte[dataStream.Length]; Array.Copy(dataStream, newarray, dataStream.Length); int stride = (width * 1 + 7) / 8; var bitmap = new WriteableBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.BlackWhite, null); bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), newarray, stride, 0); MemoryStream stream3 = new MemoryStream(); if (format == ImageFormat.TIFF) { var encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(stream3); } else if (format == ImageFormat.JPEG) { var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(stream3); } if (NewWidth != 0 && NewHeight != 0) { var newbitmap = ResizeHelpers.ScaleImage(stream3.ToArray(), NewWidth, NewHeight); return(newbitmap); } return(stream3.ToArray()); }