Search in sources :

Example 56 with WritableRaster

use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.

the class ImageTypeProducer method readInternal.

private Raster readInternal(int imageIndex, ImageReadParam param, boolean wantRaster) throws IOException {
    readHeader(imageIndex, false);
    WritableRaster imRas = null;
    int numImageBands = 0;
    if (!wantRaster) {
        // Can we read this image?
        Iterator imageTypes = getImageTypes(imageIndex);
        if (imageTypes.hasNext() == false) {
            throw new IIOException("Unsupported Image Type");
        }
        image = getDestination(param, imageTypes, width, height);
        imRas = image.getRaster();
        // The destination may still be incompatible.
        numImageBands = image.getSampleModel().getNumBands();
        // Check whether we can handle any implied color conversion
        // Throws IIOException if the stream and the image are
        // incompatible, and sets convert if a java conversion
        // is necessary
        checkColorConversion(image, param);
        // Check the source and destination bands in the param
        checkReadParamBandSettings(param, numComponents, numImageBands);
    } else {
        // Set the output color space equal to the input colorspace
        // This disables all conversions
        setOutColorSpace(structPointer, colorSpaceCode);
        image = null;
    }
    // Create an intermediate 1-line Raster that will hold the decoded,
    // subsampled, clipped, band-selected image data in a single
    // byte-interleaved buffer.  The above transformations
    // will occur in C for performance.  Every time this Raster
    // is filled we will call back to acceptPixels below to copy
    // this to whatever kind of buffer our image has.
    int[] srcBands = JPEG.bandOffsets[numComponents - 1];
    int numRasterBands = (wantRaster ? numComponents : numImageBands);
    destinationBands = null;
    Rectangle srcROI = new Rectangle(0, 0, 0, 0);
    destROI = new Rectangle(0, 0, 0, 0);
    computeRegions(param, width, height, image, srcROI, destROI);
    int periodX = 1;
    int periodY = 1;
    minProgressivePass = 0;
    maxProgressivePass = Integer.MAX_VALUE;
    if (param != null) {
        periodX = param.getSourceXSubsampling();
        periodY = param.getSourceYSubsampling();
        int[] sBands = param.getSourceBands();
        if (sBands != null) {
            srcBands = sBands;
            numRasterBands = srcBands.length;
        }
        if (!wantRaster) {
            // ignore dest bands for Raster
            destinationBands = param.getDestinationBands();
        }
        minProgressivePass = param.getSourceMinProgressivePass();
        maxProgressivePass = param.getSourceMaxProgressivePass();
        if (param instanceof JPEGImageReadParam) {
            JPEGImageReadParam jparam = (JPEGImageReadParam) param;
            if (jparam.areTablesSet()) {
                abbrevQTables = jparam.getQTables();
                abbrevDCHuffmanTables = jparam.getDCHuffmanTables();
                abbrevACHuffmanTables = jparam.getACHuffmanTables();
            }
        }
    }
    int lineSize = destROI.width * numRasterBands;
    buffer = new DataBufferByte(lineSize);
    int[] bandOffs = JPEG.bandOffsets[numRasterBands - 1];
    raster = Raster.createInterleavedRaster(buffer, destROI.width, 1, lineSize, numRasterBands, bandOffs, null);
    // target Raster that will permit a simple setRect for each scanline
    if (wantRaster) {
        target = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, destROI.width, destROI.height, lineSize, numRasterBands, bandOffs, null);
    } else {
        target = imRas;
    }
    int[] bandSizes = target.getSampleModel().getSampleSize();
    for (int i = 0; i < bandSizes.length; i++) {
        if (bandSizes[i] <= 0 || bandSizes[i] > 8) {
            throw new IIOException("Illegal band size: should be 0 < size <= 8");
        }
    }
    /*
         * If the process is sequential, and we have restart markers,
         * we could skip to the correct restart marker, if the library
         * lets us.  That's an optimization to investigate later.
         */
    // Check for update listeners (don't call back if none)
    boolean callbackUpdates = ((updateListeners != null) || (progressListeners != null));
    // Set up progression data
    initProgressData();
    // and set knownPassCount
    if (imageIndex == imageMetadataIndex) {
        // We have metadata
        knownPassCount = 0;
        for (Iterator iter = imageMetadata.markerSequence.iterator(); iter.hasNext(); ) {
            if (iter.next() instanceof SOSMarkerSegment) {
                knownPassCount++;
            }
        }
    }
    progInterval = Math.max((target.getHeight() - 1) / 20, 1);
    if (knownPassCount > 0) {
        progInterval *= knownPassCount;
    } else if (maxProgressivePass != Integer.MAX_VALUE) {
        progInterval *= (maxProgressivePass - minProgressivePass + 1);
    }
    if (debug) {
        System.out.println("**** Read Data *****");
        System.out.println("numRasterBands is " + numRasterBands);
        System.out.print("srcBands:");
        for (int i = 0; i < srcBands.length; i++) System.out.print(" " + srcBands[i]);
        System.out.println();
        System.out.println("destination bands is " + destinationBands);
        if (destinationBands != null) {
            for (int i = 0; i < destinationBands.length; i++) {
                System.out.print(" " + destinationBands[i]);
            }
            System.out.println();
        }
        System.out.println("sourceROI is " + srcROI);
        System.out.println("destROI is " + destROI);
        System.out.println("periodX is " + periodX);
        System.out.println("periodY is " + periodY);
        System.out.println("minProgressivePass is " + minProgressivePass);
        System.out.println("maxProgressivePass is " + maxProgressivePass);
        System.out.println("callbackUpdates is " + callbackUpdates);
    }
    // Finally, we are ready to read
    processImageStarted(currentImage);
    boolean aborted = false;
    // Note that getData disables acceleration on buffer, but it is
    // just a 1-line intermediate data transfer buffer that will not
    // affect the acceleration of the resulting image.
    aborted = readImage(structPointer, buffer.getData(), numRasterBands, srcBands, bandSizes, srcROI.x, srcROI.y, srcROI.width, srcROI.height, periodX, periodY, abbrevQTables, abbrevDCHuffmanTables, abbrevACHuffmanTables, minProgressivePass, maxProgressivePass, callbackUpdates);
    if (aborted) {
        processReadAborted();
    } else {
        processImageComplete();
    }
    return target;
}
Also used : WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) Rectangle(java.awt.Rectangle) IIOException(javax.imageio.IIOException) DataBufferByte(java.awt.image.DataBufferByte) JPEGImageReadParam(javax.imageio.plugins.jpeg.JPEGImageReadParam) Point(java.awt.Point)

