script/dom/
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 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    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
34    #[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    /// <https://html.spec.whatwg.org/multipage/#initialize-an-imagedata-object>
76    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        // 1. If source was given:
87        let data = if let Some(source) = source {
88            // 1. If settings["pixelFormat"] equals "rgba-unorm8" and source is not a Uint8ClampedArray,
89            // then throw an "InvalidStateError" DOMException.
90            // 2. If settings["pixelFormat"] is "rgba-float16" and source is not a Float16Array,
91            // then throw an "InvalidStateError" DOMException.
92            if !matches!(settings.pixelFormat, ImageDataPixelFormat::Rgba_unorm8) {
93                // we currently support only rgba-unorm8
94                return Err(Error::InvalidState);
95            }
96            // 3. Initialize the data attribute of imageData to source.
97            HeapBufferSource::<ClampedU8>::from_view(source)
98        } else {
99            // 2. Otherwise (source was not given):
100            match settings.pixelFormat {
101                ImageDataPixelFormat::Rgba_unorm8 => {
102                    // 1. If settings["pixelFormat"] is "rgba-unorm8",
103                    // then initialize the data attribute of imageData to a new Uint8ClampedArray object.
104                    // The Uint8ClampedArray object must use a new ArrayBuffer for its storage,
105                    // and must have a zero byte offset and byte length equal to the length of its storage, in bytes.
106                    // The storage ArrayBuffer must have a length of 4 × rows × pixelsPerRow bytes.
107                    // 3. If the storage ArrayBuffer could not be allocated,
108                    // then rethrow the RangeError thrown by JavaScript, and return.
109                    create_heap_buffer_source_with_length(
110                        GlobalScope::get_cx(),
111                        4 * rows * pixels_per_row,
112                        can_gc,
113                    )?
114                },
115                // 3. Otherwise, if settings["pixelFormat"] is "rgba-float16",
116                // then initialize the data attribute of imageData to a new Float16Array object.
117                // The Float16Array object must use a new ArrayBuffer for its storage,
118                // and must have a zero byte offset and byte length equal to the length of its storage, in bytes.
119                // The storage ArrayBuffer must have a length of 8 × rows × pixelsPerRow bytes.
120                // not implemented yet
121            }
122        };
123        // 3. Initialize the width attribute of imageData to pixelsPerRow.
124        let width = pixels_per_row;
125        // 4. Initialize the height attribute of imageData to rows.
126        let height = rows;
127        // 5. Initialize the pixelFormat attribute of imageData to settings["pixelFormat"].
128        let pixel_format = settings.pixelFormat;
129        // 6. If settings["colorSpace"] exists,
130        // then initialize the colorSpace attribute of imageData to settings["colorSpace"].
131        let color_space = settings
132            .colorSpace
133            // 7. Otherwise, if defaultColorSpace was given,
134            // then initialize the colorSpace attribute of imageData to defaultColorSpace.
135            .or(default_color_space)
136            // 8. Otherwise, initialize the colorSpace attribute of imageData to "srgb".
137            .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    /// Nothing must change the array on the JS side while the slice is live.
163    #[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        // NOTE(nox): This is just as unsafe as `as_slice` itself even though we
171        // are extending the lifetime of the slice, because the data in
172        // this ImageData instance will never change. The method is thus unsafe
173        // because the array may be manipulated from JS while the reference
174        // is live.
175        unsafe {
176            let ptr: *const [u8] = internal_data.as_slice() as *const _;
177            &*ptr
178        }
179    }
180
181    /// Nothing must change the array on the JS side while the slice is live.
182    #[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        // This is safe because we copy the slice content
202        IpcSharedMemory::from_bytes(unsafe { self.as_slice() })
203    }
204
205    #[allow(unsafe_code)]
206    pub(crate) fn to_vec(&self) -> Vec<u8> {
207        // This is safe because we copy the slice content
208        unsafe { self.as_slice() }.to_vec()
209    }
210}
211
212impl ImageDataMethods<crate::DomTypeHolder> for ImageData {
213    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata>
214    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        // 1. If one or both of sw and sh are zero, then throw an "IndexSizeError" DOMException.
223        if sw == 0 || sh == 0 {
224            return Err(Error::IndexSize);
225        }
226
227        // When a constructor is called for an ImageData that is too large, other browsers throw
228        // IndexSizeError rather than RangeError here, so we do the same.
229        pixels::compute_rgba8_byte_length_if_within_limit(sw as usize, sh as usize)
230            .ok_or(Error::IndexSize)?;
231
232        // 2. Initialize this given sw, sh, and settings.
233        // 3. Initialize the image data of this to transparent black.
234        Self::initialize(sw, sh, settings, None, None, global, proto, can_gc)
235    }
236
237    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-with-data>
238    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        // 1. Let bytesPerPixel be 4 if settings["pixelFormat"] is "rgba-unorm8"; otherwise 8.
248        let bytes_per_pixel = match settings.pixelFormat {
249            ImageDataPixelFormat::Rgba_unorm8 => 4,
250        };
251        // 2. Let length be the buffer source byte length of data.
252        let length = data.len();
253        if length == 0 {
254            return Err(Error::InvalidState);
255        }
256        // 3. If length is not a nonzero integral multiple of bytesPerPixel,
257        // then throw an "InvalidStateError" DOMException.
258        if length % bytes_per_pixel != 0 {
259            return Err(Error::InvalidState);
260        }
261        // 4. Let length be length divided by bytesPerPixel.
262        let length = length / bytes_per_pixel;
263        // 5. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
264        if sw == 0 || length % sw as usize != 0 {
265            return Err(Error::IndexSize);
266        }
267        // 6. Let height be length divided by sw.
268        let height = length / sw as usize;
269        // 7. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
270        if sh.is_some_and(|x| height != x as usize) {
271            return Err(Error::IndexSize);
272        }
273        // 8. Initialize this given sw, sh, settings, and source set to data.
274        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    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-width>
287    fn Width(&self) -> u32 {
288        self.width
289    }
290
291    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-height>
292    fn Height(&self) -> u32 {
293        self.height
294    }
295
296    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
297    fn GetData(&self, _: JSContext) -> Fallible<Uint8ClampedArray> {
298        self.data.get_typed_array().map_err(|_| Error::JSFailed)
299    }
300
301    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-pixelformat>
302    fn PixelFormat(&self) -> ImageDataPixelFormat {
303        self.pixel_format
304    }
305
306    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-colorspace>
307    fn ColorSpace(&self) -> PredefinedColorSpace {
308        self.color_space
309    }
310}