diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp index 011eb545bbf..9a614121738 100644 --- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp +++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp @@ -242,11 +242,33 @@ GC::Ref WindowOrWorkerGlobalScopeMixin::create_image_bitmap_imp [&](CanvasImageSource const& image_source) { image_source.visit( // -> img - [&](GC::Root const&) { - dbgln("(STUBBED) createImageBitmap() for HTMLImageElement"); - auto const error = JS::Error::create(realm, "Not Implemented: createImageBitmap() for HTMLImageElement"sv); - TemporaryExecutionContext const context { relevant_realm(p->promise()), TemporaryExecutionContext::CallbacksEnabled::Yes }; - WebIDL::reject_promise(realm, *p, error); + [&](GC::Root const& image_element) { + // 1. If image's media data has no natural dimensions (e.g., it's a vector graphic with no specified content size) and options's resizeWidth or options's resizeHeight is not present, then return a promise rejected with an "InvalidStateError" DOMException. + auto const has_natural_dimensions = image_element->intrinsic_width().has_value() && image_element->intrinsic_height().has_value(); + if (!has_natural_dimensions && (!options.has_value() || !options->resize_width.has_value() || !options->resize_width.has_value())) { + WebIDL::reject_promise(realm, *p, WebIDL::InvalidStateError::create(image_bitmap->realm(), "Image data is detached"_string)); + return; + } + + // 2. If image's media data has no natural dimensions (e.g., it's a vector graphic with no specified content size), it should be rendered to a bitmap of the size specified by the resizeWidth and the resizeHeight options. + // 3. Set imageBitmap's bitmap data to a copy of image's media data, cropped to the source rectangle with formatting. If this is an animated image, imageBitmap's bitmap data must only be taken from the default image of the animation (the one that the format defines is to be used when animation is not supported or is disabled), or, if there is no such image, the first frame of the animation. + // FIXME: Actually crop the image to the source rectangle with formatting: https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#cropped-to-the-source-rectangle-with-formatting + RefPtr immutable_bitmap; + if (has_natural_dimensions) { + immutable_bitmap = image_element->default_image_bitmap(Gfx::IntSize { *image_element->intrinsic_width(), *image_element->intrinsic_height() }); + } else { + immutable_bitmap = image_element->default_image_bitmap(Gfx::IntSize { *options->resize_width, *options->resize_height }); + } + image_bitmap->set_bitmap(MUST(immutable_bitmap->bitmap()->clone())); + + // FIXME: 4. If image is not origin-clean, then set the origin-clean flag of imageBitmap's bitmap to false. + + // 5. Queue a global task, using the bitmap task source, to resolve promise with imageBitmap. + queue_global_task(Task::Source::BitmapTask, image_bitmap, GC::create_function(realm.heap(), [p, image_bitmap] { + auto& realm = relevant_realm(image_bitmap); + TemporaryExecutionContext const context { realm, TemporaryExecutionContext::CallbacksEnabled::Yes }; + WebIDL::resolve_promise(realm, *p, image_bitmap); + })); }, // -> SVG image [&](GC::Root const&) { diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.txt new file mode 100644 index 00000000000..39effcaf7df --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.txt @@ -0,0 +1,13 @@ +Harness status: OK + +Found 8 tests + +8 Pass +Pass createImageBitmap with Orientation 1 +Pass createImageBitmap with Orientation 2 +Pass createImageBitmap with Orientation 3 +Pass createImageBitmap with Orientation 4 +Pass createImageBitmap with Orientation 5 +Pass createImageBitmap with Orientation 6 +Pass createImageBitmap with Orientation 7 +Pass createImageBitmap with Orientation 8 \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.html b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.html new file mode 100644 index 00000000000..8aea4daf2b3 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.html @@ -0,0 +1,61 @@ + +Test that createImageBitmap honors EXIF orientation + + + + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_1.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_1.jpg new file mode 100644 index 00000000000..0f0e8866b4d Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_1.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_2.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_2.jpg new file mode 100644 index 00000000000..526f7a6c829 Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_2.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_3.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_3.jpg new file mode 100644 index 00000000000..a21e521c2d6 Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_3.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_4.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_4.jpg new file mode 100644 index 00000000000..c4380b1e671 Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_4.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_5.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_5.jpg new file mode 100644 index 00000000000..0bdd89aa1bb Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_5.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_6.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_6.jpg new file mode 100644 index 00000000000..f197760a111 Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_6.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_7.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_7.jpg new file mode 100644 index 00000000000..9b1a346888d Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_7.jpg differ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_8.jpg b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_8.jpg new file mode 100644 index 00000000000..41d2fbe7f0f Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/canvas/element/manual/imagebitmap/resources/squares_8.jpg differ