Example 57 with WritableRaster

use of java.awt.image.WritableRaster in project gdx-skineditor by cobolfoo.

the class GlyphPage method renderGlyph.

/** Loads a single glyph to the backing texture, if it fits. */
private void renderGlyph(Glyph glyph, int width, int height) {
    // Draw the glyph to the scratch image using Java2D.
    scratchGraphics.setComposite(AlphaComposite.Clear);
    scratchGraphics.fillRect(0, 0, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE);
    scratchGraphics.setComposite(AlphaComposite.SrcOver);
    if (unicodeFont.getNativeRendering()) {
        for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) {
            Effect effect = (Effect) iter.next();
            if (effect instanceof ColorEffect)
                scratchGraphics.setColor(((ColorEffect) effect).getColor());
        }
        scratchGraphics.setColor(java.awt.Color.white);
        scratchGraphics.setFont(unicodeFont.getFont());
        scratchGraphics.drawString("" + (char) glyph.getCodePoint(), 0, unicodeFont.getAscent());
    } else {
        scratchGraphics.setColor(java.awt.Color.white);
        for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) ((Effect) iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph);
        // The shape will never be needed again.
        glyph.setShape(null);
    }
    width = Math.min(width, texture.getWidth());
    height = Math.min(height, texture.getHeight());
    WritableRaster raster = scratchImage.getRaster();
    int[] row = new int[width];
    for (int y = 0; y < height; y++) {
        raster.getDataElements(0, y, width, 1, row);
        scratchIntBuffer.put(row);
    }
    GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, pageX, pageY, width, height, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, scratchByteBuffer);
    scratchIntBuffer.clear();
    float u = pageX / (float) texture.getWidth();
    float v = pageY / (float) texture.getHeight();
    float u2 = (pageX + width) / (float) texture.getWidth();
    float v2 = (pageY + height) / (float) texture.getHeight();
    glyph.setTexture(texture, u, v, u2, v2);
}
Also used : ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) Effect(com.badlogic.gdx.tools.hiero.unicodefont.effects.Effect) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect)

Example 58 with WritableRaster

use of java.awt.image.WritableRaster in project commons-gdx by gemserk.

the class ScreenshotSaver method saveScreenshot.

