1use std::borrow::Cow;
6use std::vec::Vec;
7
8use dom_struct::dom_struct;
9use euclid::default::{Rect, Size2D};
10use ipc_channel::ipc::IpcSharedMemory;
11use js::gc::CustomAutoRooterGuard;
12use js::jsapi::JSObject;
13use js::rust::HandleObject;
14use js::typedarray::{ClampedU8, Uint8ClampedArray};
15use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
16
17use super::bindings::buffer_source::{HeapBufferSource, create_heap_buffer_source_with_length};
18use crate::dom::bindings::buffer_source::create_buffer_source;
19use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
20 ImageDataMethods, ImageDataPixelFormat, ImageDataSettings, PredefinedColorSpace,
21};
22use crate::dom::bindings::error::{Error, Fallible};
23use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
24use crate::dom::bindings::root::DomRoot;
25use crate::dom::globalscope::GlobalScope;
26use crate::script_runtime::{CanGc, JSContext};
27
28#[dom_struct]
29pub(crate) struct ImageData {
30 reflector_: Reflector,
31 width: u32,
32 height: u32,
33 #[ignore_malloc_size_of = "mozjs"]
35 data: HeapBufferSource<ClampedU8>,
36 pixel_format: ImageDataPixelFormat,
37 color_space: PredefinedColorSpace,
38}
39
40impl ImageData {
41 #[allow(unsafe_code)]
42 pub(crate) fn new(
43 global: &GlobalScope,
44 width: u32,
45 height: u32,
46 mut data: Option<Vec<u8>>,
47 can_gc: CanGc,
48 ) -> Fallible<DomRoot<ImageData>> {
49 let len =
50 pixels::compute_rgba8_byte_length_if_within_limit(width as usize, height as usize)
51 .ok_or(Error::Range(
52 "The requested image size exceeds the supported range".to_owned(),
53 ))?;
54
55 let settings = ImageDataSettings {
56 colorSpace: Some(PredefinedColorSpace::Srgb),
57 pixelFormat: ImageDataPixelFormat::Rgba_unorm8,
58 };
59
60 if let Some(ref mut d) = data {
61 d.resize(len as usize, 0);
62
63 let cx = GlobalScope::get_cx();
64 rooted!(in (*cx) let mut js_object = std::ptr::null_mut::<JSObject>());
65 auto_root!(in(*cx) let data = create_buffer_source::<ClampedU8>(cx, &d[..], js_object.handle_mut(), can_gc)
66 .map_err(|_| Error::JSFailed)?);
67
68 Self::Constructor_(global, None, can_gc, data, width, Some(height), &settings)
69 } else {
70 Self::Constructor(global, None, can_gc, width, height, &settings)
71 }
72 }
73
74 #[allow(clippy::too_many_arguments)]
75 fn initialize(
77 pixels_per_row: u32,
78 rows: u32,
79 settings: &ImageDataSettings,
80 source: Option<CustomAutoRooterGuard<Uint8ClampedArray>>,
81 default_color_space: Option<PredefinedColorSpace>,
82 global: &GlobalScope,
83 proto: Option<HandleObject>,
84 can_gc: CanGc,
85 ) -> Fallible<DomRoot<ImageData>> {
86 let data = if let Some(source) = source {
88 if !matches!(settings.pixelFormat, ImageDataPixelFormat::Rgba_unorm8) {
93 return Err(Error::InvalidState);
95 }
96 HeapBufferSource::<ClampedU8>::from_view(source)
98 } else {
99 match settings.pixelFormat {
101 ImageDataPixelFormat::Rgba_unorm8 => {
102 create_heap_buffer_source_with_length(
110 GlobalScope::get_cx(),
111 4 * rows * pixels_per_row,
112 can_gc,
113 )?
114 },
115 }
122 };
123 let width = pixels_per_row;
125 let height = rows;
127 let pixel_format = settings.pixelFormat;
129 let color_space = settings
132 .colorSpace
133 .or(default_color_space)
136 .unwrap_or(PredefinedColorSpace::Srgb);
138
139 Ok(reflect_dom_object_with_proto(
140 Box::new(ImageData {
141 reflector_: Reflector::new(),
142 width,
143 height,
144 data,
145 pixel_format,
146 color_space,
147 }),
148 global,
149 proto,
150 can_gc,
151 ))
152 }
153
154 pub(crate) fn is_detached(&self) -> bool {
155 self.data.is_detached_buffer(GlobalScope::get_cx())
156 }
157
158 pub(crate) fn get_size(&self) -> Size2D<u32> {
159 Size2D::new(self.Width(), self.Height())
160 }
161
162 #[allow(unsafe_code)]
164 pub(crate) unsafe fn as_slice(&self) -> &[u8] {
165 assert!(self.data.is_initialized());
166 let internal_data = self
167 .data
168 .get_typed_array()
169 .expect("Failed to get Data from ImageData.");
170 unsafe {
176 let ptr: *const [u8] = internal_data.as_slice() as *const _;
177 &*ptr
178 }
179 }
180
181 #[allow(unsafe_code)]
183 pub(crate) unsafe fn get_rect(&self, rect: Rect<u32>) -> Cow<'_, [u8]> {
184 pixels::rgba8_get_rect(unsafe { self.as_slice() }, self.get_size().to_u32(), rect)
185 }
186
187 #[allow(unsafe_code)]
188 pub(crate) fn get_snapshot_rect(&self, rect: Rect<u32>) -> Snapshot {
189 Snapshot::from_vec(
190 rect.size,
191 SnapshotPixelFormat::RGBA,
192 SnapshotAlphaMode::Transparent {
193 premultiplied: false,
194 },
195 unsafe { self.get_rect(rect).into_owned() },
196 )
197 }
198
199 #[allow(unsafe_code)]
200 pub(crate) fn to_shared_memory(&self) -> IpcSharedMemory {
201 IpcSharedMemory::from_bytes(unsafe { self.as_slice() })
203 }
204
205 #[allow(unsafe_code)]
206 pub(crate) fn to_vec(&self) -> Vec<u8> {
207 unsafe { self.as_slice() }.to_vec()
209 }
210}
211
212impl ImageDataMethods<crate::DomTypeHolder> for ImageData {
213 fn Constructor(
215 global: &GlobalScope,
216 proto: Option<HandleObject>,
217 can_gc: CanGc,
218 sw: u32,
219 sh: u32,
220 settings: &ImageDataSettings,
221 ) -> Fallible<DomRoot<Self>> {
222 if sw == 0 || sh == 0 {
224 return Err(Error::IndexSize);
225 }
226
227 pixels::compute_rgba8_byte_length_if_within_limit(sw as usize, sh as usize)
230 .ok_or(Error::IndexSize)?;
231
232 Self::initialize(sw, sh, settings, None, None, global, proto, can_gc)
235 }
236
237 fn Constructor_(
239 global: &GlobalScope,
240 proto: Option<HandleObject>,
241 can_gc: CanGc,
242 data: CustomAutoRooterGuard<Uint8ClampedArray>,
243 sw: u32,
244 sh: Option<u32>,
245 settings: &ImageDataSettings,
246 ) -> Fallible<DomRoot<Self>> {
247 let bytes_per_pixel = match settings.pixelFormat {
249 ImageDataPixelFormat::Rgba_unorm8 => 4,
250 };
251 let length = data.len();
253 if length == 0 {
254 return Err(Error::InvalidState);
255 }
256 if length % bytes_per_pixel != 0 {
259 return Err(Error::InvalidState);
260 }
261 let length = length / bytes_per_pixel;
263 if sw == 0 || length % sw as usize != 0 {
265 return Err(Error::IndexSize);
266 }
267 let height = length / sw as usize;
269 if sh.is_some_and(|x| height != x as usize) {
271 return Err(Error::IndexSize);
272 }
273 Self::initialize(
275 sw,
276 height as u32,
277 settings,
278 Some(data),
279 None,
280 global,
281 proto,
282 can_gc,
283 )
284 }
285
286 fn Width(&self) -> u32 {
288 self.width
289 }
290
291 fn Height(&self) -> u32 {
293 self.height
294 }
295
296 fn GetData(&self, _: JSContext) -> Fallible<Uint8ClampedArray> {
298 self.data.get_typed_array().map_err(|_| Error::JSFailed)
299 }
300
301 fn PixelFormat(&self) -> ImageDataPixelFormat {
303 self.pixel_format
304 }
305
306 fn ColorSpace(&self) -> PredefinedColorSpace {
308 self.color_space
309 }
310}