mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Implement ImageData constructors #15671
This commit is contained in:
parent
7db36abf67
commit
46a25d621c
7 changed files with 156 additions and 19 deletions
|
@ -5,6 +5,7 @@
|
|||
use core::nonzero::NonZero;
|
||||
use dom::bindings::codegen::Bindings::ImageDataBinding;
|
||||
use dom::bindings::codegen::Bindings::ImageDataBinding::ImageDataMethods;
|
||||
use dom::bindings::error::{Fallible, Error};
|
||||
use dom::bindings::js::Root;
|
||||
use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
||||
use dom::globalscope::GlobalScope;
|
||||
|
@ -28,14 +29,7 @@ pub struct ImageData {
|
|||
impl ImageData {
|
||||
#[allow(unsafe_code)]
|
||||
pub fn new(global: &GlobalScope, width: u32, height: u32, mut data: Option<Vec<u8>>) -> Root<ImageData> {
|
||||
let imagedata = box ImageData {
|
||||
reflector_: Reflector::new(),
|
||||
width: width,
|
||||
height: height,
|
||||
data: Heap::default(),
|
||||
};
|
||||
let len = width * height * 4;
|
||||
|
||||
unsafe {
|
||||
let cx = global.get_cx();
|
||||
rooted!(in (cx) let mut js_object = ptr::null_mut());
|
||||
|
@ -47,11 +41,88 @@ impl ImageData {
|
|||
None => CreateWith::Length(len),
|
||||
};
|
||||
Uint8ClampedArray::create(cx, data, js_object.handle_mut()).unwrap();
|
||||
(*imagedata).data.set(js_object.get());
|
||||
Self::new_with_jsobject(global, width, Some(height), Some(js_object.get())).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
reflect_dom_object(imagedata,
|
||||
global, ImageDataBinding::Wrap)
|
||||
#[allow(unsafe_code)]
|
||||
unsafe fn new_with_jsobject(global: &GlobalScope,
|
||||
width: u32,
|
||||
mut opt_height: Option<u32>,
|
||||
opt_jsobject: Option<*mut JSObject>)
|
||||
-> Fallible<Root<ImageData>> {
|
||||
assert!(opt_jsobject.is_some() || opt_height.is_some());
|
||||
|
||||
if width == 0 {
|
||||
return Err(Error::IndexSize);
|
||||
}
|
||||
|
||||
// checking jsobject type and verifying (height * width * 4 == jsobject.byte_len())
|
||||
if let Some(jsobject) = opt_jsobject {
|
||||
let cx = global.get_cx();
|
||||
typedarray!(in(cx) let array_res: Uint8ClampedArray = jsobject);
|
||||
let mut array = try!(array_res
|
||||
.map_err(|_| Error::Type("Argument to Image data is not an Uint8ClampedArray".to_owned())));
|
||||
|
||||
let byte_len = array.as_slice().len() as u32;
|
||||
if byte_len % 4 != 0 {
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
|
||||
let len = byte_len / 4;
|
||||
if width == 0 || len % width != 0 {
|
||||
return Err(Error::IndexSize);
|
||||
}
|
||||
|
||||
let height = len / width;
|
||||
if opt_height.map_or(false, |x| height != x) {
|
||||
return Err(Error::IndexSize);
|
||||
} else {
|
||||
opt_height = Some(height);
|
||||
}
|
||||
}
|
||||
|
||||
let height = opt_height.unwrap();
|
||||
if height == 0 {
|
||||
return Err(Error::IndexSize);
|
||||
}
|
||||
|
||||
let imagedata = box ImageData {
|
||||
reflector_: Reflector::new(),
|
||||
width: width,
|
||||
height: height,
|
||||
data: Heap::default(),
|
||||
};
|
||||
|
||||
if let Some(jsobject) = opt_jsobject {
|
||||
(*imagedata).data.set(jsobject);
|
||||
} else {
|
||||
let len = width * height * 4;
|
||||
let cx = global.get_cx();
|
||||
rooted!(in (cx) let mut array = ptr::null_mut());
|
||||
Uint8ClampedArray::create(cx, CreateWith::Length(len), array.handle_mut()).unwrap();
|
||||
(*imagedata).data.set(array.get());
|
||||
}
|
||||
|
||||
Ok(reflect_dom_object(imagedata, global, ImageDataBinding::Wrap))
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-3
|
||||
#[allow(unsafe_code)]
|
||||
pub fn Constructor(global: &GlobalScope, width: u32, height: u32) -> Fallible<Root<Self>> {
|
||||
unsafe { Self::new_with_jsobject(global, width, Some(height), None) }
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-4
|
||||
#[allow(unsafe_code)]
|
||||
#[allow(unused_variables)]
|
||||
pub unsafe fn Constructor_(cx: *mut JSContext,
|
||||
global: &GlobalScope,
|
||||
jsobject: *mut JSObject,
|
||||
width: u32,
|
||||
opt_height: Option<u32>)
|
||||
-> Fallible<Root<Self>> {
|
||||
Self::new_with_jsobject(global, width, opt_height, Some(jsobject))
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
* You are granted a license to use, reproduce and create derivative works of this document.
|
||||
*/
|
||||
|
||||
//[Constructor(unsigned long sw, unsigned long sh),
|
||||
//Constructor(Uint8ClampedArray data, unsigned long sw, optional unsigned long sh),
|
||||
[Exposed=(Window,Worker)]
|
||||
[Constructor(unsigned long sw, unsigned long sh),
|
||||
Constructor(/* Uint8ClampedArray */ object data, unsigned long sw, optional unsigned long sh),
|
||||
Exposed=(Window,Worker)]
|
||||
interface ImageData {
|
||||
//[Constant]
|
||||
readonly attribute unsigned long width;
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
[2d.imageData.object.ctor.html]
|
||||
type: testharness
|
||||
[ImageData does not have a usable constructor]
|
||||
expected: FAIL
|
|
@ -92591,6 +92591,12 @@
|
|||
{}
|
||||
]
|
||||
],
|
||||
"html/semantics/embedded-content/the-canvas-element/imagedata.html": [
|
||||
[
|
||||
"/html/semantics/embedded-content/the-canvas-element/imagedata.html",
|
||||
{}
|
||||
]
|
||||
],
|
||||
"html/semantics/embedded-content/the-canvas-element/initial.colour.html": [
|
||||
[
|
||||
"/html/semantics/embedded-content/the-canvas-element/initial.colour.html",
|
||||
|
@ -175580,6 +175586,10 @@
|
|||
"38b6f3c5707b360427854b82054d86f5364dc0b8",
|
||||
"testharness"
|
||||
],
|
||||
"html/semantics/embedded-content/the-canvas-element/imagedata.html": [
|
||||
"e685ab0d76bdd1b60a5d6f1e2126ea1a89db97d1",
|
||||
"testharness"
|
||||
],
|
||||
"html/semantics/embedded-content/the-canvas-element/initial.colour.html": [
|
||||
"22ddb77f5a18a19edc409d36532120f852bfbc57",
|
||||
"testharness"
|
||||
|
|
|
@ -4116,9 +4116,6 @@
|
|||
[TextMetrics interface: attribute ideographicBaseline]
|
||||
expected: FAIL
|
||||
|
||||
[ImageData interface object length]
|
||||
expected: FAIL
|
||||
|
||||
[DrawingStyle interface: existence and properties of interface object]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
[interfaces.worker.html]
|
||||
type: testharness
|
||||
[ImageData interface object length]
|
||||
expected: FAIL
|
||||
|
||||
[Path2D interface: existence and properties of interface object]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
<!doctype html>
|
||||
<meta charset="utf-8">
|
||||
<title>ImageData Tests</title>
|
||||
<script src="/resources/testharness.js"></script>
|
||||
<script src="/resources/testharnessreport.js"></script>
|
||||
<script>
|
||||
test(function() {
|
||||
assert_throws("IndexSizeError", function() {
|
||||
new ImageData(0, 1);
|
||||
});
|
||||
}, "ImageData(w, h), width cannot be 0");
|
||||
|
||||
test(function() {
|
||||
assert_throws("IndexSizeError", function() {
|
||||
new ImageData(1, 0);
|
||||
});
|
||||
}, "ImageData(w, h), height cannot be 0");
|
||||
|
||||
test(function() {
|
||||
var imageData = new ImageData(2, 3);
|
||||
assert_equals(imageData.width, 2);
|
||||
assert_equals(imageData.height, 3);
|
||||
assert_equals(imageData.data.length, 24);
|
||||
assert_true(imageData.data instanceof Uint8ClampedArray);
|
||||
}, "ImageData(w, h), exposed attributes check");
|
||||
|
||||
test(function() {
|
||||
assert_throws("InvalidStateError", function() {
|
||||
new ImageData(new Uint8ClampedArray(3), 1);
|
||||
});
|
||||
}, "ImageData(buffer, w), the buffer size must be a multiple of 4");
|
||||
|
||||
test(function() {
|
||||
assert_throws("IndexSizeError", function() {
|
||||
new ImageData(new Uint8ClampedArray(16), 3);
|
||||
});
|
||||
}, "ImageData(buffer, w), buffer size must be a multiple of the image width");
|
||||
|
||||
test(function() {
|
||||
assert_throws("IndexSizeError", function() {
|
||||
new ImageData(new Uint8ClampedArray(16), 4, 3);
|
||||
});
|
||||
}, "ImageData(buffer, w, h), buffer.lenght == 4 * w * h must be true");
|
||||
|
||||
test(function() {
|
||||
assert_throws(new TypeError(), function() {
|
||||
new ImageData(new Int8Array(1), 1);
|
||||
});
|
||||
}, "ImageData(buffer, w, opt h), Uint8ClampedArray argument type check");
|
||||
|
||||
test(function() {
|
||||
var imageData = new ImageData(new Uint8ClampedArray(24), 2);
|
||||
assert_equals(imageData.width, 2);
|
||||
assert_equals(imageData.height, 3);
|
||||
assert_equals(imageData.data.length, 24);
|
||||
assert_true(imageData.data instanceof Uint8ClampedArray);
|
||||
}, "ImageData(buffer, w, opt h), exposed attributes check");
|
||||
</script>
|
Loading…
Add table
Add a link
Reference in a new issue