Example usage for com.google.gwt.html5.client CanvasRenderingContext2D putImageData

List of usage examples for com.google.gwt.html5.client CanvasRenderingContext2D putImageData

Introduction

In this page you can find the example usage for com.google.gwt.html5.client CanvasRenderingContext2D putImageData.

Prototype

public final native void putImageData(ImageData imagedata, float dx, float dy) ;

Source Link

Usage

From source file:com.googlecode.gwtquake.client.GwtWebGLRenderer.java

License:Open Source License

@Override
public void GL_ResampleTexture(int[] in, int inwidth, int inheight, int[] out, int outwidth, int outheight) {

    if (canvas1.getWidth() < inwidth) {
        canvas1.setWidth(inwidth);//from   w ww  .ja va2 s  .  c om
    }
    if (canvas1.getHeight() < inheight) {
        canvas1.setHeight(inheight);
    }

    CanvasRenderingContext2D inCtx = canvas1.getContext2D();
    ImageData data = inCtx.createImageData(inwidth, inheight);
    CanvasPixelArray pixels = data.getData();

    int len = inwidth * inheight;
    int p = 0;

    for (int i = 0; i < len; i++) {
        int abgr = in[i];
        pixels.set(p, (abgr & 255));
        pixels.set(p + 1, (abgr >> 8) & 255);
        pixels.set(p + 2, (abgr >> 16) & 255);
        pixels.set(p + 3, (abgr >> 24) & 255);
        p += 4;
    }
    inCtx.putImageData(data, 0, 0);

    if (canvas2.getWidth() < outwidth) {
        canvas2.setWidth(outwidth);
    }
    if (canvas2.getHeight() < outheight) {
        canvas2.setHeight(outheight);
    }

    CanvasRenderingContext2D outCtx = canvas2.getContext2D();
    outCtx.drawImage(canvas1, 0, 0, inwidth, inheight, 0, 0, outwidth, outheight);

    data = outCtx.getImageData(0, 0, outwidth, outheight);
    pixels = data.getData();

    len = outwidth * outheight;
    p = 0;

    for (int i = 0; i < len; i++) {
        int r = pixels.get(p) & 255;
        int g = pixels.get(p + 1) & 255;
        int b = pixels.get(p + 2) & 255;
        int a = pixels.get(p + 3) & 255;
        p += 4;
        out[i] = (a << 24) | (b << 16) | (g << 8) | r;
    }
}