From e9d1a1fa9d698c0f6507f1b2ab719ce0fc5a0550 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sat, 26 Mar 2022 14:15:46 +0000 Subject: Include: Bitmap: Add API for setting core bitmap format. --- desktop/Makefile | 2 +- desktop/bitmap.c | 46 ++++++++++++++++++ desktop/bitmap.h | 124 +++++++++++++++++++++++++++++++++++++++++++++++ include/netsurf/bitmap.h | 67 +++++++++++++++++++++++++ 4 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 desktop/bitmap.c create mode 100644 desktop/bitmap.h diff --git a/desktop/Makefile b/desktop/Makefile index 63749a6f2..5e190275d 100644 --- a/desktop/Makefile +++ b/desktop/Makefile @@ -12,7 +12,7 @@ desktop/version.c: testament $(OBJROOT)/testament.h # S_BROWSER are sources related to full browsers but are common # between RISC OS, GTK, BeOS and AmigaOS builds -S_BROWSER := browser.c browser_window.c browser_history.c \ +S_BROWSER := bitmap.c browser.c browser_window.c browser_history.c \ download.c frames.c netsurf.c cw_helper.c \ save_complete.c save_text.c selection.c textinput.c gui_factory.c \ save_pdf.c font_haru.c diff --git a/desktop/bitmap.c b/desktop/bitmap.c new file mode 100644 index 000000000..48065d080 --- /dev/null +++ b/desktop/bitmap.c @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Michael Drake + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** \file + * Internal core bitmap interface. + */ + +#include +#include +#include + +#include "utils/log.h" +#include "utils/errors.h" + +#include "desktop/bitmap.h" +#include "desktop/gui_internal.h" + +/** The client bitmap format. */ +bitmap_fmt_t bitmap_fmt; + +/** The client bitmap colour channel layout. */ +struct bitmap_colour_layout bitmap_layout; + +/* Exported function, documented in include/netsurf/bitmap.h */ +void bitmap_set_format(const bitmap_fmt_t *bitmap_format) +{ + bitmap_fmt = *bitmap_format; + + bitmap_fmt.layout = bitmap_sanitise_bitmap_layout(bitmap_fmt.layout); + bitmap_layout = bitmap__get_colour_layout(&bitmap_fmt); +} diff --git a/desktop/bitmap.h b/desktop/bitmap.h new file mode 100644 index 000000000..490928980 --- /dev/null +++ b/desktop/bitmap.h @@ -0,0 +1,124 @@ +/* + * Copyright 2022 Michael Drake + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** \file + * Internal core bitmap interface. + */ + +#ifndef _NETSURF_DESKTOP_BITMAP_H_ +#define _NETSURF_DESKTOP_BITMAP_H_ + +#include + +#include "netsurf/bitmap.h" + +/** The client bitmap format. */ +extern bitmap_fmt_t bitmap_fmt; + +/** Pixel format: colour component order. */ +struct bitmap_colour_layout { + uint8_t r; /**< Byte offset within pixel to red component. */ + uint8_t g; /**< Byte offset within pixel to green component. */ + uint8_t b; /**< Byte offset within pixel to blue component. */ + uint8_t a; /**< Byte offset within pixel to alpha component. */ +}; + +/** + * Get the colour layout for the given bitmap format. + * + * \param[in] fmt Pixel format to get channel layout for, + * \return channel layout structure. + */ +static inline struct bitmap_colour_layout bitmap__get_colour_layout( + const bitmap_fmt_t *fmt) +{ + switch (fmt->layout) { + default: + /* Fall through. */ + case BITMAP_LAYOUT_R8G8B8A8: + return (struct bitmap_colour_layout) { + .r = 0, + .g = 1, + .b = 2, + .a = 3, + }; + + case BITMAP_LAYOUT_B8G8R8A8: + return (struct bitmap_colour_layout) { + .b = 0, + .g = 1, + .r = 2, + .a = 3, + }; + + case BITMAP_LAYOUT_A8R8G8B8: + return (struct bitmap_colour_layout) { + .a = 0, + .r = 1, + .g = 2, + .b = 3, + }; + + case BITMAP_LAYOUT_A8B8G8R8: + return (struct bitmap_colour_layout) { + .a = 0, + .b = 1, + .g = 2, + .r = 3, + }; + } +} + +/** + * Sanitise bitmap pixel component layout. + * + * Map endian-dependant layouts to byte-wise layout for the host. + * + * \param[in] layout Layout to convert. + * \return sanitised layout. + */ +static inline enum bitmap_layout bitmap_sanitise_bitmap_layout( + enum bitmap_layout layout) +{ + bool le = endian_host_is_le(); + + switch (layout) { + case BITMAP_LAYOUT_RGBA8888: + layout = (le) ? BITMAP_LAYOUT_A8B8G8R8 + : BITMAP_LAYOUT_R8G8B8A8; + break; + case BITMAP_LAYOUT_BGRA8888: + layout = (le) ? BITMAP_LAYOUT_A8R8G8B8 + : BITMAP_LAYOUT_B8G8R8A8; + break; + case BITMAP_LAYOUT_ARGB8888: + layout = (le) ? BITMAP_LAYOUT_B8G8R8A8 + : BITMAP_LAYOUT_A8R8G8B8; + break; + case BITMAP_LAYOUT_ABGR8888: + layout = (le) ? BITMAP_LAYOUT_R8G8B8A8 + : BITMAP_LAYOUT_A8B8G8R8; + break; + default: + break; + } + + return layout; +} + +#endif diff --git a/include/netsurf/bitmap.h b/include/netsurf/bitmap.h index a5be75d74..da1c284e6 100644 --- a/include/netsurf/bitmap.h +++ b/include/netsurf/bitmap.h @@ -61,10 +61,77 @@ enum gui_bitmap_flags { BITMAP_CLEAR = (1 << 1), /**< memory should be wiped to 0 */ }; +/** + * NetSurf bitmap pixel layout. + * + * All pixels are 32 bits per pixel (bpp). The different layouts allow control + * over the ordering of colour channels. All colour channels are 8 bits wide. + */ +enum bitmap_layout { + /** Bite-wise RGBA: Byte order: 0xRR, 0xGG, 0xBB, 0xAA. */ + BITMAP_LAYOUT_R8G8B8A8, + + /** Bite-wise BGRA: Byte order: 0xBB, 0xGG, 0xRR, 0xAA. */ + BITMAP_LAYOUT_B8G8R8A8, + + /** Bite-wise ARGB: Byte order: 0xAA, 0xRR, 0xGG, 0xBB. */ + BITMAP_LAYOUT_A8R8G8B8, + + /** Bite-wise ABGR: Byte order: 0xAA, 0xBB, 0xGG, 0xRR. */ + BITMAP_LAYOUT_A8B8G8R8, + + /** + * 32-bit RGBA (0xRRGGBBAA). + * + * * On little endian host, same as \ref BITMAP_LAYOUT_A8B8G8R8. + * * On big endian host, same as \ref BITMAP_LAYOUT_R8G8B8A8. + */ + BITMAP_LAYOUT_RGBA8888, + + /** + * 32-bit BGRA (0xBBGGRRAA). + * + * * On little endian host, same as \ref BITMAP_LAYOUT_A8R8G8B8. + * * On big endian host, same as \ref BITMAP_LAYOUT_B8G8R8A8. + */ + BITMAP_LAYOUT_BGRA8888, + + /** + * 32-bit ARGB (0xAARRGGBB). + * + * * On little endian host, same as \ref BITMAP_LAYOUT_B8G8R8A8. + * * On big endian host, same as \ref BITMAP_LAYOUT_A8R8G8B8. + */ + BITMAP_LAYOUT_ARGB8888, + + /** + * 32-bit BGRA (0xAABBGGRR). + * + * * On little endian host, same as \ref BITMAP_LAYOUT_R8G8B8A8. + * * On big endian host, same as \ref BITMAP_LAYOUT_A8B8G8R8. + */ + BITMAP_LAYOUT_ABGR8888, +}; + +/** Bitmap format specifier. */ +typedef struct bitmap_fmt { + enum bitmap_layout layout; /** Colour component layout. */ +} bitmap_fmt_t; + struct content; struct bitmap; struct hlcache_handle; +/** + * Set client bitmap format. + * + * Set this to ensure that the bitmaps decoded by the core are in the + * correct format for the front end. + * + * \param[in] bitmap_format The bitmap format specification to set. + */ +void bitmap_set_format(const bitmap_fmt_t *bitmap_format); + /** * Bitmap operations. */ -- cgit v1.2.3