Search in sources :

Example 1 with NativeUint8ClampedArray

use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray in project htmlunit by HtmlUnit.

the class ImageData method getData.

/**
 * Returns a {@link NativeUint8ClampedArray} representing a one-dimensional array containing
 * the data in the RGBA order, with integer values between 0 and 255 (included).
 * @return the {@code data} property
 */
@JsxGetter
public NativeUint8ClampedArray getData() {
    if (data_ == null) {
        final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(bytes_.length);
        System.arraycopy(bytes_, 0, arrayBuffer.getBuffer(), 0, bytes_.length);
        data_ = new NativeUint8ClampedArray(arrayBuffer, 0, bytes_.length);
        data_.setParentScope(getParentScope());
        data_.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), data_.getClassName()));
    }
    return data_;
}
Also used : NativeUint8ClampedArray(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray) NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 2 with NativeUint8ClampedArray

use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray in project htmlunit by HtmlUnit.

the class ImageData method jsConstructor.

/**
 * JavaScript constructor.
 * @param cx the current context
 * @param args the arguments to the WebSocket constructor
 * @param ctorObj the function object
 * @param inNewExpr Is new or not
 * @return the java object to allow JavaScript to access
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public static Scriptable jsConstructor(final Context cx, final Object[] args, final Function ctorObj, final boolean inNewExpr) {
    if (args.length < 2) {
        throw Context.reportRuntimeError("ImageData ctor - too less arguments");
    }
    NativeUint8ClampedArray data = null;
    final int width;
    final int height;
    if (args[0] instanceof NativeUint8ClampedArray) {
        data = (NativeUint8ClampedArray) args[0];
        if (data.getArrayLength() % 4 != 0) {
            throw Context.reportRuntimeError("ImageData ctor - data length mod 4 not zero");
        }
        width = (int) ScriptRuntime.toInteger(args[1]);
        if (args.length < 3) {
            height = data.getArrayLength() / 4 / width;
            if (data.getArrayLength() != 4 * width * height) {
                throw Context.reportRuntimeError("ImageData ctor - width not correct");
            }
        } else {
            height = (int) ScriptRuntime.toInteger(args[2]);
        }
        if (data.getArrayLength() != 4 * width * height) {
            throw Context.reportRuntimeError("ImageData ctor - width/height not correct");
        }
    } else {
        width = (int) ScriptRuntime.toInteger(args[0]);
        height = (int) ScriptRuntime.toInteger(args[1]);
    }
    if (width < 0) {
        throw Context.reportRuntimeError("ImageData ctor - width negative");
    }
    if (height < 0) {
        throw Context.reportRuntimeError("ImageData ctor - height negative");
    }
    final ImageData result = new ImageData(null, 0, 0, width, height);
    if (data != null) {
        final byte[] bytes = data.getBuffer().getBuffer();
        System.arraycopy(bytes, 0, result.bytes_, 0, Math.min(bytes.length, result.bytes_.length));
    }
    return result;
}
Also used : NativeUint8ClampedArray(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Aggregations

NativeUint8ClampedArray (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray)2 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)1 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)1 NativeArrayBuffer (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer)1