Example usage for java.awt.image BufferedImage getRaster

List of usage examples for java.awt.image BufferedImage getRaster

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRaster.

Prototype

public WritableRaster getRaster() 

Source Link

Document

Returns the WritableRaster .

Usage

From source file:org.pentaho.di.core.gui.SwingGC.java

private void drawImage(SwingUniversalImage image, int locationX, int locationY, int imageSize) {
    if (isDrawingPixelatedImages() && image.isBitmap()) {
        BufferedImage img = image.getAsBitmapForSize(imageSize, imageSize);
        ColorModel cm = img.getColorModel();
        Raster raster = img.getRaster();

        for (int x = 0; x < img.getWidth(observer); x++) {
            for (int y = 0; y < img.getHeight(observer); y++) {
                Object pix = raster.getDataElements(x, y, null);
                gc.setColor(new Color(cm.getRed(pix), cm.getGreen(pix), cm.getBlue(pix), cm.getAlpha(pix)));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x + 1,
                        locationY + yOffset + y + 1);
            }// w  w  w  .jav a  2  s  .c om
        }
    } else {
        image.drawToGraphics(gc, locationX, locationY, imageSize, imageSize);
    }
}

From source file:org.pentaho.di.core.gui.SwingGC.java

private void drawImage(SwingUniversalImage image, int centerX, int centerY, double angle, int imageSize) {
    if (isDrawingPixelatedImages() && image.isBitmap()) {
        BufferedImage img = image.getAsBitmapForSize(imageSize, imageSize, angle);
        ColorModel cm = img.getColorModel();
        Raster raster = img.getRaster();

        int offx = centerX + xOffset - img.getWidth() / 2;
        int offy = centerY + yOffset - img.getHeight() / 2;
        for (int x = 0; x < img.getWidth(observer); x++) {
            for (int y = 0; y < img.getHeight(observer); y++) {
                Object pix = raster.getDataElements(x, y, null);
                gc.setColor(new Color(cm.getRed(pix), cm.getGreen(pix), cm.getBlue(pix), cm.getAlpha(pix)));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(offx + x, offy + y, offx + x + 1, offy + y + 1);
            }//  w w w . j a  v a  2  s .  co  m
        }
    } else {
        image.drawToGraphics(gc, centerX, centerY, imageSize, imageSize, angle);
    }
}

From source file:org.apache.jetspeed.security.mfa.impl.CaptchaImageResource.java

protected void noiseEffects(Graphics2D gfx, BufferedImage image) {
    // XOR circle
    int dx = randomInt(width, 2 * width);
    int dy = randomInt(width, 2 * height);
    int x = randomInt(0, width / 2);
    int y = randomInt(0, height / 2);

    gfx.setXORMode(Color.GRAY);/*from  www  .  j a  v  a2  s  .c om*/
    if (config.isFontSizeRandom())
        gfx.setStroke(new BasicStroke(randomInt(config.getFontSize() / 8, config.getFontSize() / 2)));
    else
        gfx.setStroke(new BasicStroke(config.getFontSize()));

    gfx.drawOval(x, y, dx, dy);

    WritableRaster rstr = image.getRaster();
    int[] vColor = new int[3];
    int[] oldColor = new int[3];
    Random vRandom = new Random(System.currentTimeMillis());
    // noise
    for (x = 0; x < width; x++) {
        for (y = 0; y < height; y++) {
            rstr.getPixel(x, y, oldColor);

            // hard noise
            vColor[0] = 0 + (int) (Math.floor(vRandom.nextFloat() * 1.03) * 255);
            // soft noise
            vColor[0] = vColor[0] ^ (170 + (int) (vRandom.nextFloat() * 80));
            // xor to image
            vColor[0] = vColor[0] ^ oldColor[0];
            vColor[1] = vColor[0];
            vColor[2] = vColor[0];

            rstr.setPixel(x, y, vColor);
        }
    }
}

From source file:Engine.WorldMap.java

