1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::borrow::Cow;
use std::ptr;
use std::vec::Vec;

use dom_struct::dom_struct;
use euclid::default::{Rect, Size2D};
use ipc_channel::ipc::IpcSharedMemory;
use js::jsapi::{Heap, JSObject};
use js::rust::HandleObject;
use js::typedarray::{ClampedU8, CreateWith, Uint8ClampedArray};

use super::bindings::buffer_source::{
    new_initialized_heap_buffer_source, BufferSource, HeapBufferSource, HeapTypedArrayInit,
};
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::ImageDataMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::{CanGc, JSContext};

#[dom_struct]
pub struct ImageData {
    reflector_: Reflector,
    width: u32,
    height: u32,
    #[ignore_malloc_size_of = "mozjs"]
    data: HeapBufferSource<ClampedU8>,
}

impl ImageData {
    #[allow(unsafe_code)]
    pub fn new(
        global: &GlobalScope,
        width: u32,
        height: u32,
        mut data: Option<Vec<u8>>,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<ImageData>> {
        let len = width * height * 4;
        unsafe {
            let cx = GlobalScope::get_cx();
            rooted!(in (*cx) let mut js_object = ptr::null_mut::<JSObject>());
            if let Some(ref mut d) = data {
                d.resize(len as usize, 0);
                let data = CreateWith::Slice(&d[..]);
                Uint8ClampedArray::create(*cx, data, js_object.handle_mut()).unwrap();
                Self::new_with_jsobject(global, None, width, Some(height), js_object.get(), can_gc)
            } else {
                Self::new_without_jsobject(global, None, width, height, can_gc)
            }
        }
    }

    #[allow(unsafe_code)]
    fn new_with_jsobject(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        width: u32,
        opt_height: Option<u32>,
        jsobject: *mut JSObject,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<ImageData>> {
        let heap_typed_array = match new_initialized_heap_buffer_source::<ClampedU8>(
            HeapTypedArrayInit::Buffer(BufferSource::Uint8ClampedArray(Heap::boxed(jsobject))),
        ) {
            Ok(heap_typed_array) => heap_typed_array,
            Err(_) => return Err(Error::JSFailed),
        };

        let typed_array = match heap_typed_array.get_buffer() {
            Ok(array) => array,
            Err(_) => {
                return Err(Error::Type(
                    "Argument to Image data is not an Uint8ClampedArray".to_owned(),
                ))
            },
        };

        let byte_len = unsafe { typed_array.as_slice().len() } as u32;
        if byte_len == 0 || byte_len % 4 != 0 {
            return Err(Error::InvalidState);
        }

        let len = byte_len / 4;
        if width == 0 || len % width != 0 {
            return Err(Error::IndexSize);
        }

        let height = len / width;
        if opt_height.is_some_and(|x| height != x) {
            return Err(Error::IndexSize);
        }

        let imagedata = Box::new(ImageData {
            reflector_: Reflector::new(),
            width,
            height,
            data: heap_typed_array,
        });

        Ok(reflect_dom_object_with_proto(
            imagedata, global, proto, can_gc,
        ))
    }

    fn new_without_jsobject(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        width: u32,
        height: u32,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<ImageData>> {
        if width == 0 || height == 0 {
            return Err(Error::IndexSize);
        }
        let cx = GlobalScope::get_cx();
        let len = width * height * 4;

        let heap_typed_array =
            match new_initialized_heap_buffer_source::<ClampedU8>(HeapTypedArrayInit::Info {
                len,
                cx,
            }) {
                Ok(heap_typed_array) => heap_typed_array,
                Err(_) => return Err(Error::JSFailed),
            };
        let imagedata = Box::new(ImageData {
            reflector_: Reflector::new(),
            width,
            height,
            data: heap_typed_array,
        });

        Ok(reflect_dom_object_with_proto(
            imagedata, global, proto, can_gc,
        ))
    }
    #[allow(unsafe_code)]
    pub fn to_shared_memory(&self) -> IpcSharedMemory {
        IpcSharedMemory::from_bytes(unsafe { self.as_slice() })
    }

    #[allow(unsafe_code)]
    pub unsafe fn get_rect(&self, rect: Rect<u64>) -> Cow<[u8]> {
        pixels::rgba8_get_rect(self.as_slice(), self.get_size().to_u64(), rect)
    }

    pub fn get_size(&self) -> Size2D<u32> {
        Size2D::new(self.Width(), self.Height())
    }

    /// Nothing must change the array on the JS side while the slice is live.
    #[allow(unsafe_code)]
    pub unsafe fn as_slice(&self) -> &[u8] {
        assert!(self.data.is_initialized());
        let internal_data = self
            .data
            .get_buffer()
            .expect("Failed to get Data from ImageData.");
        // NOTE(nox): This is just as unsafe as `as_slice` itself even though we
        // are extending the lifetime of the slice, because the data in
        // this ImageData instance will never change. The method is thus unsafe
        // because the array may be manipulated from JS while the reference
        // is live.
        let ptr: *const [u8] = internal_data.as_slice() as *const _;
        &*ptr
    }
}

impl ImageDataMethods for ImageData {
    /// <https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-3>
    fn Constructor(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        width: u32,
        height: u32,
    ) -> Fallible<DomRoot<Self>> {
        Self::new_without_jsobject(global, proto, width, height, can_gc)
    }

    /// <https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-4>
    fn Constructor_(
        _cx: JSContext,
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        jsobject: *mut JSObject,
        width: u32,
        opt_height: Option<u32>,
    ) -> Fallible<DomRoot<Self>> {
        Self::new_with_jsobject(global, proto, width, opt_height, jsobject, can_gc)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-width>
    fn Width(&self) -> u32 {
        self.width
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-height>
    fn Height(&self) -> u32 {
        self.height
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
    fn GetData(&self, _: JSContext) -> Fallible<Uint8ClampedArray> {
        self.data.get_buffer().map_err(|_| Error::JSFailed)
    }
}