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