Skip to content

Truly independent web browser

LibWeb: Honor ImageBitmap vertical orientation

Andreas Klingb703d63533b6
diff --git a/Libraries/LibWeb/HTML/ImageBitmap.idl b/Libraries/LibWeb/HTML/ImageBitmap.idl
index 172fddca3e3..a552bc317dc 100644
--- a/Libraries/LibWeb/HTML/ImageBitmap.idl
+++ b/Libraries/LibWeb/HTML/ImageBitmap.idl
@@ -12,13 +12,14 @@ typedef (CanvasImageSource or
Blob or
ImageData) ImageBitmapSource;
-enum ImageOrientation { "from-image", "flipY" };
+// INTEROP: Chromium and WebKit still accept the legacy "none" orientation.
+enum ImageOrientation { "from-image", "flipY", "none" };
enum PremultiplyAlpha { "none", "premultiply", "default" };
enum ColorSpaceConversion { "none", "default" };
enum ResizeQuality { "pixelated", "low", "medium", "high" };
dictionary ImageBitmapOptions {
- // FIXME: ImageOrientation imageOrientation = "from-image";
+ ImageOrientation imageOrientation = "from-image";
// FIXME: PremultiplyAlpha premultiplyAlpha = "default";
ColorSpaceConversion colorSpaceConversion = "default";
[EnforceRange] unsigned long resizeWidth;
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index 3816b9bec8e..a57ab878e7b 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -10,6 +10,7 @@
#include <AK/Base64.h>
#include <AK/NumericLimits.h>
#include <AK/QuickSort.h>
+#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
@@ -412,8 +413,18 @@ static ErrorOr<Gfx::DecodedImageFrame> crop_to_the_source_rectangle_with_formatt
}
auto output_color_space = input_frame.color_space();
- // FIXME: 8. If the value of the imageOrientation member of options is "flipY", output must be flipped vertically,
- // disregarding any image orientation metadata of the source (such as EXIF metadata), if any. [EXIF]
+ // 8. If the value of the imageOrientation member of options is "flipY", output must be flipped vertically,
+ // disregarding any image orientation metadata of the source (such as EXIF metadata), if any. [EXIF]
+ // If the value is "from-image", no extra step is needed.
+ if (options.image_orientation == Bindings::ImageOrientation::FlipY) {
+ // NB: Cropping and scaling produced a copy, so swapping rows does not modify the source bitmap.
+ for (i32 y = 0; y < output->height() / 2; ++y) {
+ auto* top_row = output->scanline(y);
+ auto* bottom_row = output->scanline(output->height() - y - 1);
+ for (i32 x = 0; x < output->width(); ++x)
+ AK::swap(top_row[x], bottom_row[x]);
+ }
+ }
// 9. If image is an img element or a Blob object, let val be the value of the colorSpaceConversion member of
// options, and then run these substeps:
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-flipY.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-flipY.txt
index a016ee79a6b..a05f059c2b6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-flipY.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-flipY.txt
@@ -2,27 +2,27 @@ Harness status: OK
Found 22 tests
-9 Pass
-13 Fail
+18 Pass
+4 Fail
Pass createImageBitmap from an HTMLCanvasElement imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from an HTMLCanvasElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from an HTMLCanvasElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
Fail createImageBitmap from an HTMLVideoElement imageOrientation: "from-image", and drawImage on the created ImageBitmap
Fail createImageBitmap from an HTMLVideoElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
Fail createImageBitmap from an HTMLVideoElement from a data URL imageOrientation: "from-image", and drawImage on the created ImageBitmap
Fail createImageBitmap from an HTMLVideoElement from a data URL imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from a bitmap HTMLImageElement imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from a bitmap HTMLImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from a bitmap HTMLImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from a vector HTMLImageElement imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from a vector HTMLImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from a vector HTMLImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from a bitmap SVGImageElement imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from a bitmap SVGImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from a bitmap SVGImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from a vector SVGImageElement imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from a vector SVGImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from a vector SVGImageElement imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from an OffscreenCanvas imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from an OffscreenCanvas imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from an OffscreenCanvas imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from an ImageData imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from an ImageData imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from an ImageData imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from an ImageBitmap imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from an ImageBitmap imageOrientation: "flipY", and drawImage on the created ImageBitmap
+Pass createImageBitmap from an ImageBitmap imageOrientation: "flipY", and drawImage on the created ImageBitmap
Pass createImageBitmap from a Blob imageOrientation: "from-image", and drawImage on the created ImageBitmap
-Fail createImageBitmap from a Blob imageOrientation: "flipY", and drawImage on the created ImageBitmap
\ No newline at end of file
+Pass createImageBitmap from a Blob imageOrientation: "flipY", and drawImage on the created ImageBitmap
\ No newline at end of file