Java Examples for com.google.gwt.canvas.dom.client.ImageData

The following java examples will help you to understand the usage of com.google.gwt.canvas.dom.client.ImageData. These source code samples are taken from different open source projects.

Example 1
Project: akjava_gwtlib-master  File: CanvasUtils.java View source code
public static void copyFromCanvasRedToImageDataAlpha(Canvas canvas, ImageData paintedData) {
    ImageData canvasData = ImageDataUtils.copyFrom(canvas);
    checkArgument(canvasData.getWidth() == paintedData.getWidth() && canvasData.getHeight() == paintedData.getHeight(), "copyToImageDataRedAsAlpha:must same size");
    for (int y = 0; y < paintedData.getHeight(); y++) {
        for (int x = 0; x < paintedData.getWidth(); x++) {
            paintedData.setAlphaAt(canvasData.getRedAt(x, y), x, y);
        }
    }
}
Example 2
Project: geogebra-master  File: RendererImplShadersW.java View source code
/**
	 * create alpha texture from image for the label
	 * 
	 * @param label
	 *            label
	 * @param image
	 *            image
	 * @param bimg
	 *            buffered image
	 */
public void createAlphaTexture(DrawLabel3D label, ImageElement image, GBufferedImageW bimg) {
    if (label.isPickable()) {
        // values for picking (ignore transparent bytes)
        ImageData data = bimg.getImageData();
        int xmin = label.getWidth(), xmax = 0, ymin = label.getHeight(), ymax = 0;
        for (int y = 0; y < label.getHeight(); y++) {
            for (int x = 0; x < label.getWidth(); x++) {
                int alpha = data.getAlphaAt(x, y);
                if (alpha != 0) {
                    if (x < xmin) {
                        xmin = x;
                    }
                    if (x > xmax) {
                        xmax = x;
                    }
                    if (y < ymin) {
                        ymin = y;
                    }
                    if (y > ymax) {
                        ymax = y;
                    }
                }
            }
        }
        label.setPickingDimension(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
    }
    // create texture
    WebGLTexture texture;
    int textureIndex = label.getTextureIndex();
    if (textureIndex == -1) {
        textureIndex = texturesArray.size();
        texture = glContext.createTexture();
        texturesArray.add(texture);
    } else {
        texture = texturesArray.get(textureIndex);
    }
    glContext.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
    glContext.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
    glContext.generateMipmap(WebGLRenderingContext.TEXTURE_2D);
    glContext.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
    label.setTextureIndex(textureIndex);
}
Example 3
Project: playn-master  File: HtmlImage.java View source code
@Override
public void getRgb(int startX, int startY, int width, int height, int[] rgbArray, int offset, int scanSize) {
    assert isReady() : "Cannot getRgb() a non-ready image";
    if (canvas == null) {
        canvas = img.getOwnerDocument().createCanvasElement();
        canvas.setHeight(img.getHeight());
        canvas.setWidth(img.getWidth());
        canvas.getContext2d().drawImage(img, 0, 0);
    // img.getOwnerDocument().getBody().appendChild(canvas);
    }
    Context2d ctx = canvas.getContext2d();
    ImageData imageData = ctx.getImageData(startX, startY, width, height);
    CanvasPixelArray pixelData = imageData.getData();
    int i = 0;
    int dst = offset;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int r = pixelData.get(i++);
            int g = pixelData.get(i++);
            int b = pixelData.get(i++);
            int a = pixelData.get(i++);
            rgbArray[dst + x] = a << 24 | r << 16 | g << 8 | b;
        }
        dst += scanSize;
    }
}
Example 4
Project: mgwt-master  File: ImageConverter.java View source code
public ImageResource convert(ImageResource resource, String color) {
    if (color == null) {
        throw new IllegalArgumentException();
    }
    if (!color.startsWith("#")) {
        throw new IllegalArgumentException();
    }
    int hexColor = Integer.parseInt(color.substring(1), 16);
    int red = hexColor >> 16 & 0xFF;
    int green = hexColor >> 8 & 0xFF;
    int blue = hexColor & 0xFF;
    int height = resource.getHeight();
    int width = resource.getWidth();
    ImageElement imageElement = loadImage(resource.getSafeUri().asString(), width, height);
    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);
    return new ConvertedImageResource(canvas.toDataUrl("image/png"), resource.getWidth(), resource.getHeight());
}
Example 5
Project: SilenceEngine-master  File: GwtImageReader.java View source code
private static void jsLoadedCallback(ImageData pixels, int width, int height, int oWidth, int oHeight, UniCallback<Image> onComplete) {
    Image image = new Image(width, height, oWidth, oHeight);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) image.setPixel(x, y, new Color(pixels.getRedAt(x, y) / 255f, pixels.getGreenAt(x, y) / 255f, pixels.getBlueAt(x, y) / 255f, pixels.getAlphaAt(x, y) / 255f));
    }
    onComplete.invoke(image);
}
Example 6
Project: jetpad-projectional-master  File: TextMetricsCalculator.java View source code
private static int measureHeight(Font font, String text) {
    Canvas canvas = canvas();
    Context2d ctx = canvas.getContext2d();
    ctx.setFont(getFontString(font));
    ctx.setFillStyle("rgb(255, 0, 0)");
    int width = (int) ctx.measureText(text).getWidth();
    int canvasHeight = font.getSize() * 2;
    canvas.setHeight(canvasHeight + "px");
    canvas.setHeight(font.getSize() * 2 + "px");
    canvas.setWidth(width + "px");
    ctx.fillText(text, 0, font.getSize());
    ImageData data = ctx.getImageData(0, 0, width, canvasHeight);
    int firstY = canvasHeight - 1;
    int lastY = 0;
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < canvasHeight; y++) {
            int red = data.getRedAt(x, y);
            if (red != 0) {
                if (firstY > y) {
                    firstY = y;
                }
                if (lastY < y) {
                    lastY = y;
                }
            }
        }
    }
    return lastY - firstY;
}
Example 7
Project: gwt-three.js-test-master  File: WebGLRenderTarget.java View source code
//TODO convert native?
/*
 * not work WebGLRenderTargetCube
 */
