Skip to content

Truly independent web browser

6.7 KB
Raw Blame
/*
 * Copyright (c) 2024-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/ByteReader.h>
#include <Compositor/BitmapExport.h>
#include <Compositor/SkiaUtils.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ColorSpace.h>

#include <core/SkCanvas.h>
#include <core/SkColorSpace.h>
#include <core/SkImage.h>
#include <core/SkSurface.h>

namespace Gfx {

StringView export_format_name(ExportFormat format)
{
    switch (format) {
#define ENUMERATE_EXPORT_FORMAT(format) \
    case Gfx::ExportFormat::format:     \
        return #format##sv;
        ENUMERATE_EXPORT_FORMATS(ENUMERATE_EXPORT_FORMAT)
#undef ENUMERATE_EXPORT_FORMAT
    }
    VERIFY_NOT_REACHED();
}

static int bytes_per_pixel_for_export_format(ExportFormat format)
{
    switch (format) {
    case ExportFormat::Luminance8:
    case ExportFormat::Alpha8:
        return 1;
    case ExportFormat::LuminanceAlpha88:
    case ExportFormat::RGB565:
    case ExportFormat::RGBA5551:
    case ExportFormat::RGBA4444:
        return 2;
    case ExportFormat::RGB888:
        return 3;
    case ExportFormat::RGBA8888:
        return 4;
    default:
        VERIFY_NOT_REACHED();
    }
}

static SkColorType export_format_to_skia_color_type(ExportFormat format)
{
    switch (format) {
    case ExportFormat::Alpha8:
        return SkColorType::kAlpha_8_SkColorType;
    case ExportFormat::RGB565:
        return SkColorType::kRGB_565_SkColorType;
    case ExportFormat::RGBA4444:
        return SkColorType::kARGB_4444_SkColorType;
    case ExportFormat::Luminance8:
    case ExportFormat::LuminanceAlpha88:
    case ExportFormat::RGBA5551:
    case ExportFormat::RGB888:
        // These need to be converted manually because Skia has no color type for them.
        VERIFY_NOT_REACHED();
    case ExportFormat::RGBA8888:
        return SkColorType::kRGBA_8888_SkColorType;
    default:
        VERIFY_NOT_REACHED();
    }
}

static ErrorOr<void> draw_bitmap_into_pixels(Bitmap const& bitmap, ColorSpace const& color_space, SkColorType color_type, int flags, int width, int height, void* pixels, size_t pitch)
{
    auto image = sk_image_from_bitmap(bitmap, color_space);
    if (!image)
        return Error::from_string_literal("Failed to create a Skia image for this Bitmap");

    auto image_info = SkImageInfo::Make(
        width,
        height,
        color_type,
        flags & ExportFlags::PremultiplyAlpha ? SkAlphaType::kPremul_SkAlphaType : SkAlphaType::kUnpremul_SkAlphaType,
        SkColorSpace::MakeSRGB());
    auto surface = SkSurfaces::WrapPixels(image_info, pixels, pitch);
    if (!surface)
        return Error::from_string_literal("Failed to create a Skia surface for the exported pixels");
    auto* surface_canvas = surface->getCanvas();
    auto dst_rect = Gfx::to_skia_rect(Gfx::Rect { 0, 0, width, height });

    if (flags & ExportFlags::FlipY) {
        surface_canvas->translate(0, dst_rect.height());
        surface_canvas->scale(1, -1);
    }

    surface_canvas->drawImageRect(
        image.get(),
        dst_rect,
        Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor));
    return {};
}

ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(
    Bitmap const& bitmap,
    ColorSpace const& color_space,
    ExportFormat format,
    int flags,
    Optional<int> target_width,
    Optional<int> target_height)
{
    int width = target_width.value_or(bitmap.width());
    int height = target_height.value_or(bitmap.height());

    Checked<size_t> buffer_pitch = width;
    int number_of_bytes = bytes_per_pixel_for_export_format(format);
    buffer_pitch *= number_of_bytes;
    if (buffer_pitch.has_overflow())
        return Error::from_string_literal("Gfx::export_bitmap_to_byte_buffer size overflow");

    if (Checked<size_t>::multiplication_would_overflow(buffer_pitch.value(), height))
        return Error::from_string_literal("Gfx::export_bitmap_to_byte_buffer size overflow");

    auto buffer = MUST(ByteBuffer::create_zeroed(buffer_pitch.value() * height));

    if (width > 0 && height > 0) {
        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))
                return Error::from_string_literal("Gfx::export_bitmap_to_byte_buffer size overflow");
            auto rgba_buffer = TRY(ByteBuffer::create_zeroed(rgba_pitch.value() * height));
            TRY(draw_bitmap_into_pixels(bitmap, color_space, SkColorType::kRGBA_8888_SkColorType, flags, width, height, rgba_buffer.data(), rgba_pitch.value()));

            for (auto y = 0; y < height; y++) {
                auto const* source_row = rgba_buffer.data() + (y * rgba_pitch.value());
                auto* destination_row = buffer.data() + (y * buffer_pitch.value());
                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);
                        break;
                    }
                    case ExportFormat::Luminance8:
                        destination_row[x] = source[0];
                        break;
                    case ExportFormat::LuminanceAlpha88:
                        destination_row[(x * 2ull) + 0] = source[0];
                        destination_row[(x * 2ull) + 1] = source[3];
                        break;
                    default:
                        VERIFY_NOT_REACHED();
                    }
                }
            }
        } else {
            TRY(draw_bitmap_into_pixels(bitmap, color_space, export_format_to_skia_color_type(format), flags, width, height, buffer.data(), buffer_pitch.value()));
        }
    } else {
        VERIFY(buffer.is_empty());
    }

    return BitmapExportResult {
        .buffer = move(buffer),
        .width = width,
        .height = height,
    };
}

}