Example usage for com.google.gwt.canvas.dom.client ImageData getData

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

Introduction

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

Prototype

public final native CanvasPixelArray getData() ;

Source Link

Document

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

Usage

From source file:anagram.client.Lens.java

License:Apache License

public void draw(Context2d back, Context2d front) {
    front.drawImage(back.getCanvas(), 0, 0);

    if (!GWT.isScript()) {
        // in devmode this effect is slow so we disable it here
    } else {/*from www . j a  va2 s  .c  om*/
        ImageData frontData = front.getImageData((int) (pos.x - radius), (int) (pos.y - radius), 2 * radius,
                2 * radius);
        CanvasPixelArray frontPixels = frontData.getData();
        ImageData backData = back.getImageData((int) (pos.x - radius), (int) (pos.y - radius), 2 * radius,
                2 * radius);
        CanvasPixelArray backPixels = backData.getData();
        int srcIdx, dstIdx;
        for (int i = lensArray.length - 1; i >= 0; i--) {
            dstIdx = 4 * lensArray[i][0];
            srcIdx = 4 * lensArray[i][1];
            frontPixels.set(dstIdx + 0, backPixels.get(srcIdx + 0));
            frontPixels.set(dstIdx + 1, backPixels.get(srcIdx + 1));
            frontPixels.set(dstIdx + 2, backPixels.get(srcIdx + 2));
        }
        front.putImageData(frontData, (int) (pos.x - radius), (int) (pos.y - radius));
    }

    front.setStrokeStyle(strokeStyle);
    front.beginPath();
    front.arc(pos.x, pos.y, radius, 0, Math.PI * 2, true);
    front.closePath();
    front.stroke();
}

From source file:com.ait.lienzo.client.core.types.ImageDataPixelColor.java

License:Open Source License

public ImageDataPixelColor(final ImageData source) {
    final CanvasPixelArray data = source.getData();

    m_r = color(data, 0);// ww w  .  j a v  a  2  s.  c  om

    m_g = color(data, 1);

    m_b = color(data, 2);

    m_a = color(data, 3);
}

From source file:com.googlecode.mgwt.image.client.ImageConverter.java

License:Apache License

public void convert(final ImageResource resource, String color,
        final ImageConverterCallback imageConverterCallback) {

    if (color == null) {
        throw new IllegalArgumentException();
    }/*from  www. j ava 2s .  co m*/

    if (!color.startsWith("#")) {
        throw new IllegalArgumentException();
    }

    color = maybeExpandColor(color);

    final int hexColor = Integer.parseInt(color.substring(1), 16);

    final int red = hexColor >> 16 & 0xFF;
    final int green = hexColor >> 8 & 0xFF;
    final int blue = hexColor & 0xFF;

    final int height = resource.getHeight();
    final int width = resource.getWidth();

    loadImage(resource.getSafeUri().asString(), width, height, new LoadImageCallback() {

        @Override
        public void onSuccess(ImageElement imageElement) {

            Canvas canvas = Canvas.createIfSupported();
            canvas.getElement().setPropertyInt("height", height);
            canvas.getElement().setPropertyInt("width", width);

            Context2d context = canvas.getContext2d();
            context.drawImage(imageElement, 0, 0);
            ImageData imageData = context.getImageData(0, 0, width, height);

            CanvasPixelArray canvasPixelArray = imageData.getData();

            for (int i = 0; i < canvasPixelArray.getLength(); i += 4) {
                canvasPixelArray.set(i, red);
                canvasPixelArray.set(i + 1, green);
                canvasPixelArray.set(i + 2, blue);
                canvasPixelArray.set(i + 3, canvasPixelArray.get(i + 3));
            }
            context.putImageData(imageData, 0, 0);
            imageConverterCallback.onSuccess(new ConvertedImageResource(canvas.toDataUrl("image/png"),
                    resource.getWidth(), resource.getHeight()));
        }
    });
}

From source file:com.lizardtech.djvu.GMap.java

License:Open Source License

protected void setImageData(ImageData imageData) {
    this.imageData = imageData;
    Uint8Array imageArray = (Uint8Array) imageData.getData();
    // image array is clamped by default, we need non-clamped
    data = TypedArrays.createUint8Array(imageArray.buffer());
}

From source file:com.yocalist.client.ui.GwtCanvasView.java

License:Apache License

private ImageDataUpdate setTileColor(int tileRow, int tileColumn, int colorIndex) {
    ImageData image = context.createImageData(TILE_WIDTH, TILE_HEIGHT);
    CanvasPixelArray imageData = image.getData();
    int dataIndex = 0;
    int colorValueIndex = (TILE_HEIGHT * tileRow * FRAME_WIDTH) + (TILE_WIDTH * tileColumn);
    for (int i = 0; i < TILE_HEIGHT; i++) {
        for (int j = 0; j < TILE_WIDTH; j++) {
            imageData.set(dataIndex * 4, colors[colorIndex].getRed());
            imageData.set(dataIndex * 4 + 1, colors[colorIndex].getGreen());
            imageData.set(dataIndex * 4 + 2, colors[colorIndex].getBlue());
            imageData.set(dataIndex * 4 + 3, 255);
            colorIndexes[colorValueIndex] = colorIndex;
            dataIndex++;/*from   w  w  w .  j  a  v  a 2 s.c om*/
            colorValueIndex++;
        }
        colorValueIndex += FRAME_WIDTH - TILE_WIDTH;
    }
    return new ImageDataUpdate(TILE_WIDTH * tileColumn, TILE_HEIGHT * tileRow, image);
}

From source file:com.yocalist.client.ui.GwtCanvasView.java

License:Apache License

