List of usage examples for javax.imageio.stream ImageInputStream readBits
long readBits(int numBits) throws IOException;
From source file:org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java
private static BufferedImage fromAny(PDImage pdImage, WritableRaster raster, COSArray colorKey) throws IOException { final PDColorSpace colorSpace = pdImage.getColorSpace(); final int numComponents = colorSpace.getNumberOfComponents(); final int width = pdImage.getWidth(); final int height = pdImage.getHeight(); final int bitsPerComponent = pdImage.getBitsPerComponent(); final float[] decode = getDecodeArray(pdImage); // read bit stream ImageInputStream iis = null; try {/*ww w .java2 s .c o m*/ // create stream iis = new MemoryCacheImageInputStream(pdImage.createInputStream()); final float sampleMax = (float) Math.pow(2, bitsPerComponent) - 1f; final boolean isIndexed = colorSpace instanceof PDIndexed; // init color key mask float[] colorKeyRanges = null; BufferedImage colorKeyMask = null; if (colorKey != null) { colorKeyRanges = colorKey.toFloatArray(); colorKeyMask = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } // calculate row padding int padding = 0; if (width * numComponents * bitsPerComponent % 8 > 0) { padding = 8 - (width * numComponents * bitsPerComponent % 8); } // read stream byte[] srcColorValues = new byte[numComponents]; byte[] alpha = new byte[1]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { boolean isMasked = true; for (int c = 0; c < numComponents; c++) { int value = (int) iis.readBits(bitsPerComponent); // color key mask requires values before they are decoded if (colorKeyRanges != null) { isMasked &= value >= colorKeyRanges[c * 2] && value <= colorKeyRanges[c * 2 + 1]; } // decode array final float dMin = decode[c * 2]; final float dMax = decode[(c * 2) + 1]; // interpolate to domain float output = dMin + (value * ((dMax - dMin) / sampleMax)); if (isIndexed) { // indexed color spaces get the raw value, because the TYPE_BYTE // below cannot be reversed by the color space without it having // knowledge of the number of bits per component srcColorValues[c] = (byte) Math.round(output); } else { // interpolate to TYPE_BYTE int outputByte = Math .round(((output - Math.min(dMin, dMax)) / Math.abs(dMax - dMin)) * 255f); srcColorValues[c] = (byte) outputByte; } } raster.setDataElements(x, y, srcColorValues); // set alpha channel in color key mask, if any if (colorKeyMask != null) { alpha[0] = (byte) (isMasked ? 255 : 0); colorKeyMask.getRaster().setDataElements(x, y, alpha); } } // rows are padded to the nearest byte iis.readBits(padding); } // use the color space to convert the image to RGB BufferedImage rgbImage = colorSpace.toRGBImage(raster); // apply color mask, if any if (colorKeyMask != null) { return applyColorKeyMask(rgbImage, colorKeyMask); } else { return rgbImage; } } finally { if (iis != null) { iis.close(); } } }
From source file:org.apache.pdfbox.pdmodel.graphics.shading.GouraudShadingContext.java
/** * Read a vertex from the bit input stream performs interpolations. * * @param input bit input stream//from w w w . ja v a2s . co m * @param maxSrcCoord max value for source coordinate (2^bits-1) * @param maxSrcColor max value for source color (2^bits-1) * @param rangeX dest range for X * @param rangeY dest range for Y * @param colRangeTab dest range array for colors * @param matrix the pattern matrix concatenated with that of the parent content stream * @return a new vertex with the flag and the interpolated values * @throws IOException if something went wrong */ protected Vertex readVertex(ImageInputStream input, long maxSrcCoord, long maxSrcColor, PDRange rangeX, PDRange rangeY, PDRange[] colRangeTab, Matrix matrix, AffineTransform xform) throws IOException { float[] colorComponentTab = new float[numberOfColorComponents]; long x = input.readBits(bitsPerCoordinate); long y = input.readBits(bitsPerCoordinate); float dstX = interpolate(x, maxSrcCoord, rangeX.getMin(), rangeX.getMax()); float dstY = interpolate(y, maxSrcCoord, rangeY.getMin(), rangeY.getMax()); LOG.debug("coord: " + String.format("[%06X,%06X] -> [%f,%f]", x, y, dstX, dstY)); Point2D p = matrix.transformPoint(dstX, dstY); xform.transform(p, p); for (int n = 0; n < numberOfColorComponents; ++n) { int color = (int) input.readBits(bitsPerColorComponent); colorComponentTab[n] = interpolate(color, maxSrcColor, colRangeTab[n].getMin(), colRangeTab[n].getMax()); LOG.debug("color[" + n + "]: " + color + "/" + String.format("%02x", color) + "-> color[" + n + "]: " + colorComponentTab[n]); } return new Vertex(p, colorComponentTab); }
From source file:org.apache.pdfbox.pdmodel.graphics.shading.PatchMeshesShadingContext.java
/** * Create a patch list from a data stream, the returned list contains all the patches contained * in the data stream./* w w w . j a v a 2 s . c o m*/ * * @param shadingType the shading type * @param xform transformation for user to device space * @param matrix the pattern matrix concatenated with that of the parent content stream * @param controlPoints number of control points, 12 for type 6 shading and 16 for type 7 shading * @return the obtained patch list * @throws IOException when something went wrong */ final List<Patch> collectPatches(PDShadingType6 shadingType, AffineTransform xform, Matrix matrix, int controlPoints) throws IOException { COSDictionary dict = shadingType.getCOSObject(); int bitsPerFlag = shadingType.getBitsPerFlag(); PDRange rangeX = shadingType.getDecodeForParameter(0); PDRange rangeY = shadingType.getDecodeForParameter(1); PDRange[] colRange = new PDRange[numberOfColorComponents]; for (int i = 0; i < numberOfColorComponents; ++i) { colRange[i] = shadingType.getDecodeForParameter(2 + i); } List<Patch> list = new ArrayList<Patch>(); long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1; long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1; COSStream cosStream = (COSStream) dict; ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream()); try { Point2D[] implicitEdge = new Point2D[4]; float[][] implicitCornerColor = new float[2][numberOfColorComponents]; byte flag = 0; try { flag = (byte) (mciis.readBits(bitsPerFlag) & 3); } catch (EOFException ex) { LOG.error(ex); } while (true) { try { boolean isFree = (flag == 0); Patch current = readPatch(mciis, isFree, implicitEdge, implicitCornerColor, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform, controlPoints); if (current == null) { break; } list.add(current); flag = (byte) (mciis.readBits(bitsPerFlag) & 3); switch (flag) { case 0: break; case 1: implicitEdge = current.getFlag1Edge(); implicitCornerColor = current.getFlag1Color(); break; case 2: implicitEdge = current.getFlag2Edge(); implicitCornerColor = current.getFlag2Color(); break; case 3: implicitEdge = current.getFlag3Edge(); implicitCornerColor = current.getFlag3Color(); break; default: LOG.warn("bad flag: " + flag); break; } } catch (EOFException ex) { break; } } } finally { mciis.close(); } return list; }
From source file:org.apache.pdfbox.pdmodel.graphics.shading.PatchMeshesShadingContext.java
/** * Read a single patch from a data stream, a patch contains information of its coordinates and * color parameters./* w ww. j av a2 s . co m*/ * * @param input the image source data stream * @param isFree whether this is a free patch * @param implicitEdge implicit edge when a patch is not free, otherwise it's not used * @param implicitCornerColor implicit colors when a patch is not free, otherwise it's not used * @param maxSrcCoord the maximum coordinate value calculated from source data * @param maxSrcColor the maximum color value calculated from source data * @param rangeX range for coordinate x * @param rangeY range for coordinate y * @param colRange range for color * @param matrix the pattern matrix concatenated with that of the parent content stream * @param xform transformation for user to device space * @param controlPoints number of control points, 12 for type 6 shading and 16 for type 7 shading * @return a single patch * @throws IOException when something went wrong */ protected Patch readPatch(ImageInputStream input, boolean isFree, Point2D[] implicitEdge, float[][] implicitCornerColor, long maxSrcCoord, long maxSrcColor, PDRange rangeX, PDRange rangeY, PDRange[] colRange, Matrix matrix, AffineTransform xform, int controlPoints) throws IOException { float[][] color = new float[4][numberOfColorComponents]; Point2D[] points = new Point2D[controlPoints]; int pStart = 4, cStart = 2; if (isFree) { pStart = 0; cStart = 0; } else { points[0] = implicitEdge[0]; points[1] = implicitEdge[1]; points[2] = implicitEdge[2]; points[3] = implicitEdge[3]; for (int i = 0; i < numberOfColorComponents; i++) { color[0][i] = implicitCornerColor[0][i]; color[1][i] = implicitCornerColor[1][i]; } } try { for (int i = pStart; i < controlPoints; i++) { long x = input.readBits(bitsPerCoordinate); long y = input.readBits(bitsPerCoordinate); float px = interpolate(x, maxSrcCoord, rangeX.getMin(), rangeX.getMax()); float py = interpolate(y, maxSrcCoord, rangeY.getMin(), rangeY.getMax()); Point2D p = matrix.transformPoint(px, py); xform.transform(p, p); points[i] = p; } for (int i = cStart; i < 4; i++) { for (int j = 0; j < numberOfColorComponents; j++) { long c = input.readBits(bitsPerColorComponent); color[i][j] = interpolate(c, maxSrcColor, colRange[j].getMin(), colRange[j].getMax()); } } } catch (EOFException ex) { LOG.debug("EOF"); return null; } return generatePatch(points, color); }
From source file:org.apache.pdfbox.pdmodel.graphics.shading.Type4ShadingContext.java
private List<ShadedTriangle> collectTriangles(PDShadingType4 freeTriangleShadingType, AffineTransform xform, Matrix matrix) throws IOException { COSDictionary dict = freeTriangleShadingType.getCOSObject(); PDRange rangeX = freeTriangleShadingType.getDecodeForParameter(0); PDRange rangeY = freeTriangleShadingType.getDecodeForParameter(1); PDRange[] colRange = new PDRange[numberOfColorComponents]; for (int i = 0; i < numberOfColorComponents; ++i) { colRange[i] = freeTriangleShadingType.getDecodeForParameter(2 + i); }//from ww w.j a v a 2 s. co m List<ShadedTriangle> list = new ArrayList<ShadedTriangle>(); long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1; long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1; COSStream stream = (COSStream) dict; ImageInputStream mciis = new MemoryCacheImageInputStream(stream.createInputStream()); try { byte flag = (byte) 0; try { flag = (byte) (mciis.readBits(bitsPerFlag) & 3); } catch (EOFException ex) { LOG.error(ex); } while (true) { Vertex p0, p1, p2; Point2D[] ps; float[][] cs; int lastIndex; try { switch (flag) { case 0: p0 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform); flag = (byte) (mciis.readBits(bitsPerFlag) & 3); if (flag != 0) { LOG.error("bad triangle: " + flag); } p1 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform); mciis.readBits(bitsPerFlag); if (flag != 0) { LOG.error("bad triangle: " + flag); } p2 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform); ps = new Point2D[] { p0.point, p1.point, p2.point }; cs = new float[][] { p0.color, p1.color, p2.color }; list.add(new ShadedTriangle(ps, cs)); flag = (byte) (mciis.readBits(bitsPerFlag) & 3); break; case 1: case 2: lastIndex = list.size() - 1; if (lastIndex < 0) { LOG.error("broken data stream: " + list.size()); } else { ShadedTriangle preTri = list.get(lastIndex); p2 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform); ps = new Point2D[] { flag == 1 ? preTri.corner[1] : preTri.corner[0], preTri.corner[2], p2.point }; cs = new float[][] { flag == 1 ? preTri.color[1] : preTri.color[0], preTri.color[2], p2.color }; list.add(new ShadedTriangle(ps, cs)); flag = (byte) (mciis.readBits(bitsPerFlag) & 3); } break; default: LOG.warn("bad flag: " + flag); break; } } catch (EOFException ex) { break; } } } finally { mciis.close(); } return list; }