Java Code Examples for javax.imageio.stream.ImageInputStream#readBits()

The following examples show how to use javax.imageio.stream.ImageInputStream#readBits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GouraudShadingContext.java    From sambox with Apache License 2.0 6 votes vote down vote up
/**
 * Read a vertex from the bit input stream performs interpolations.
 *
 * @param input bit input stream
 * @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);
}
 
Example 2
Source File: TIFFDecompressor.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Reformats bit-discontiguous data into the {@code DataBuffer}
 * of the supplied {@code WritableRaster}.
 */
private static void reformatDiscontiguousData(byte[] buf,
                                              int[] bitsPerSample,
                                              int stride,
                                              int w,
                                              int h,
                                              WritableRaster raster)
    throws IOException {

    // Get SampleModel info.
    SampleModel sm = raster.getSampleModel();
    int numBands = sm.getNumBands();

    // Initialize input stream.
    ByteArrayInputStream is = new ByteArrayInputStream(buf);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);

    // Reformat.
    long iisPosition = 0L;
    int y = raster.getMinY();
    for(int j = 0; j < h; j++, y++) {
        iis.seek(iisPosition);
        int x = raster.getMinX();
        for(int i = 0; i < w; i++, x++) {
            for(int b = 0; b < numBands; b++) {
                long bits = iis.readBits(bitsPerSample[b]);
                raster.setSample(x, y, b, (int)bits);
            }
        }
        iisPosition += stride;
    }
}
 
Example 3
Source File: TIFFDecompressor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reformats bit-discontiguous data into the {@code DataBuffer}
 * of the supplied {@code WritableRaster}.
 */
private static void reformatDiscontiguousData(byte[] buf,
                                              int[] bitsPerSample,
                                              int stride,
                                              int w,
                                              int h,
                                              WritableRaster raster)
    throws IOException {

    // Get SampleModel info.
    SampleModel sm = raster.getSampleModel();
    int numBands = sm.getNumBands();

    // Initialize input stream.
    ByteArrayInputStream is = new ByteArrayInputStream(buf);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);

    // Reformat.
    long iisPosition = 0L;
    int y = raster.getMinY();
    for(int j = 0; j < h; j++, y++) {
        iis.seek(iisPosition);
        int x = raster.getMinX();
        for(int i = 0; i < w; i++, x++) {
            for(int b = 0; b < numBands; b++) {
                long bits = iis.readBits(bitsPerSample[b]);
                raster.setSample(x, y, b, (int)bits);
            }
        }
        iisPosition += stride;
    }
}
 
Example 4
Source File: GouraudShadingContext.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Read a vertex from the bit input stream performs interpolations.
 *
 * @param input bit input stream
 * @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]);
    }

    // "Each set of vertex data shall occupy a whole number of bytes.
    // If the total number of bits required is not divisible by 8, the last data byte
    // for each vertex is padded at the end with extra bits, which shall be ignored."
    int bitOffset = input.getBitOffset();
    if (bitOffset != 0)
    {
        input.readBits(8 - bitOffset);
    }

    return new Vertex(p, colorComponentTab);
}
 
Example 5
Source File: PDFunctionType0.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get all sample values of this function.
 *
 * @return an array with all samples.
 */
private int[][] getSamples()
{
    if (samples == null)
    {
        int arraySize = 1;
        int nIn = getNumberOfInputParameters();
        int nOut = getNumberOfOutputParameters();
        COSArray sizes = getSize();
        for (int i = 0; i < nIn; i++)
        {
            arraySize *= sizes.getInt(i);
        }
        samples = new int[arraySize][nOut];
        int bitsPerSample = getBitsPerSample();
        int index = 0;
        try
        {
            // PDF spec 1.7 p.171:
            // Each sample value is represented as a sequence of BitsPerSample bits. 
            // Successive values are adjacent in the bit stream; there is no padding at byte boundaries.
            ImageInputStream mciis = new MemoryCacheImageInputStream(getPDStream().createInputStream());
            for (int i = 0; i < arraySize; i++)
            {
                for (int k = 0; k < nOut; k++)
                {
                    // TODO will this cast work properly for 32 bitsPerSample or should we use long[]?
                    samples[index][k] = (int) mciis.readBits(bitsPerSample);
                }
                index++;
            }
            mciis.close();
        }
        catch (IOException exception)
        {
            LOG.error("IOException while reading the sample values of this function.", exception);
        }
    }
    return samples;
}
 
Example 6
Source File: PDFunctionType0.java    From sambox with Apache License 2.0 5 votes vote down vote up
/**
 * Get all sample values of this function.
 *
 * @return an array with all samples.
 */
