NetSurf
bitmap.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 Michael Drake <tlsa@nesturf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/** \file
20 * Internal core bitmap interface.
21 */
22
23#ifndef _NETSURF_DESKTOP_BITMAP_H_
24#define _NETSURF_DESKTOP_BITMAP_H_
25
26#include <nsutils/endian.h>
27
28#include "netsurf/types.h"
29#include "netsurf/bitmap.h"
30
31/** Pixel format: colour component order. */
33 uint8_t r; /**< Byte offset within pixel to red component. */
34 uint8_t g; /**< Byte offset within pixel to green component. */
35 uint8_t b; /**< Byte offset within pixel to blue component. */
36 uint8_t a; /**< Byte offset within pixel to alpha component. */
37};
38
39/** The client bitmap format. */
41
42/** The client bitmap colour channel layout. */
44
45/**
46 * Convert a bitmap pixel to a NetSurf colour (0xAARRGGBB).
47 *
48 * The bitmap must be in the client format.
49 *
50 * \param[in] Pointer to a pixel in the bitmap's pixel data.
51 * \return The corresponding NetSurf colour.
52 */
53static inline colour bitmap_pixel_to_colour(const uint8_t *pixel)
54{
55 return (pixel[bitmap_layout.r] << 0) |
56 (pixel[bitmap_layout.g] << 8) |
57 (pixel[bitmap_layout.b] << 16) |
58 (pixel[bitmap_layout.a] << 24);
59}
60
61/**
62 * Sanitise bitmap pixel component layout.
63 *
64 * Map endian-dependant layouts to byte-wise layout for the host.
65 *
66 * \param[in] layout Layout to convert.
67 * \return sanitised layout.
68 */
70 enum bitmap_layout layout)
71{
72 bool le = endian_host_is_le();
73
74 switch (layout) {
76 layout = (le) ? BITMAP_LAYOUT_A8B8G8R8
78 break;
80 layout = (le) ? BITMAP_LAYOUT_A8R8G8B8
82 break;
84 layout = (le) ? BITMAP_LAYOUT_B8G8R8A8
86 break;
88 layout = (le) ? BITMAP_LAYOUT_R8G8B8A8
90 break;
91 default:
92 break;
93 }
94
95 return layout;
96}
97
98/**
99 * Convert bitmap from one format to another.
100 *
101 * Note that both formats should be sanitised.
102 *
103 * \param[in] bitmap The bitmap to convert.
104 * \param[in] from The current bitmap format specifier.
105 * \param[in] to The bitmap format to convert to.
106 */
108 const bitmap_fmt_t *from,
109 const bitmap_fmt_t *to);
110
111/**
112 * Convert a bitmap to the client bitmap format.
113 *
114 * \param[in] bitmap The bitmap to convert.
115 * \param[in] current_fmt The current bitmap format specifier.
116 */
117static inline void bitmap_format_to_client(
118 void *bitmap,
119 const bitmap_fmt_t *current_fmt)
120{
121 bitmap_fmt_t from = *current_fmt;
122
123 from.layout = bitmap_sanitise_bitmap_layout(from.layout);
124 if (from.layout != bitmap_fmt.layout || from.pma != bitmap_fmt.pma) {
126 }
127}
128
129/**
130 * Convert a bitmap to the client bitmap format.
131 *
132 * \param[in] bitmap The bitmap to convert.
133 * \param[in] target_fmt The target bitmap format specifier.
134 */
135static inline void bitmap_format_from_client(
136 void *bitmap,
137 const bitmap_fmt_t *target_fmt)
138{
139 bitmap_fmt_t to = *target_fmt;
140
141 to.layout = bitmap_sanitise_bitmap_layout(to.layout);
142 if (to.layout != bitmap_fmt.layout || to.pma != bitmap_fmt.pma) {
144 }
145}
146
147#endif
static enum bitmap_layout bitmap_sanitise_bitmap_layout(enum bitmap_layout layout)
Sanitise bitmap pixel component layout.
Definition: bitmap.h:69
void bitmap_format_convert(void *bitmap, const bitmap_fmt_t *from, const bitmap_fmt_t *to)
Convert bitmap from one format to another.
Definition: bitmap.c:278
bitmap_fmt_t bitmap_fmt
The client bitmap format.
Definition: bitmap.c:34
static void bitmap_format_from_client(void *bitmap, const bitmap_fmt_t *target_fmt)
Convert a bitmap to the client bitmap format.
Definition: bitmap.h:135
static void bitmap_format_to_client(void *bitmap, const bitmap_fmt_t *current_fmt)
Convert a bitmap to the client bitmap format.
Definition: bitmap.h:117
static colour bitmap_pixel_to_colour(const uint8_t *pixel)
Convert a bitmap pixel to a NetSurf colour (0xAARRGGBB).
Definition: bitmap.h:53
Generic bitmap handling interface.
bitmap_layout
NetSurf bitmap pixel layout.
Definition: bitmap.h:48
@ BITMAP_LAYOUT_ABGR8888
32-bit BGRA (0xAABBGGRR).
Definition: bitmap.h:91
@ BITMAP_LAYOUT_BGRA8888
32-bit BGRA (0xBBGGRRAA).
Definition: bitmap.h:75
@ BITMAP_LAYOUT_ARGB8888
32-bit ARGB (0xAARRGGBB).
Definition: bitmap.h:83
@ BITMAP_LAYOUT_B8G8R8A8
Bite-wise BGRA: Byte order: 0xBB, 0xGG, 0xRR, 0xAA.
Definition: bitmap.h:53
@ BITMAP_LAYOUT_R8G8B8A8
Bite-wise RGBA: Byte order: 0xRR, 0xGG, 0xBB, 0xAA.
Definition: bitmap.h:50
@ BITMAP_LAYOUT_A8B8G8R8
Bite-wise ABGR: Byte order: 0xAA, 0xBB, 0xGG, 0xRR.
Definition: bitmap.h:59
@ BITMAP_LAYOUT_A8R8G8B8
Bite-wise ARGB: Byte order: 0xAA, 0xRR, 0xGG, 0xBB.
Definition: bitmap.h:56
@ BITMAP_LAYOUT_RGBA8888
32-bit RGBA (0xRRGGBBAA).
Definition: bitmap.h:67
Pixel format: colour component order.
Definition: bitmap.h:32
uint8_t g
Byte offset within pixel to green component.
Definition: bitmap.h:34
uint8_t b
Byte offset within pixel to blue component.
Definition: bitmap.h:35
uint8_t a
Byte offset within pixel to alpha component.
Definition: bitmap.h:36
uint8_t r
Byte offset within pixel to red component.
Definition: bitmap.h:33
Bitmap format specifier.
Definition: bitmap.h:95
bool pma
Premultiplied alpha.
Definition: bitmap.h:97
enum bitmap_layout layout
Colour component layout.
Definition: bitmap.h:96
RISC OS wimp toolkit bitmap.
Definition: bitmap.c:68
NetSurf types.
uint32_t colour
Colour type: XBGR.
Definition: types.h:35
char from[32]
Encoding name to convert from.
Definition: utf8.c:143
char to[32]
Encoding name to convert to.
Definition: utf8.c:144