public static void saveScreenshot(File file, byte[] pixels, int width, int height, boolean hasAlpha) throws IOException {
    DataBufferByte dataBuffer = new DataBufferByte(pixels, pixels.length);
    PixelInterleavedSampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, 4, 4 * width, getOffsets(hasAlpha));
    WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
    BufferedImage img = new BufferedImage(getColorModel(hasAlpha), raster, false, null);
    ImageIO.write(img, "png", file);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) WritableRaster(java.awt.image.WritableRaster) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Example 59 with WritableRaster

use of java.awt.image.WritableRaster in project jmonkeyengine by jMonkeyEngine.

the class Screenshots method convertScreenShot2.

public static void convertScreenShot2(IntBuffer bgraBuf, BufferedImage out) {
    WritableRaster wr = out.getRaster();
    DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
    int[] cpuArray = db.getData();
    bgraBuf.clear();
    bgraBuf.get(cpuArray);
//        int width  = wr.getWidth();
//        int height = wr.getHeight();
//
//        // flip the components the way AWT likes them
//        for (int y = 0; y < height / 2; y++){
//            for (int x = 0; x < width; x++){
//                int inPtr  = (y * width + x);
//                int outPtr = ((height-y-1) * width + x);
//                int pixel = cpuArray[inPtr];
//                cpuArray[inPtr] = cpuArray[outPtr];
//                cpuArray[outPtr] = pixel;
//            }
//        }
}
Also used : WritableRaster(java.awt.image.WritableRaster) DataBufferInt(java.awt.image.DataBufferInt)

Example 60 with WritableRaster

use of java.awt.image.WritableRaster in project jmonkeyengine by jMonkeyEngine.

the class Screenshots method convertScreenShot.

/**
     * Flips the image along the Y axis and converts BGRA to ABGR
     * 
     * @param bgraBuf
     * @param out 
     */
public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out) {
    WritableRaster wr = out.getRaster();
    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
    byte[] cpuArray = db.getData();
    // copy native memory to java memory
    bgraBuf.clear();
    bgraBuf.get(cpuArray);
    bgraBuf.clear();
    int width = wr.getWidth();
    int height = wr.getHeight();
    // flip the components the way AWT likes them
    // calcuate half of height such that all rows of the array are written to
    // e.g. for odd heights, write 1 more scanline
    int heightdiv2ceil = height % 2 == 1 ? (height / 2) + 1 : height / 2;
    for (int y = 0; y < heightdiv2ceil; y++) {
        for (int x = 0; x < width; x++) {
            int inPtr = (y * width + x) * 4;
            int outPtr = ((height - y - 1) * width + x) * 4;
            byte b1 = cpuArray[inPtr + 0];
            byte g1 = cpuArray[inPtr + 1];
            byte r1 = cpuArray[inPtr + 2];
            byte a1 = cpuArray[inPtr + 3];
            byte b2 = cpuArray[outPtr + 0];
            byte g2 = cpuArray[outPtr + 1];
            byte r2 = cpuArray[outPtr + 2];
            byte a2 = cpuArray[outPtr + 3];
            cpuArray[outPtr + 0] = a1;
            cpuArray[outPtr + 1] = b1;
            cpuArray[outPtr + 2] = g1;
            cpuArray[outPtr + 3] = r1;
            cpuArray[inPtr + 0] = a2;
            cpuArray[inPtr + 1] = b2;
            cpuArray[inPtr + 2] = g2;
            cpuArray[inPtr + 3] = r2;
        }
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) DataBufferByte(java.awt.image.DataBufferByte)

Aggregations

WritableRaster (java.awt.image.WritableRaster)110 BufferedImage (java.awt.image.BufferedImage)74 ColorModel (java.awt.image.ColorModel)33 DirectColorModel (java.awt.image.DirectColorModel)20 IndexColorModel (java.awt.image.IndexColorModel)19 DataBufferByte (java.awt.image.DataBufferByte)17 Point (java.awt.Point)15 Raster (java.awt.image.Raster)15 Rectangle (java.awt.Rectangle)13 Graphics2D (java.awt.Graphics2D)12 ComponentColorModel (java.awt.image.ComponentColorModel)12 DataBufferInt (java.awt.image.DataBufferInt)10 SampleModel (java.awt.image.SampleModel)9 DataBuffer (java.awt.image.DataBuffer)8 IOException (java.io.IOException)8 ColorSpace (java.awt.color.ColorSpace)7 Color (java.awt.Color)6 Iterator (java.util.Iterator)6 AffineTransform (java.awt.geom.AffineTransform)5 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)5