summaryrefslogtreecommitdiff
path: root/desktop/bitmap.h
blob: af7e9040ffb3d5f07b1010d7d9907e6febb48392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright 2022 Michael Drake <tlsa@nesturf-browser.org>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

/** \file
 * Internal core bitmap interface.
 */

#ifndef _NETSURF_DESKTOP_BITMAP_H_
#define _NETSURF_DESKTOP_BITMAP_H_

#include <nsutils/endian.h>

#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;
}

/**
 * Convert bitmap from one format to another.
 *
 * Note that both formats should be sanitised.
 *
 * \param[in]  bitmap  The bitmap to convert.
 * \param[in]  from    The current bitmap format specifier.
 * \param[in]  to      The bitmap format to convert to.
 */
void bitmap_format_convert(void *bitmap,
		const bitmap_fmt_t *from,
		const bitmap_fmt_t *to);

/**
 * Convert a bitmap to the client bitmap format.
 *
 * \param[in]  bitmap       The bitmap to convert.
 * \param[in]  current_fmt  The current bitmap format specifier.
 */
static inline void bitmap_format_to_client(
		void *bitmap,
		const bitmap_fmt_t *current_fmt)
{
	bitmap_fmt_t from = *current_fmt;

	from.layout = bitmap_sanitise_bitmap_layout(from.layout);
	if (from.layout != bitmap_fmt.layout) {
		bitmap_format_convert(bitmap, &from, &bitmap_fmt);
	}
}

/**
 * Convert a bitmap to the client bitmap format.
 *
 * \param[in]  bitmap      The bitmap to convert.
 * \param[in]  target_fmt  The target bitmap format specifier.
 */
static inline void bitmap_format_from_client(
		void *bitmap,
		const bitmap_fmt_t *target_fmt)
{
	bitmap_fmt_t to = *target_fmt;

	to.layout = bitmap_sanitise_bitmap_layout(to.layout);
	if (to.layout != bitmap_fmt.layout) {
		bitmap_format_convert(bitmap, &bitmap_fmt, &to);
	}
}

#endif