public WorldMap() {
    Map = new double[mapSize][];

    for (int a = 0; a < mapSize; a++) {
        Map[a] = new double[mapSize];
    }/*from w  ww.j  a  v  a  2  s  .co  m*/
    //z jakiego powodu tu byo -1, ale ju nie wiem dlaczego
    /*
    ObjectsMap = new ArrayList[mapSize-1][];
    for (int a = 0; a < mapSize-1; a++)
    {
    ObjectsMap[a] = new ArrayList[mapSize-1];
    }
    for (int x = 0; x < mapSize-1; x++) {
       for(int y = 0; y < mapSize-1; y++) {
                ObjectsMap[x][y] = new ArrayList<IRenderableGameObject>();
            }
    }*/
    ObjectsMap = new ArrayList[mapSize][];
    for (int a = 0; a < mapSize; a++) {
        ObjectsMap[a] = new ArrayList[mapSize];
    }
    for (int x = 0; x < mapSize; x++) {
        for (int y = 0; y < mapSize; y++) {
            ObjectsMap[x][y] = new ArrayList<IRenderableGameObject>();
        }
    }

    for (int x = 0; x < mapSize; x++) {
        for (int y = 0; y < mapSize; y++) {
            //ObjectsMap[x][y] = new ArrayList<IRenderableGameObject>();
            Map[x][y] = NoiseHelper(x, y);
            double min = Math.min(Math.abs(mapSize - x - 1), Math.abs(x));
            min = Math.min(min, Math.abs(mapSize - y - 1));
            min = Math.min(min, Math.abs(y));
            if (min < cooling) {
                Map[x][y] *= min / cooling;
            }
        }
    }
    /*
    Map[1][1] = 10;
    Map[3][1] = 20;
    Map[5][1] = 30;
    Map[7][1] = 40;
    Map[9][1] = 50;
    Map[11][1] = 60;*/

    if (startForceCalculator) {
        BufferedImage bufferedImage;
        try {
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            InputStream is = classloader.getResourceAsStream("./Images/LUT-1.bmp");

            ///img = ImageIO.read(new File("/Images/face.jpg"));
            bufferedImage = ImageIO.read(is);
            is.close();

            //bufferedImage = ImageIO.read(new File("e:\\Projects\\java 3d tutorial\\Game\\LUT-1.bmp"));
            LUT = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
        } catch (IOException ex) {
            Logger.getLogger(MapDrawer.class.getName()).log(Level.SEVERE, null, ex);
        }

        ForceCalculator fc = new ForceCalculator(width, height);

        for (int a = 0; a < sourcesCount; a++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int[] fv = new int[4];
            int which = random.nextInt(4);
            fv[which] = random.nextInt(maxRandom);
            //fv[1] = random.nextInt(maxRandom);
            //fv[2] = random.nextInt(maxRandom);
            //fv[3] = random.nextInt(maxRandom);
            int[] maxR = new int[4];
            for (int b = 0; b < maxR.length; b++)
                maxR[b] = maxRadius;
            ForceDescription fd = new ForceDescription(x, y, fv, width, height, maxR);
            forces.add(fd);
        }

        map = fc.map;
        forceMap = fc.forceMap;
        heightMap = fc.heighMap;

        HeightMapToMap();

        ForceCalculatorWorker fcw = new ForceCalculatorWorker(fc, this);
        fcw.start();
        MoveForcesHelperWorker mfhw = new MoveForcesHelperWorker(forces, width, height);
        mfhw.start();
    }
}

From source file:GraphicsUtil.java

/**
 * Copies data from one bufferedImage to another paying attention
 * to the state of AlphaPreMultiplied.//from   ww w . j a  v  a 2 s .  c  o m
 *
 * @param src The source
 * @param srcRect The Rectangle of source data to be copied
 * @param dst The destination
 * @param destP The Place for the upper left corner of srcRect in dst.
 */
