script/dom/canvas/imagedata.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::borrow::Cow;
6use std::vec::Vec;
7
8use base::id::{ImageDataId, ImageDataIndex};
9use constellation_traits::SerializableImageData;
10use dom_struct::dom_struct;
11use euclid::default::{Rect, Size2D};
12use ipc_channel::ipc::IpcSharedMemory;
13use js::gc::CustomAutoRooterGuard;
14use js::jsapi::JSObject;
15use js::rust::HandleObject;
16use js::typedarray::{ClampedU8, Uint8ClampedArray};
17use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
18use rustc_hash::FxHashMap;
19
20use crate::dom::bindings::buffer_source::{
21 HeapBufferSource, create_buffer_source, create_heap_buffer_source_with_length,
22};
23use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
24 ImageDataMethods, ImageDataPixelFormat, ImageDataSettings, PredefinedColorSpace,
25};
26use crate::dom::bindings::error::{Error, Fallible};
27use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
28use crate::dom::bindings::root::DomRoot;
29use crate::dom::bindings::serializable::Serializable;
30use crate::dom::bindings::structuredclone::StructuredData;
31use crate::dom::globalscope::GlobalScope;
32use crate::script_runtime::{CanGc, JSContext};
33
34#[dom_struct]
35pub(crate) struct ImageData {
36 reflector_: Reflector,
37 width: u32,
38 height: u32,
39 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
40 #[ignore_malloc_size_of = "mozjs"]
41 data: HeapBufferSource<ClampedU8>,
42 pixel_format: ImageDataPixelFormat,
43 color_space: PredefinedColorSpace,
44}
45
46impl ImageData {
47 pub(crate) fn new(
48 global: &GlobalScope,
49 width: u32,
50 height: u32,
51 mut data: Option<Vec<u8>>,
52 can_gc: CanGc,
53 ) -> Fallible<DomRoot<ImageData>> {
54 let len =
55 pixels::compute_rgba8_byte_length_if_within_limit(width as usize, height as usize)
56 .ok_or(Error::Range(
57 "The requested image size exceeds the supported range".to_owned(),
58 ))?;
59
60 let settings = ImageDataSettings {
61 colorSpace: Some(PredefinedColorSpace::Srgb),
62 pixelFormat: ImageDataPixelFormat::Rgba_unorm8,
63 };
64
65 if let Some(ref mut d) = data {
66 d.resize(len as usize, 0);
67
68 let cx = GlobalScope::get_cx();
69 rooted!(in (*cx) let mut js_object = std::ptr::null_mut::<JSObject>());
70 auto_root!(in(*cx) let data = create_buffer_source::<ClampedU8>(cx, &d[..], js_object.handle_mut(), can_gc)
71 .map_err(|_| Error::JSFailed)?);
72
73 Self::Constructor_(global, None, can_gc, data, width, Some(height), &settings)
74 } else {
75 Self::Constructor(global, None, can_gc, width, height, &settings)
76 }
77 }
78
79 #[allow(clippy::too_many_arguments)]
80 /// <https://html.spec.whatwg.org/multipage/#initialize-an-imagedata-object>
81 fn initialize(
82 pixels_per_row: u32,
83 rows: u32,
84 settings: &ImageDataSettings,
85 source: Option<CustomAutoRooterGuard<Uint8ClampedArray>>,
86 default_color_space: Option<PredefinedColorSpace>,
87 global: &GlobalScope,
88 proto: Option<HandleObject>,
89 can_gc: CanGc,
90 ) -> Fallible<DomRoot<ImageData>> {
91 // 1. If source was given:
92 let data = if let Some(source) = source {
93 // 1. If settings["pixelFormat"] equals "rgba-unorm8" and source is not a Uint8ClampedArray,
94 // then throw an "InvalidStateError" DOMException.
95 // 2. If settings["pixelFormat"] is "rgba-float16" and source is not a Float16Array,
96 // then throw an "InvalidStateError" DOMException.
97 if !matches!(settings.pixelFormat, ImageDataPixelFormat::Rgba_unorm8) {
98 // we currently support only rgba-unorm8
99 return Err(Error::InvalidState(None));
100 }
101 // 3. Initialize the data attribute of imageData to source.
102 HeapBufferSource::<ClampedU8>::from_view(source)
103 } else {
104 // 2. Otherwise (source was not given):
105 match settings.pixelFormat {
106 ImageDataPixelFormat::Rgba_unorm8 => {
107 // 1. If settings["pixelFormat"] is "rgba-unorm8",
108 // then initialize the data attribute of imageData to a new Uint8ClampedArray object.
109 // The Uint8ClampedArray object must use a new ArrayBuffer for its storage,
110 // and must have a zero byte offset and byte length equal to the length of its storage, in bytes.
111 // The storage ArrayBuffer must have a length of 4 × rows × pixelsPerRow bytes.
112 // 3. If the storage ArrayBuffer could not be allocated,
113 // then rethrow the RangeError thrown by JavaScript, and return.
114 create_heap_buffer_source_with_length(
115 GlobalScope::get_cx(),
116 4 * rows * pixels_per_row,
117 can_gc,
118 )?
119 },
120 // 3. Otherwise, if settings["pixelFormat"] is "rgba-float16",
121 // then initialize the data attribute of imageData to a new Float16Array object.
122 // The Float16Array object must use a new ArrayBuffer for its storage,
123 // and must have a zero byte offset and byte length equal to the length of its storage, in bytes.
124 // The storage ArrayBuffer must have a length of 8 × rows × pixelsPerRow bytes.
125 // not implemented yet
126 }
127 };
128 // 3. Initialize the width attribute of imageData to pixelsPerRow.
129 let width = pixels_per_row;
130 // 4. Initialize the height attribute of imageData to rows.
131 let height = rows;
132 // 5. Initialize the pixelFormat attribute of imageData to settings["pixelFormat"].
133 let pixel_format = settings.pixelFormat;
134 // 6. If settings["colorSpace"] exists,
135 // then initialize the colorSpace attribute of imageData to settings["colorSpace"].
136 let color_space = settings
137 .colorSpace
138 // 7. Otherwise, if defaultColorSpace was given,
139 // then initialize the colorSpace attribute of imageData to defaultColorSpace.
140 .or(default_color_space)
141 // 8. Otherwise, initialize the colorSpace attribute of imageData to "srgb".
142 .unwrap_or(PredefinedColorSpace::Srgb);
143
144 Ok(reflect_dom_object_with_proto(
145 Box::new(ImageData {
146 reflector_: Reflector::new(),
147 width,
148 height,
149 data,
150 pixel_format,
151 color_space,
152 }),
153 global,
154 proto,
155 can_gc,
156 ))
157 }
158
159 pub(crate) fn is_detached(&self) -> bool {
160 self.data.is_detached_buffer(GlobalScope::get_cx())
161 }
162
163 pub(crate) fn get_size(&self) -> Size2D<u32> {
164 Size2D::new(self.Width(), self.Height())
165 }
166
167 /// Nothing must change the array on the JS side while the slice is live.
168 #[expect(unsafe_code)]
169 pub(crate) unsafe fn as_slice(&self) -> &[u8] {
170 assert!(self.data.is_initialized());
171 let internal_data = self
172 .data
173 .get_typed_array()
174 .expect("Failed to get Data from ImageData.");
175 // NOTE(nox): This is just as unsafe as `as_slice` itself even though we
176 // are extending the lifetime of the slice, because the data in
177 // this ImageData instance will never change. The method is thus unsafe
178 // because the array may be manipulated from JS while the reference
179 // is live.
180 unsafe {
181 let ptr: *const [u8] = internal_data.as_slice() as *const _;
182 &*ptr
183 }
184 }
185
186 /// Nothing must change the array on the JS side while the slice is live.
187 #[expect(unsafe_code)]
188 pub(crate) unsafe fn get_rect(&self, rect: Rect<u32>) -> Cow<'_, [u8]> {
189 pixels::rgba8_get_rect(unsafe { self.as_slice() }, self.get_size().to_u32(), rect)
190 }
191
192 #[expect(unsafe_code)]
193 pub(crate) fn get_snapshot_rect(&self, rect: Rect<u32>) -> Snapshot {
194 Snapshot::from_vec(
195 rect.size,
196 SnapshotPixelFormat::RGBA,
197 SnapshotAlphaMode::Transparent {
198 premultiplied: false,
199 },
200 unsafe { self.get_rect(rect).into_owned() },
201 )
202 }
203
204 #[expect(unsafe_code)]
205 pub(crate) fn to_shared_memory(&self) -> IpcSharedMemory {
206 // This is safe because we copy the slice content
207 IpcSharedMemory::from_bytes(unsafe { self.as_slice() })
208 }
209
210 #[expect(unsafe_code)]
211 pub(crate) fn to_vec(&self) -> Vec<u8> {
212 // This is safe because we copy the slice content
213 unsafe { self.as_slice() }.to_vec()
214 }
215}
216
217impl Serializable for ImageData {
218 type Index = ImageDataIndex;
219 type Data = SerializableImageData;
220
221 /// <https://html.spec.whatwg.org/multipage/#the-imagedata-interface:serializable-objects>
222 fn serialize(&self) -> Result<(ImageDataId, Self::Data), ()> {
223 // Step 1 Set serialized.[[Data]] to the sub-serialization of the value of value's data attribute.
224 let data = self.to_vec();
225
226 // Step 2 Set serialized.[[Width]] to the value of value's width attribute.
227 // Step 3 Set serialized.[[Height]] to the value of value's height attribute.
228 // Step 4 Set serialized.[[ColorSpace]] to the value of value's colorSpace attribute.
229 // Step 5 Set serialized.[[PixelFormat]] to the value of value's pixelFormat attribute.
230 // Note: Since we don't support Float16Array and display-p3 color space
231 // we don't need to serialize colorSpace and pixelFormat
232 let serialized = SerializableImageData {
233 data,
234 width: self.width,
235 height: self.height,
236 };
237 Ok((ImageDataId::new(), serialized))
238 }
239
240 /// <https://html.spec.whatwg.org/multipage/#the-imagedata-interface:deserialization-steps>
241 fn deserialize(
242 owner: &GlobalScope,
243 serialized: Self::Data,
244 can_gc: CanGc,
245 ) -> Result<DomRoot<Self>, ()> {
246 // Step 1 Initialize value's data attribute to the sub-deserialization of serialized.[[Data]].
247 // Step 2 Initialize value's width attribute to serialized.[[Width]].
248 // Step 3 Initialize value's height attribute to serialized.[[Height]].
249 // Step 4 Initialize value's colorSpace attribute to serialized.[[ColorSpace]].
250 // Step 5 Initialize value's pixelFormat attribute to serialized.[[PixelFormat]].
251 ImageData::new(
252 owner,
253 serialized.width,
254 serialized.height,
255 Some(serialized.data),
256 can_gc,
257 )
258 .map_err(|_| ())
259 }
260
261 fn serialized_storage<'a>(
262 reader: StructuredData<'a, '_>,
263 ) -> &'a mut Option<FxHashMap<ImageDataId, Self::Data>> {
264 match reader {
265 StructuredData::Reader(r) => &mut r.image_data,
266 StructuredData::Writer(w) => &mut w.image_data,
267 }
268 }
269}
270
271impl ImageDataMethods<crate::DomTypeHolder> for ImageData {
272 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata>
273 fn Constructor(
274 global: &GlobalScope,
275 proto: Option<HandleObject>,
276 can_gc: CanGc,
277 sw: u32,
278 sh: u32,
279 settings: &ImageDataSettings,
280 ) -> Fallible<DomRoot<Self>> {
281 // 1. If one or both of sw and sh are zero, then throw an "IndexSizeError" DOMException.
282 if sw == 0 || sh == 0 {
283 return Err(Error::IndexSize(None));
284 }
285
286 // When a constructor is called for an ImageData that is too large, other browsers throw
287 // IndexSizeError rather than RangeError here, so we do the same.
288 pixels::compute_rgba8_byte_length_if_within_limit(sw as usize, sh as usize)
289 .ok_or(Error::IndexSize(None))?;
290
291 // 2. Initialize this given sw, sh, and settings.
292 // 3. Initialize the image data of this to transparent black.
293 Self::initialize(sw, sh, settings, None, None, global, proto, can_gc)
294 }
295
296 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-with-data>
297 fn Constructor_(
298 global: &GlobalScope,
299 proto: Option<HandleObject>,
300 can_gc: CanGc,
301 data: CustomAutoRooterGuard<Uint8ClampedArray>,
302 sw: u32,
303 sh: Option<u32>,
304 settings: &ImageDataSettings,
305 ) -> Fallible<DomRoot<Self>> {
306 // 1. Let bytesPerPixel be 4 if settings["pixelFormat"] is "rgba-unorm8"; otherwise 8.
307 let bytes_per_pixel = match settings.pixelFormat {
308 ImageDataPixelFormat::Rgba_unorm8 => 4,
309 };
310 // 2. Let length be the buffer source byte length of data.
311 let length = data.len();
312 if length == 0 {
313 return Err(Error::InvalidState(None));
314 }
315 // 3. If length is not a nonzero integral multiple of bytesPerPixel,
316 // then throw an "InvalidStateError" DOMException.
317 if length % bytes_per_pixel != 0 {
318 return Err(Error::InvalidState(None));
319 }
320 // 4. Let length be length divided by bytesPerPixel.
321 let length = length / bytes_per_pixel;
322 // 5. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
323 if sw == 0 || length % sw as usize != 0 {
324 return Err(Error::IndexSize(None));
325 }
326 // 6. Let height be length divided by sw.
327 let height = length / sw as usize;
328 // 7. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
329 if sh.is_some_and(|x| height != x as usize) {
330 return Err(Error::IndexSize(None));
331 }
332 // 8. Initialize this given sw, sh, settings, and source set to data.
333 Self::initialize(
334 sw,
335 height as u32,
336 settings,
337 Some(data),
338 None,
339 global,
340 proto,
341 can_gc,
342 )
343 }
344
345 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-width>
346 fn Width(&self) -> u32 {
347 self.width
348 }
349
350 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-height>
351 fn Height(&self) -> u32 {
352 self.height
353 }
354
355 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
356 fn GetData(&self, _: JSContext) -> Fallible<Uint8ClampedArray> {
357 self.data.get_typed_array().map_err(|_| Error::JSFailed)
358 }
359
360 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-pixelformat>
361 fn PixelFormat(&self) -> ImageDataPixelFormat {
362 self.pixel_format
363 }
364
365 /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-colorspace>
366 fn ColorSpace(&self) -> PredefinedColorSpace {
367 self.color_space
368 }
369}