Skip to main content

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