public final Canvas gwtTextureToCanvas(WebGLRenderer renderer) {
    int w = getWidth();
    int h = getHeight();
    //rgba
    Uint8ArrayNative buffer = Uint8ArrayNative.create(w * h * 4);
    renderer.readRenderTargetPixels(this, 0, 0, w, h, buffer);
    Canvas canvas = CanvasUtils.createCanvas(w, h);
    ImageData imageData = CanvasUtils.createSameSizeImageData(canvas);
    ImageDataUtils.set(imageData, buffer);
    ImageDataUtils.putImageData(imageData, canvas);
    return canvas;
}
Example 8
Project: TGAReader-master  File: TexturedCube.java View source code
Canvas createImageCanvas(int[] pixels, int width, int height) {
    Canvas canvas = Canvas.createIfSupported();
    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++) {
        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);
    }
    context.putImageData(data, 0, 0);
    return canvas;
}
Example 9
Project: LGame-master  File: GWTAssets.java View source code
@Override
protected ImageImpl.Data load(String path) throws Exception {
    path = getPath(path);
    if (path.startsWith(LSystem.FRAMEWORK_IMG_NAME)) {
        path = GWT_DEF_RES + path;
    }
    Exception error = null;
    for (Scale.ScaledResource rsrc : assetScale().getScaledResources(path)) {
        try {
            ImageElement image = localImageElement(path);
            Scale viewScale = game.graphics().scale(), imageScale = rsrc.scale;
            float viewImageRatio = viewScale.factor / imageScale.factor;
            if (viewImageRatio < 1f) {
                ImageData data = GWTImage.scaleImage(image, viewImageRatio);
                ImageElement img = Document.get().createImageElement();
                img.setWidth(data.getWidth());
                img.setHeight(data.getHeight());
                image = img;
                imageScale = viewScale;
            }
            return new ImageImpl.Data(imageScale, image, image.getWidth(), image.getHeight());
        } catch (Exception fnfe) {
            error = fnfe;
        }
    }
    game.log().warn("Could not load image: " + path + " [error=" + error + "]");
    throw error != null ? error : new Exception(path);
}
Example 10
Project: HTML5-Player-deprecated-master  File: Scene.java View source code
/**
	 *
	 * @param imageElement
	 * @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(//red channel
                pixelsData.get(index) + //red channel
                brightnessAdj);
                pixelsData.set(index, r);
                int g = checkColorRange(//green channel
                pixelsData.get(++index) + //green channel
                brightnessAdj);
                pixelsData.set(index, g);
                int b = checkColorRange(//blue channel
                pixelsData.get(++index) + //blue channel
                brightnessAdj);
                pixelsData.set(index, b);
                //alpha channel
                index++;
            }
            index++;
        }
    }
    context.putImageData(imageData, 0, 0);
    return temp;
}
Example 11
Project: LGA-master  File: GWTAssets.java View source code
@Override
protected ImageImpl.Data load(String path) throws Exception {
    path = getPath(path);
    if (path.startsWith(LSystem.FRAMEWORK_IMG_NAME)) {
        path = GWT_DEF_RES + path;
    }
    Exception error = null;
    for (Scale.ScaledResource rsrc : assetScale().getScaledResources(path)) {
        try {
            ImageElement image = localImageElement(path);
            Scale viewScale = game.graphics().scale(), imageScale = rsrc.scale;
            float viewImageRatio = viewScale.factor / imageScale.factor;
            if (viewImageRatio < 1f) {
                ImageData data = GWTImage.scaleImage(image, viewImageRatio);
                ImageElement img = Document.get().createImageElement();
                img.setWidth(data.getWidth());
                img.setHeight(data.getHeight());
                image = img;
                imageScale = viewScale;
            }
            return new ImageImpl.Data(imageScale, image, image.getWidth(), image.getHeight());
        } catch (Exception fnfe) {
            error = fnfe;
        }
    }
    game.log().warn("Could not load image: " + path + " [error=" + error + "]");
    throw error != null ? error : new Exception(path);
}
Example 12
Project: skWiki-master  File: ImageData.java View source code
/**
	 * Returns the original image data.
	 */
public com.google.gwt.canvas.dom.client.ImageData getGWTImageData() {
    return imageData;
}