private int[][] getSamples()
{
    if (samples == null)
    {
        int arraySize = 1;
        int nIn = getNumberOfInputParameters();
        int nOut = getNumberOfOutputParameters();
        COSArray sizes = getSize();
        for (int i = 0; i < nIn; i++)
        {
            arraySize *= sizes.getInt(i);
        }
        samples = new int[arraySize][nOut];
        int bitsPerSample = getBitsPerSample();
        int index = 0;
        try
        {
            // PDF spec 1.7 p.171:
            // Each sample value is represented as a sequence of BitsPerSample bits. 
            // Successive values are adjacent in the bit stream; there is no padding at byte boundaries.
            ImageInputStream mciis = new MemoryCacheImageInputStream(getPDStream().createInputStream());
            for (int i = 0; i < arraySize; i++)
            {
                for (int k = 0; k < nOut; k++)
                {
                    // TODO will this cast work properly for 32 bitsPerSample or should we use long[]?
                    samples[index][k] = (int) mciis.readBits(bitsPerSample);
                }
                index++;
            }
            mciis.close();
        }
        catch (IOException exception)
        {
            LOG.error("IOException while reading the sample values of this function.", exception);
        }
    }
    return samples;
}
 
Example 7
Source File: ReadBitsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    byte[] buffer = new byte[] {(byte)169, (byte)85}; // 10101001 01010101
    InputStream ins = new ByteArrayInputStream(buffer);
    ImageInputStream in = new FileCacheImageInputStream(ins,null);

    if (in.getBitOffset() != 0) {
        throw new RuntimeException("Initial bit offset != 0!");
    }

    int bit0 = in.readBit(); // 1
    if (bit0 != 1) {
        throw new RuntimeException("First bit != 1");
    }
    if (in.getBitOffset() != 1) {
        throw new RuntimeException("Second bit offset != 1");
    }

    long bits1 = in.readBits(5); // 01010 = 10
    if (bits1 != 10) {
        throw new RuntimeException("Bits 1-5 != 10 (= " + bits1 + ")");
    }
    if (in.getBitOffset() != 6) {
        throw new RuntimeException("Third bit offset != 6");
    }

    int bit1 = in.readBit(); // 0
    if (bit1 != 0) {
        throw new RuntimeException("Bit 6 != 0");
    }
    if (in.getBitOffset() != 7) {
        throw new RuntimeException("Third bit offset != 7");
    }

    long bits2 = in.readBits(8); // 10101010 = 170
    if (bits2 != 170) {
        throw new RuntimeException("Bits 7-14 != 170 (= " + bits2 + ")");
    }
    if (in.getBitOffset() != 7) {
        throw new RuntimeException("Fourth bit offset != 7");
    }

    int bit2 = in.readBit(); // 1
    if (bit2 != 1) {
        throw new RuntimeException("Bit 15 != 1");
    }
    if (in.getBitOffset() != 0) {
        throw new RuntimeException("Fifth bit offset != 0");
    }

    in.close();
}
 
Example 8
Source File: PatchMeshesShadingContext.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Read a single patch from a data stream, a patch contains information of its coordinates and
 * color parameters.
 *
 * @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);
}
 
Example 9
Source File: ReadBitsTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    byte[] buffer = new byte[] {(byte)169, (byte)85}; // 10101001 01010101
    InputStream ins = new ByteArrayInputStream(buffer);
    ImageInputStream in = new FileCacheImageInputStream(ins,null);

    if (in.getBitOffset() != 0) {
        throw new RuntimeException("Initial bit offset != 0!");
    }

    int bit0 = in.readBit(); // 1
    if (bit0 != 1) {
        throw new RuntimeException("First bit != 1");
    }
    if (in.getBitOffset() != 1) {
        throw new RuntimeException("Second bit offset != 1");
    }

    long bits1 = in.readBits(5); // 01010 = 10
    if (bits1 != 10) {
        throw new RuntimeException("Bits 1-5 != 10 (= " + bits1 + ")");
    }
    if (in.getBitOffset() != 6) {
        throw new RuntimeException("Third bit offset != 6");
    }

    int bit1 = in.readBit(); // 0
    if (bit1 != 0) {
        throw new RuntimeException("Bit 6 != 0");
    }
    if (in.getBitOffset() != 7) {
        throw new RuntimeException("Third bit offset != 7");
    }

    long bits2 = in.readBits(8); // 10101010 = 170
    if (bits2 != 170) {
        throw new RuntimeException("Bits 7-14 != 170 (= " + bits2 + ")");
    }
    if (in.getBitOffset() != 7) {
        throw new RuntimeException("Fourth bit offset != 7");
    }

    int bit2 = in.readBit(); // 1
    if (bit2 != 1) {
        throw new RuntimeException("Bit 15 != 1");
    }
    if (in.getBitOffset() != 0) {
        throw new RuntimeException("Fifth bit offset != 0");
    }

    in.close();
}
 
Example 10
Source File: PatchMeshesShadingContext.java    From sambox with Apache License 2.0 4 votes vote down vote up
/**
 * Read a single patch from a data stream, a patch contains information of its coordinates and
 * color parameters.
 *
 * @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);
}