NetSurf
bitmap.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
3 * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net>
4 *
5 * This file is part of NetSurf, http://www.netsurf-browser.org/
6 *
7 * NetSurf is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * NetSurf is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * \file
22 * win32 implementation of the bitmap operations.
23 */
24
25#include "utils/config.h"
26
27#include <inttypes.h>
28#include <sys/types.h>
29#include <string.h>
30#include <windows.h>
31
32#include "utils/log.h"
33#include "netsurf/bitmap.h"
34#include "netsurf/plotters.h"
35#include "netsurf/content.h"
36
37#include "windows/plot.h"
38#include "windows/bitmap.h"
39
40/**
41 * Create a bitmap.
42 *
43 * \param width width of image in pixels
44 * \param height height of image in pixels
45 * \param state flags flags for bitmap creation
46 * \return an opaque struct bitmap, or NULL on memory exhaustion
47 */
48static void *win32_bitmap_create(int width, int height, enum gui_bitmap_flags flags)
49{
50 struct bitmap *bitmap;
51 BITMAPV5HEADER *pbmi;
52 HBITMAP windib;
53 uint8_t *pixdata;
54
55 NSLOG(netsurf, INFO, "width %d, height %d, flags %u", width, height,
56 (unsigned)flags);
57
58 pbmi = calloc(1, sizeof(BITMAPV5HEADER));
59 if (pbmi == NULL) {
60 return NULL;
61 }
62
63 pbmi->bV5Size = sizeof(BITMAPV5HEADER);
64 pbmi->bV5Width = width;
65 pbmi->bV5Height = -height;
66 pbmi->bV5Planes = 1;
67 pbmi->bV5BitCount = 32;
68 pbmi->bV5Compression = BI_BITFIELDS;
69
70 pbmi->bV5RedMask = 0xff; /* red mask */
71 pbmi->bV5GreenMask = 0xff00; /* green mask */
72 pbmi->bV5BlueMask = 0xff0000; /* blue mask */
73 pbmi->bV5AlphaMask = 0xff000000; /* alpha mask */
74
75 windib = CreateDIBSection(NULL, (BITMAPINFO *)pbmi, DIB_RGB_COLORS, (void **)&pixdata, NULL, 0);
76
77 if (windib == NULL) {
78 free(pbmi);
79 return NULL;
80 }
81
82 bitmap = calloc(1 , sizeof(struct bitmap));
83 if (bitmap == NULL) {
84 DeleteObject(windib);
85 free(pbmi);
86 return NULL;
87 }
88
92 bitmap->pbmi = pbmi;
94 if ((flags & BITMAP_OPAQUE) != 0) {
95 bitmap->opaque = true;
96 } else {
97 bitmap->opaque = false;
98 }
99
100 NSLOG(netsurf, INFO, "bitmap %p", bitmap);
101
102 return bitmap;
103}
104
105
106/**
107 * Return a pointer to the pixel data in a bitmap.
108 *
109 * The pixel data is packed as BITMAP_FORMAT, possibly with padding at the end
110 * of rows. The width of a row in bytes is given by bitmap_get_rowstride().
111 *
112 * \param bitmap a bitmap, as returned by bitmap_create()
113 * \return pointer to the pixel buffer
114 */
115static unsigned char *bitmap_get_buffer(void *bitmap)
116{
117 struct bitmap *bm = bitmap;
118 if (bitmap == NULL) {
119 NSLOG(netsurf, INFO, "NULL bitmap!");
120 return NULL;
121 }
122
123 return bm->pixdata;
124}
125
126
127/**
128 * Find the width of a pixel row in bytes.
129 *
130 * \param bitmap a bitmap, as returned by bitmap_create()
131 * \return width of a pixel row in the bitmap
132 */
133static size_t bitmap_get_rowstride(void *bitmap)
134{
135 struct bitmap *bm = bitmap;
136
137 if (bitmap == NULL) {
138 NSLOG(netsurf, INFO, "NULL bitmap!");
139 return 0;
140 }
141
142 return (bm->width) * 4;
143}
144
145
146/**
147 * Free a bitmap.
148 *
149 * \param bitmap a bitmap, as returned by bitmap_create()
150 */
152{
153 struct bitmap *bm = bitmap;
154
155 if (bitmap == NULL) {
156 NSLOG(netsurf, INFO, "NULL bitmap!");
157 return;
158 }
159
160 DeleteObject(bm->windib);
161 free(bm->pbmi);
162 free(bm);
163}
164
165
166/**
167 * The bitmap image has changed, so flush any persistant cache.
168 *
169 * \param bitmap a bitmap, as returned by bitmap_create()
170 */
171static void bitmap_modified(void *bitmap) {
172}
173
174/**
175 * Sets whether a bitmap should be plotted opaque
176 *
177 * \param bitmap a bitmap, as returned by bitmap_create()
178 * \param opaque whether the bitmap should be plotted opaque
179 */
180static void bitmap_set_opaque(void *bitmap, bool opaque)
181{
182 struct bitmap *bm = bitmap;
183
184 if (bitmap == NULL) {
185 NSLOG(netsurf, INFO, "NULL bitmap!");
186 return;
187 }
188
189 NSLOG(netsurf, INFO, "setting bitmap %p to %s", bm,
190 opaque ? "opaque" : "transparent");
191 bm->opaque = opaque;
192}
193
194
195/**
196 * Gets whether a bitmap should be plotted opaque
197 *
198 * \param bitmap a bitmap, as returned by bitmap_create()
199 */
200static bool bitmap_get_opaque(void *bitmap)
201{
202 struct bitmap *bm = bitmap;
203
204 if (bitmap == NULL) {
205 NSLOG(netsurf, INFO, "NULL bitmap!");
206 return false;
207 }
208
209 return bm->opaque;
210}
211
212static int bitmap_get_width(void *bitmap)
213{
214 struct bitmap *bm = bitmap;
215
216 if (bitmap == NULL) {
217 NSLOG(netsurf, INFO, "NULL bitmap!");
218 return 0;
219 }
220
221 return(bm->width);
222}
223
224static int bitmap_get_height(void *bitmap)
225{
226 struct bitmap *bm = bitmap;
227
228 if (bitmap == NULL) {
229 NSLOG(netsurf, INFO, "NULL bitmap!");
230 return 0;
231 }
232
233 return(bm->height);
234}
235
236struct bitmap *bitmap_scale(struct bitmap *prescale, int width, int height)
237{
238 struct bitmap *ret = malloc(sizeof(struct bitmap));
239 int i, ii, v, vv;
240 uint32_t *retpixdata, *inpixdata; /* 4 byte types for quicker
241 * transfer */
242 if (ret == NULL)
243 return NULL;
244
245 retpixdata = malloc(width * height * 4);
246 if (retpixdata == NULL) {
247 free(ret);
248 return NULL;
249 }
250
251 inpixdata = (uint32_t *)prescale->pixdata;
252 ret->pixdata = (uint8_t *)retpixdata;
253 ret->height = height;
254 ret->width = width;
255 for (i = 0; i < height; i++) {
256 v = i * width;
257 vv = (int)((i * prescale->height) / height) * prescale->width;
258 for (ii = 0; ii < width; ii++) {
259 retpixdata[v + ii] = inpixdata[vv + (int)
260 ((ii * prescale->width) / width)];
261 }
262 }
263 return ret;
264
265}
266
267
268static nserror
270{
271 int width;
272 int height;
273 HDC hdc, bufferdc, minidc;
274 struct bitmap *fsbitmap;
275 struct redraw_context ctx = {
276 .interactive = false,
277 .background_images = true,
278 .plot = &win_plotters
279 };
280
282 height = ((width * bitmap->height) + (bitmap->width / 2)) /
283 bitmap->width;
284
285 NSLOG(netsurf, INFO, "bitmap %p for content %p width %d, height %d",
287
288 /* create two memory device contexts to put the bitmaps in */
289 bufferdc = CreateCompatibleDC(NULL);
290 if ((bufferdc == NULL)) {
291 return NSERROR_NOMEM;
292 }
293
294 minidc = CreateCompatibleDC(NULL);
295 if ((minidc == NULL)) {
296 DeleteDC(bufferdc);
297 return NSERROR_NOMEM;
298 }
299
300 /* create a full size bitmap and plot into it */
302
303 SelectObject(bufferdc, fsbitmap->windib);
304
305 hdc = plot_hdc;
306 plot_hdc = bufferdc;
307 /* render the content */
309 plot_hdc = hdc;
310
311 /* scale bitmap bufferbm into minibm */
312 SelectObject(minidc, bitmap->windib);
313
314 bitmap->opaque = true;
315
316 StretchBlt(minidc, 0, 0, bitmap->width, bitmap->height, bufferdc, 0, 0, width, height, SRCCOPY);
317
318 DeleteDC(bufferdc);
319 DeleteDC(minidc);
320 win32_bitmap_destroy(fsbitmap);
321
322 return NSERROR_OK;
323}
324
327 .destroy = win32_bitmap_destroy,
328 .set_opaque = bitmap_set_opaque,
329 .get_opaque = bitmap_get_opaque,
330 .get_buffer = bitmap_get_buffer,
331 .get_rowstride = bitmap_get_rowstride,
332 .get_width = bitmap_get_width,
333 .get_height = bitmap_get_height,
334 .modified = bitmap_modified,
335 .render = bitmap_render,
336};
337
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
int bitmap_get_width(void *bitmap)
get width of a bitmap.
Definition: bitmap.c:319
int bitmap_get_height(void *bitmap)
get height of a bitmap.
Definition: bitmap.c:336
static struct gui_bitmap_table bitmap_table
Definition: bitmap.c:325
struct gui_bitmap_table * win32_bitmap_table
Definition: bitmap.c:338
static void bitmap_modified(void *bitmap)
The bitmap image has changed, so flush any persistant cache.
Definition: bitmap.c:171
static size_t bitmap_get_rowstride(void *bitmap)
Find the width of a pixel row in bytes.
Definition: bitmap.c:133
void win32_bitmap_destroy(void *bitmap)
Free a bitmap.
Definition: bitmap.c:151
static nserror bitmap_render(struct bitmap *bitmap, struct hlcache_handle *content)
Definition: bitmap.c:269
static unsigned char * bitmap_get_buffer(void *bitmap)
Return a pointer to the pixel data in a bitmap.
Definition: bitmap.c:115
struct bitmap * bitmap_scale(struct bitmap *prescale, int width, int height)
Definition: bitmap.c:236
static void bitmap_set_opaque(void *bitmap, bool opaque)
Sets whether a bitmap should be plotted opaque.
Definition: bitmap.c:180
static void * win32_bitmap_create(int width, int height, enum gui_bitmap_flags flags)
Create a bitmap.
Definition: bitmap.c:48
static bool bitmap_get_opaque(void *bitmap)
Gets whether a bitmap should be plotted opaque.
Definition: bitmap.c:200
Generic bitmap handling interface.
gui_bitmap_flags
Bitmap creation flags.
Definition: bitmap.h:36
@ BITMAP_CLEAR
memory should be wiped to 0
Definition: bitmap.h:39
@ BITMAP_OPAQUE
image is opaque
Definition: bitmap.h:38
Public content interface.
int content_get_width(struct hlcache_handle *h)
Retrieve width of content.
Definition: content.c:1158
bool content_scaled_redraw(struct hlcache_handle *h, int width, int height, const struct redraw_context *ctx)
Redraw a content with scale set for horizontal fit.
Definition: content.c:583
Target independent plotting interface.
Netsurf additional integer type formatting macros.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
int width
Definition: gui.c:160
int height
Definition: gui.c:161
Interface to utility string handling.
RISC OS wimp toolkit bitmap.
Definition: bitmap.c:68
int width
width of bitmap
Definition: bitmap.c:69
BITMAPV5HEADER * pbmi
Definition: bitmap.h:27
int height
height of bitmap
Definition: bitmap.c:70
bool opaque
Whether the bitmap is opaque.
Definition: bitmap.c:74
UBYTE * pixdata
Definition: bitmap.c:71
HBITMAP windib
Definition: bitmap.h:26
Content which corresponds to a single URL.
Bitmap operations.
Definition: bitmap.h:125
void *(* create)(int width, int height, enum gui_bitmap_flags flags)
Create a new bitmap.
Definition: bitmap.h:136
High-level cache handle.
Definition: hlcache.c:66
Redraw context.
Definition: plotters.h:51
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59
#define min(x, y)
Definition: utils.h:46
#define max(x, y)
Definition: utils.h:50
static nserror bitmap(const struct redraw_context *ctx, struct bitmap *bitmap, int x, int y, int width, int height, colour bg, bitmap_flags_t flags)
Plot a bitmap.
Definition: plot.c:857
HDC plot_hdc
Definition: plot.c:44
const struct plotter_table win_plotters
win32 API plot operation table
Definition: plot.c:1040