Example usage for com.google.gwt.corp.gfx.client.canvas ImageData getData

List of usage examples for com.google.gwt.corp.gfx.client.canvas ImageData getData

Introduction

In this page you can find the example usage for com.google.gwt.corp.gfx.client.canvas ImageData getData.

Prototype

public native final CanvasPixelArray getData() ;

Source Link

Document

Returns a canvas pixel array of the size width * height * 4.

Usage

From source file:jake2.gwt.client.GwtWebGLRenderer.java

License:Open Source License

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

    if (canvas1.getWidth() < inwidth) {
        canvas1.setWidth(inwidth);/*from ww w .  ja v  a  2 s.c  o m*/
    }
    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;
    }
}

From source file:jake2.gwt.client.GwtWebGLRenderer.java

License:Open Source License

public void setPicDataLowLevel(image_t image, ImageElement img) {
    CanvasElement canvas = (CanvasElement) Document.get().createElement("canvas");
    int w = img.getWidth();
    int h = img.getHeight();
    canvas.setWidth(w);//from  w ww  .  j  ava2 s.c o m
    canvas.setHeight(h);
    //      canvas.getStyle().setProperty("border", "solid 1px green");
    canvas.getStyle().setDisplay(Display.NONE);
    Document.get().getBody().appendChild(canvas);
    CanvasRenderingContext2D ctx = canvas.getContext2D();
    ctx.drawImage(img, 0, 0);
    ImageData data = ctx.getImageData(0, 0, w, h);
    CanvasPixelArray pixels = data.getData();

    int count = w * h * 4;
    byte[] pic = new byte[count];
    for (int i = 0; i < count; i += 4) {
        pic[i + 3] = (byte) pixels.get(i + 3); // alpha, then bgr
        pic[i + 2] = (byte) pixels.get(i + 2);
        pic[i + 1] = (byte) pixels.get(i + 1);
        pic[i] = (byte) pixels.get(i);
    }

    GL_SetPicData(image, pic, w, h, 32);
}

From source file:jake2.gwt.client.GwtWebGLRenderer.java

License:Open Source License

protected void debugLightmap(IntBuffer lightmapBuffer, int w, int h, float scale) {
    CanvasElement canvas = (CanvasElement) Document.get().createElement("canvas");
    canvas.setWidth(w);/*  w ww .j av  a 2s. com*/
    canvas.setHeight(h);
    Document.get().getBody().appendChild(canvas);
    ImageData id = canvas.getContext2D().createImageData(w, h);
    CanvasPixelArray pd = id.getData();
    for (int i = 0; i < w * h; i++) {
        int abgr = lightmapBuffer.get(i);
        pd.set(i * 4, abgr & 255);
        pd.set(i * 4 + 1, abgr & 255);
        pd.set(i * 4 + 2, abgr & 255);
        pd.set(i * 4 + 3, abgr & 255);
    }
    canvas.getContext2D().putImageData(id, 0, 0);
}