private void refreshColorTable() {
    ImageData image = context.createImageData(FRAME_WIDTH, FRAME_HEIGHT);
    CanvasPixelArray imageData = image.getData();
    for (int i = 0; i < FRAME_WIDTH * FRAME_HEIGHT; i++) {
        imageData.set(i * 4, colors[colorIndexes[i]].getRed());
        imageData.set(i * 4 + 1, colors[colorIndexes[i]].getGreen());
        imageData.set(i * 4 + 2, colors[colorIndexes[i]].getBlue());
        imageData.set(i * 4 + 3, 255);//from  ww  w  .  jav  a  2s .c  o  m
    }
    updates.add(Collections.singletonList(new ImageDataUpdate(0, 0, image)));
}

From source file:com.yocalist.client.ui.GwtCanvasView.java

License:Apache License

private void setTile(TileBlock tile) {
    ImageData image = context.createImageData(TILE_WIDTH, TILE_HEIGHT);
    CanvasPixelArray imageData = image.getData();
    int x = tile.getColumn() * TILE_WIDTH;
    int y = tile.getRow() * TILE_HEIGHT;
    int dataIndex = 0;
    int colorValueIndex = y * FRAME_WIDTH + x;
    int pix, colorIndex;
    for (int i = 0; i < tile.getPixels().length; i++) {
        for (int j = 0; j < TILE_WIDTH; j++) {
            pix = (tile.getPixels()[i] >>> (5 - j)) & 0x01;
            colorIndex = pix == 0 ? tile.getColorIndexOff() : tile.getColorIndexOn();
            if (!tile.isNormal()) {
                colorIndex ^= colorIndexes[colorIndex];
            }/*from w ww. j  a  va 2  s. com*/
            imageData.set(dataIndex * 4, colors[colorIndex].getRed());
            imageData.set(dataIndex * 4 + 1, colors[colorIndex].getGreen());
            imageData.set(dataIndex * 4 + 2, colors[colorIndex].getBlue());
            imageData.set(dataIndex * 4 + 3, 255);
            colorIndexes[colorValueIndex] = colorIndex;
            dataIndex++;
            colorValueIndex++;
        }
        colorValueIndex += FRAME_WIDTH - TILE_WIDTH;
    }
    updates.add(Collections.singletonList(new ImageDataUpdate(x, y, image)));
}

From source file:net.npe.gwt.canvas.CanvasCreator.java

License:MIT License

public static Canvas createImageCanvas(int[] pixels, int width, int height) {

    Canvas canvas = Canvas.createIfSupported();

    if (canvas == null)
        return null;

    canvas.setCoordinateSpaceWidth(width);
    canvas.setCoordinateSpaceHeight(height);

    Context2d context = canvas.getContext2d();
    ImageData data = context.createImageData(width, height);

    CanvasPixelArray array = data.getData();
    for (int i = 0; i < width * height; i++) { // ABGR
        array.set(4 * i + 0, pixels[i] & 0xFF);
        array.set(4 * i + 1, (pixels[i] >> 8) & 0xFF);
        array.set(4 * i + 2, (pixels[i] >> 16) & 0xFF);
        array.set(4 * i + 3, (pixels[i] >> 24) & 0xFF);
    }//from  w ww.j a  v  a  2 s.  co  m
    context.putImageData(data, 0, 0);

    return canvas;

}

From source file:net.npe.image.util.gwt.GwtImageReader.java

License:MIT License

public static Canvas createCanvas(int[] pixels, int width, int height) {

    Canvas canvas = Canvas.createIfSupported();

    if (canvas == null)
        return null;

    canvas.setCoordinateSpaceWidth(width);
    canvas.setCoordinateSpaceHeight(height);

    Context2d context = canvas.getContext2d();
    ImageData data = context.createImageData(width, height);

    CanvasPixelArray array = data.getData();
    for (int i = 0; i < width * height; i++) { // ABGR
        array.set(4 * i + 0, pixels[i] & 0xFF);
        array.set(4 * i + 1, (pixels[i] >> 8) & 0xFF);
        array.set(4 * i + 2, (pixels[i] >> 16) & 0xFF);
        array.set(4 * i + 3, (pixels[i] >> 24) & 0xFF);
    }/*from   w ww  .j a  v  a2s .c om*/
    context.putImageData(data, 0, 0);

    return canvas;

}

From source file:org.catrobat.html5player.client.Scene.java

License:Open Source License

/**
 *
 * @param imageElement/*from  w w w  .ja  va2  s  .  co m*/
 * @param brightness
 * @return Canvas canvas with the adjusted image
 */
private Canvas adjustImageBrightness(ImageElement imageElement, double brightness) {

    int width = imageElement.getWidth();
    int height = imageElement.getHeight();

    Canvas temp = Canvas.createIfSupported();
    temp.setCoordinateSpaceWidth(width);
    temp.setCoordinateSpaceHeight(height);

    Context2d context = temp.getContext2d();

    context.drawImage(imageElement, 0, 0);

    ImageData imageData = context.getImageData(0, 0, width, height);

    CanvasPixelArray pixelsData = imageData.getData();

    int index = 0;

    System.out.println(pixelsData.getLength());

    int brightnessAdj = (int) (255d * brightness) - 255;

    if (brightnessAdj != 0) {
        while (index < pixelsData.getLength()) {
            if ((index + 1) % 4 != 0) {
                int r = checkColorRange(pixelsData.get(index) + brightnessAdj); //red channel
                pixelsData.set(index, r);

                int g = checkColorRange(pixelsData.get(++index) + brightnessAdj); //green channel
                pixelsData.set(index, g);

                int b = checkColorRange(pixelsData.get(++index) + brightnessAdj); //blue channel
                pixelsData.set(index, b);

                index++; //alpha channel
            }
            index++;
        }
    }

    context.putImageData(imageData, 0, 0);

    return temp;
}