Example usage for com.google.gwt.canvas.dom.client CanvasPixelArray getLength

List of usage examples for com.google.gwt.canvas.dom.client CanvasPixelArray getLength

Introduction

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

Prototype

public final native int getLength() ;

Source Link

Document

Returns the length of the array.

Usage

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();
    }//w  w  w. j a v  a  2  s.  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.willshex.palette.client.CanvasBitmap.java

License:Apache License

@Override
public void getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height) {
    CanvasPixelArray array = canvas.getContext2d().getImageData(0, 0, width, height).getData();
    // copy it to an array
    int dataLength = array.getLength();
    for (int i = 0; i < dataLength; i++) {
        if (i % 4 == 0) {
            pixels[i / 4] = Color.argb(array.get(i + 3), array.get(i), array.get(i + 1), array.get(i + 2));
        }/*  w w  w  . j a  v  a2  s  .co  m*/
    }
}

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

License:Open Source License

/**
 *
 * @param imageElement//from w  ww. j a va 2s  .  com
 * @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;
}