mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Use the typed array APIs in ImageData.
This commit is contained in:
parent
13826970c4
commit
2839bca065
1 changed files with 11 additions and 20 deletions
|
@ -10,11 +10,10 @@ use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
||||||
use dom::globalscope::GlobalScope;
|
use dom::globalscope::GlobalScope;
|
||||||
use euclid::size::Size2D;
|
use euclid::size::Size2D;
|
||||||
use js::jsapi::{Heap, JSContext, JSObject};
|
use js::jsapi::{Heap, JSContext, JSObject};
|
||||||
use js::jsapi::{JS_GetUint8ClampedArrayData, JS_NewUint8ClampedArray};
|
use js::rust::Runtime;
|
||||||
use libc::uint8_t;
|
use js::typedarray::Uint8ClampedArray;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::slice;
|
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
|
@ -37,16 +36,10 @@ impl ImageData {
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let cx = global.get_cx();
|
let cx = global.get_cx();
|
||||||
let js_object: *mut JSObject = JS_NewUint8ClampedArray(cx, width * height * 4);
|
rooted!(in (cx) let mut js_object = ptr::null_mut());
|
||||||
assert!(!js_object.is_null());
|
let data = data.as_ref().map(|d| &d[..]);
|
||||||
|
Uint8ClampedArray::create(cx, width * height * 4, data, js_object.handle_mut()).unwrap();
|
||||||
if let Some(vec) = data {
|
(*imagedata).data.set(js_object.get());
|
||||||
let mut is_shared = false;
|
|
||||||
let js_object_data: *mut uint8_t = JS_GetUint8ClampedArrayData(js_object, &mut is_shared, ptr::null());
|
|
||||||
assert!(!is_shared);
|
|
||||||
ptr::copy_nonoverlapping(vec.as_ptr(), js_object_data, vec.len())
|
|
||||||
}
|
|
||||||
(*imagedata).data.set(js_object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reflect_dom_object(imagedata,
|
reflect_dom_object(imagedata,
|
||||||
|
@ -56,14 +49,12 @@ impl ImageData {
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn get_data_array(&self) -> Vec<u8> {
|
pub fn get_data_array(&self) -> Vec<u8> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut is_shared = false;
|
|
||||||
assert!(!self.data.get().is_null());
|
assert!(!self.data.get().is_null());
|
||||||
let data: *const uint8_t =
|
let cx = Runtime::get();
|
||||||
JS_GetUint8ClampedArrayData(self.data.get(), &mut is_shared, ptr::null()) as *const uint8_t;
|
assert!(!cx.is_null());
|
||||||
assert!(!data.is_null());
|
typedarray!(in(cx) let array: Uint8ClampedArray = self.data.get());
|
||||||
assert!(!is_shared);
|
let vec = array.unwrap().as_slice().to_vec();
|
||||||
let len = self.Width() * self.Height() * 4;
|
vec
|
||||||
slice::from_raw_parts(data, len as usize).to_vec()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue