Truly independent web browser
Compositor: Export RGB888 pixels with the requested alpha handling
sideshowbarker5b2d968d1f12
diff --git a/Services/Compositor/BitmapExport.cpp b/Services/Compositor/BitmapExport.cppindex 3e07459e39d..7ffcc70186e 100644--- a/Services/Compositor/BitmapExport.cpp+++ b/Services/Compositor/BitmapExport.cpp@@ -112,12 +112,6 @@ ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(int width = target_width.value_or(bitmap.width());int height = target_height.value_or(bitmap.height());- if (format == ExportFormat::RGB888 && (width != bitmap.width() || height != bitmap.height())) {- dbgln("FIXME: Ignoring target width and height because scaling is not implemented for this export format.");- width = bitmap.width();- height = bitmap.height();- }-Checked<size_t> buffer_pitch = width;int number_of_bytes = bytes_per_pixel_for_export_format(format);buffer_pitch *= number_of_bytes;@@ -130,22 +124,9 @@ ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(auto buffer = MUST(ByteBuffer::create_zeroed(buffer_pitch.value() * height));if (width > 0 && height > 0) {- if (format == ExportFormat::RGB888) {- // 24 bit RGB is not supported by Skia, so we need to handle this format ourselves.- auto* raw_buffer = buffer.data();- for (auto y = 0; y < height; y++) {- auto target_y = flags & ExportFlags::FlipY ? height - y - 1 : y;- for (auto x = 0; x < width; x++) {- auto pixel = bitmap.get_pixel(x, y);- auto buffer_offset = (target_y * buffer_pitch.value()) + (x * 3ull);- raw_buffer[buffer_offset + 0] = pixel.red();- raw_buffer[buffer_offset + 1] = pixel.green();- raw_buffer[buffer_offset + 2] = pixel.blue();- }- }- } else if (format == ExportFormat::RGBA5551 || format == ExportFormat::Luminance8 || format == ExportFormat::LuminanceAlpha88) {- // Skia has no color type for 16 bit RGBA with 1 bit of alpha, or for a luminance that's the red channel (its- // gray color type weighs all three channels), so we draw into 32 bit RGBA and pack that.+ if (format == ExportFormat::RGB888 || format == ExportFormat::RGBA5551 || format == ExportFormat::Luminance8 || format == ExportFormat::LuminanceAlpha88) {+ // Skia has no color type for 24 bit RGB, for 16 bit RGBA with 1 bit of alpha, or for a luminance that's the+ // red channel (its gray color type weighs all three channels), so we draw into 32 bit RGBA and pack that.Checked<size_t> rgba_pitch = width;rgba_pitch *= 4;if (rgba_pitch.has_overflow() || Checked<size_t>::multiplication_would_overflow(rgba_pitch.value(), height))@@ -159,6 +140,11 @@ ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(for (auto x = 0; x < width; x++) {auto const* source = source_row + (x * 4ull);switch (format) {+ case ExportFormat::RGB888:+ destination_row[(x * 3ull) + 0] = source[0];+ destination_row[(x * 3ull) + 1] = source[1];+ destination_row[(x * 3ull) + 2] = source[2];+ break;case ExportFormat::RGBA5551: {u16 pixel = ((source[0] >> 3) << 11) | ((source[1] >> 3) << 6) | ((source[2] >> 3) << 1) | (source[3] >> 7);ByteReader::store(destination_row + (x * 2ull), pixel);diff --git a/Tests/Compositor/TestBitmapExport.cpp b/Tests/Compositor/TestBitmapExport.cppindex 9f6cc6b1621..50b26aaf344 100644--- a/Tests/Compositor/TestBitmapExport.cpp+++ b/Tests/Compositor/TestBitmapExport.cpp@@ -183,13 +183,29 @@ TEST_CASE(export_bitmap_to_byte_buffer){ 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },},{- all_bitmap_formats,+ non_alpha_bitmap_formats,{ Premultiplied::No, Premultiplied::Yes },{ 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF },Gfx::ExportFormat::RGB888,{ Premultiplied::No, Premultiplied::Yes },{ 0x11, 0x22, 0x33, 0x55, 0x66, 0x77, 0x99, 0xAA, 0xBB, 0xDD, 0xEE, 0xFF },},+ {+ alpha_bitmap_formats,+ { Premultiplied::No },+ { 0xFFFF0000, 0x80FF0000, 0x00000000, 0xFF0000FF },+ Gfx::ExportFormat::RGB888,+ { Premultiplied::No },+ { 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF },+ },+ {+ alpha_bitmap_formats,+ { Premultiplied::No },+ { 0xFFFF0000, 0x80FF0000, 0x00000000, 0xFF0000FF },+ Gfx::ExportFormat::RGB888,+ { Premultiplied::Yes },+ { 0xFF, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF },+ },{alpha_bitmap_formats,{ Premultiplied::No },diff --git a/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txt b/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txtindex ee7724d05ad..1b3ac00d0f1 100644--- a/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txt+++ b/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txt@@ -1,3 +1,4 @@+RGB: white 255,255,255,255 | orange 200,100,50,255 | half-transparent white 255,255,255,255 | transparent 0,0,0,255 | error 0LUMINANCE: white 255,255,255,255 | orange 200,200,200,255 | half-transparent white 255,255,255,255 | transparent 0,0,0,255 | error 0LUMINANCE_ALPHA: white 255,255,255,255 | orange 200,200,200,255 | half-transparent white 255,255,255,128 | transparent 0,0,0,0 | error 0ALPHA: white 0,0,0,255 | orange 0,0,0,255 | half-transparent white 0,0,0,128 | transparent 0,0,0,0 | error 0diff --git a/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.html b/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.htmlindex f67017beb74..dee8c13a37f 100644--- a/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.html+++ b/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.html@@ -64,7 +64,7 @@gl.enableVertexAttribArray(position);gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);- for (const [name, format] of [["LUMINANCE", gl.LUMINANCE], ["LUMINANCE_ALPHA", gl.LUMINANCE_ALPHA], ["ALPHA", gl.ALPHA]])+ for (const [name, format] of [["RGB", gl.RGB], ["LUMINANCE", gl.LUMINANCE], ["LUMINANCE_ALPHA", gl.LUMINANCE_ALPHA], ["ALPHA", gl.ALPHA]])println(`${name}: ${sampledTexels(gl, format)}`);});</script>