private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document) throws IOException { int height = image.getHeight(); int width = image.getWidth(); int[] rgbLineBuffer = new int[width]; int bpc = image.getColorModel().getPixelSize(); ByteArrayOutputStream baos = new ByteArrayOutputStream(((width*bpc/8)+(width*bpc%8 != 0 ? 1:0))*height); try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos)) { for (int y = 0; y < height; ++y) { for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { mcios.writeBits(pixel & 0xFF, bpc); } int bitOffset = mcios.getBitOffset(); if (bitOffset != 0) { mcios.writeBits(0, 8 - bitOffset); } } mcios.flush(); } return prepareImageXObject(document, baos.toByteArray(), image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE); }
try (MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(encoded)) out.writeBits(CLEAR_TABLE, chunk); int foundCode = -1; int r; out.writeBits(foundCode, chunk); out.writeBits(CLEAR_TABLE, chunk); codeTable = createCodeTable(); out.writeBits(foundCode, chunk); out.writeBits(EOD, chunk); out.writeBits(0, 7);
private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document) throws IOException { int height = image.getHeight(); int width = image.getWidth(); int[] rgbLineBuffer = new int[width]; int bpc = image.getColorModel().getPixelSize(); ByteArrayOutputStream baos = new ByteArrayOutputStream(((width*bpc/8)+(width*bpc%8 != 0 ? 1:0))*height); MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos); for (int y = 0; y < height; ++y) { for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { mcios.writeBits(pixel & 0xFF, bpc); } int bitOffset = mcios.getBitOffset(); if (bitOffset != 0) { mcios.writeBits(0, 8 - bitOffset); } } mcios.flush(); mcios.close(); return prepareImageXObject(document, baos.toByteArray(), image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE); }
try (MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(encoded)) out.writeBits(CLEAR_TABLE, chunk); int foundCode = -1; int r; out.writeBits(foundCode, chunk); out.writeBits(CLEAR_TABLE, chunk); codeTable = createCodeTable(); out.writeBits(foundCode, chunk); out.writeBits(EOD, chunk); out.writeBits(0, 7);