/** * Decode JBIG2 data using Java ImageIO library. * * <p>{@inheritDoc} */ public void decode( InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex) throws IOException { /** * A working JBIG2 ImageIO plugin is needed to decode JBIG2 encoded streams. The following is * known to be working. It can't be bundled with PDFBox because of an incompatible license. * http://code.google.com/p/jbig2-imageio/ */ Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JBIG2"); if (!readers.hasNext()) { LOG.error("Can't find an ImageIO plugin to decode the JBIG2 encoded datastream."); return; } ImageReader reader = readers.next(); COSDictionary decodeP = (COSDictionary) options.getDictionaryObject(COSName.DECODE_PARMS); COSInteger bits = (COSInteger) options.getDictionaryObject(COSName.BITS_PER_COMPONENT); COSStream st = null; if (decodeP != null) { st = (COSStream) decodeP.getDictionaryObject(COSName.JBIG2_GLOBALS); } if (st != null) { reader.setInput( ImageIO.createImageInputStream( new SequenceInputStream(st.getFilteredStream(), compressedData))); } else { reader.setInput(ImageIO.createImageInputStream(compressedData)); } BufferedImage bi = reader.read(0); reader.dispose(); if (bi != null) { // I am assuming since JBIG2 is always black and white // depending on your renderer this might or might be needed if (bi.getColorModel().getPixelSize() != bits.intValue()) { if (bits.intValue() != 1) { LOG.error("Do not know how to deal with JBIG2 with more than 1 bit"); return; } BufferedImage packedImage = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_BINARY); Graphics graphics = packedImage.getGraphics(); graphics.drawImage(bi, 0, 0, null); graphics.dispose(); bi = packedImage; } DataBuffer dBuf = bi.getData().getDataBuffer(); if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) { result.write(((DataBufferByte) dBuf).getData()); } else { LOG.error("Image data buffer not of type byte but type " + dBuf.getDataType()); } } else { LOG.error("Something went wrong when decoding the JBIG2 encoded datastream."); } }
@SuppressWarnings("unchecked") public static void main_3(String[] args) throws IOException { PDDocument doc = PDDocument.load(iconFile); List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); List<COSObject> objects = doc.getDocument().getObjects(); for (COSObject cosObject : objects) { COSBase cosbase = cosObject.getObject(); if (cosObject.getObject() instanceof COSStream) { COSStream cosstream = (COSStream) cosbase; COSBase filter = cosstream.getDictionaryObject(COSName.FILTER); COSBase subtype = cosstream.getDictionaryObject(COSName.SUBTYPE); if (subtype != null && subtype.equals(COSName.IMAGE)) { System.out.println(filter); InputStream filtered = cosstream.getFilteredStream(); // PDStream stream = new PDStream(costream); System.out.println(Hex.encodeHex(IOUtils.toByteArray(filtered))); } } } for (PDPage pdPage : pages) { PDResources resources = pdPage.getResources(); Map<String, PDXObject> images = resources.getXObjects(); Set<String> keys = images.keySet(); for (String key : keys) { PDXObject image = images.get(key); byte[] imgData = image.getPDStream().getByteArray(); System.out.println(Hex.encodeHex(imgData)); } } }