public static void copyData(BufferedImage src, Rectangle srcRect, BufferedImage dst, Point destP) {

    /*
     if (srcCS != dstCS)
    throw new IllegalArgumentException
        ("Images must be in the same ColorSpace in order "+
         "to copy Data between them");
     */
    boolean srcAlpha = src.getColorModel().hasAlpha();
    boolean dstAlpha = dst.getColorModel().hasAlpha();

    // System.out.println("Src has: " + srcAlpha +
    //                    " is: " + src.isAlphaPremultiplied());
    //
    // System.out.println("Dst has: " + dstAlpha +
    //                    " is: " + dst.isAlphaPremultiplied());

    if (srcAlpha == dstAlpha)
        if (!srcAlpha || src.isAlphaPremultiplied() == dst.isAlphaPremultiplied()) {
            // They match one another so just copy everything...
            copyData(src.getRaster(), dst.getRaster());
            return;
        }

    // System.out.println("Using Slow CopyData");

    int[] pixel = null;
    Raster srcR = src.getRaster();
    WritableRaster dstR = dst.getRaster();
    int bands = dstR.getNumBands();

    int dx = destP.x - srcRect.x;
    int dy = destP.y - srcRect.y;

    int w = srcRect.width;
    int x0 = srcRect.x;
    int y0 = srcRect.y;
    int y1 = y0 + srcRect.height - 1;

    if (!srcAlpha) {
        // Src has no alpha dest does so set alpha to 1.0 everywhere.
        // System.out.println("Add Alpha");
        int[] oPix = new int[bands * w];
        int out = (w * bands) - 1; // The 2 skips alpha channel
        while (out >= 0) {
            // Fill alpha channel with 255's
            oPix[out] = 255;
            out -= bands;
        }

        int b, in;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = w * (bands - 1) - 1;
            out = (w * bands) - 2; // The 2 skips alpha channel on last pix
            switch (bands) {
            case 4:
                while (in >= 0) {
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    out--;
                }
                break;
            default:
                while (in >= 0) {
                    for (b = 0; b < bands - 1; b++)
                        oPix[out--] = pixel[in--];
                    out--;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else if (dstAlpha && dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to multiply it for dst.
        // System.out.println("Mult Case");
        int a, b, alpha, in, fpNorm = (1 << 24) / 255, pt5 = 1 << 23;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = bands * w - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= 4;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= bands;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (dstAlpha && !dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to divide it out for dst.
        // System.out.println("Div Case");
        int a, b, ialpha, in, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands * w) - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= 4;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= bands;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (src.isAlphaPremultiplied()) {
        int[] oPix = new int[bands * w];
        // Src has alpha dest does not so unpremult and store...
        // System.out.println("Remove Alpha, Div Case");
        int a, b, ialpha, in, out, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands + 1) * w - 1;
            out = (bands * w) - 1;
            while (in >= 0) {
                a = pixel[in];
                in--;
                if (a > 0) {
                    if (a < 255) {
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands; b++)
                            oPix[out--] = (pixel[in--] * ialpha + pt5) >>> 16;
                    } else
                        for (b = 0; b < bands; b++)
                            oPix[out--] = pixel[in--];
                } else {
                    in -= bands;
                    for (b = 0; b < bands; b++)
                        oPix[out--] = 255;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else {
        // Src has unpremult alpha, dest does not have alpha,
        // just copy the color channels over.
        Rectangle dstRect = new Rectangle(destP.x, destP.y, srcRect.width, srcRect.height);
        for (int b = 0; b < bands; b++)
            copyBand(srcR, srcRect, b, dstR, dstRect, b);
    }
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testTransparentPaletteOpaqueOutput() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&bgcolor=0xFF0000";
    BufferedImage bi = getAsImage(url, "image/png");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.OPAQUE, cm.getTransparency());

    // grab a pixel in the low left corner, should be red (BG color)
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[3];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(255, color[0]);//from ww w. j  a va2  s.c  o  m
    assertEquals(0, color[1]);
    assertEquals(0, color[2]);

    // a pixel high enough to be solid, should be fully green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testTransparentPaletteTransparentOutput() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&transparent=true";
    BufferedImage bi = getAsImage(url, "image/png");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.TRANSLUCENT, cm.getTransparency());

    // grab a pixel in the low left corner, should be transparent
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[4];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[3]);//from   w  ww.  java  2  s . c  o m

    // a pixel high enough to be solid, should be solid green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
    assertEquals(255, color[3]);
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testTransparentPaletteTransparentOutputPng8() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng8&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&transparent=true";
    BufferedImage bi = getAsImage(url, "image/png; mode=8bit");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.TRANSLUCENT, cm.getTransparency());

    // grab a pixel in the low left corner, should be transparent
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[4];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[3]);//from   w  w  w .j a  v  a  2  s . com

    // a pixel high enough to be solid, should be solid green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
    assertEquals(255, color[3]);
}

From source file:org.apache.fop.render.pcl.PCLGenerator.java

private RenderedImage getMask(RenderedImage img, Dimension targetDim) {
    ColorModel cm = img.getColorModel();
    if (cm.hasAlpha()) {
        BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Raster raster = img.getData();
        GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0);

        BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null);
        BufferedImage alphat = op1.filter(alpha, null);

        BufferedImage mask;/*from w ww. ja  va2  s.  c om*/
        if (true) {
            mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY);
        } else {
            byte[] arr = { (byte) 0, (byte) 0xff };
            ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr);
            WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width,
                    targetDim.height, 1, 1, null);
            mask = new BufferedImage(colorModel, wraster, false, null);
        }

        Graphics2D g2d = mask.createGraphics();
        try {
            AffineTransform at = new AffineTransform();
            double sx = targetDim.getWidth() / img.getWidth();
            double sy = targetDim.getHeight() / img.getHeight();
            at.scale(sx, sy);
            g2d.drawRenderedImage(alphat, at);
        } finally {
            g2d.dispose();
        }
        /*
        try {
        BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png"));
        BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png"));
        } catch (IOException e) {
        e.printStackTrace();
        }*/
        return mask;
    } else {
        return null;
    }
}

From source file:org.eclipse.swt.snippets.Snippet156.java

static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
                data.setPixel(x, y, pixel);
                if (colorModel.hasAlpha()) {
                    data.setAlpha(x, y, (rgb >> 24) & 0xFF);
                }/*w ww  .  j av a  2 s .c o m*/
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}