summaryrefslogtreecommitdiff
path: root/riscos/bitmap.c
blob: 1fad86ff9d6ea622906a705fb969600b7cad7a96 (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
174
175
176
177
/*

 * This file is part of NetSurf, http://netsurf.sourceforge.net/

 * Licensed under the GNU General Public License,

 *                http://www.opensource.org/licenses/gpl-license

 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>

 */

/** \file

 * Generic bitmap handling (RISC OS implementation).

 *

 * This implements the interface given by desktop/bitmap.h using RISC OS

 * sprites.

 */

#include <assert.h>

#include <stdbool.h>

#include <string.h>

#include "oslib/osspriteop.h"

#include "netsurf/content/content.h"

#include "netsurf/image/bitmap.h"

#include "netsurf/riscos/bitmap.h"

#include "netsurf/riscos/image.h"

#include "netsurf/utils/log.h"

#include "netsurf/utils/utils.h"



/**

 * Create a bitmap.

 *

 * \param  width   width of image in pixels

 * \param  height  width of image in pixels

 * \return an opaque struct bitmap, or NULL on memory exhaustion

 */

struct bitmap *bitmap_create(int width, int height)
{
	unsigned int area_size;
	struct bitmap *bitmap;
	osspriteop_area *sprite_area;
	osspriteop_header *sprite;

	if (width == 0 || height == 0)
		return NULL;

	area_size = 16 + 44 + width * height * 4;
	bitmap = calloc(sizeof(struct bitmap) + area_size, 1);
	if (!bitmap)
		return NULL;

	bitmap->width = width;
	bitmap->height = height;
	bitmap->opaque = false;

	/* area control block */
	sprite_area = &bitmap->sprite_area;
	sprite_area->size = area_size;
	sprite_area->sprite_count = 1;
	sprite_area->first = 16;
	sprite_area->used = area_size;

	/* sprite control block */
	sprite = (osspriteop_header *) (sprite_area + 1);
	sprite->size = area_size - 16;
/*	memset(sprite->name, 0x00, 12); */
	strncpy(sprite->name, "bitmap", 12);
	sprite->width = width - 1;
	sprite->height = height - 1;
	sprite->left_bit = 0;
	sprite->right_bit = 31;
	sprite->image = sprite->mask = 44;
	sprite->mode = (os_mode) 0x301680b5;

	return bitmap;
}


/**

 * Sets whether a bitmap should be plotted opaque

 *

 * \param  bitmap  a bitmap, as returned by bitmap_create()

 * \param  opaque  whether the bitmap should be plotted opaque

 */
void bitmap_set_opaque(struct bitmap *bitmap, bool opaque)
{
	assert(bitmap);
	bitmap->opaque = opaque;
}


/**

 * Tests whether a bitmap has an opaque alpha channel

 *

 * \param  bitmap  a bitmap, as returned by bitmap_create()

 * \return whether the bitmap is opaque

 */
bool bitmap_test_opaque(struct bitmap *bitmap)
{
	assert(bitmap);
	char *sprite = bitmap_get_buffer(bitmap);
	unsigned int width = bitmap_get_rowstride(bitmap);
	osspriteop_header *sprite_header =
		(osspriteop_header *) (&(bitmap->sprite_area) + 1);
	unsigned int height = (sprite_header->height + 1);
	unsigned int size = width * height;
	for (unsigned int i = 3; i < size; i += 4)
		if (sprite[i] != 0xff)
			return false;
	return true;
}

/**

 * Return a pointer to the pixel data in a bitmap.

 *

 * \param  bitmap  a bitmap, as returned by bitmap_create()

 * \return pointer to the pixel buffer

 *

 * The pixel data is packed as BITMAP_FORMAT, possibly with padding at the end

 * of rows. The width of a row in bytes is given by bitmap_get_rowstride().

 */

char *bitmap_get_buffer(struct bitmap *bitmap)
{
	assert(bitmap);
	return ((char *) (&(bitmap->sprite_area))) + 16 + 44;
}


/**

 * Find the width of a pixel row in bytes.

 *

 * \param  bitmap  a bitmap, as returned by bitmap_create()

 * \return width of a pixel row in the bitmap

 */

size_t bitmap_get_rowstride(struct bitmap *bitmap)
{
	osspriteop_header *sprite;
	assert(bitmap);
	sprite = (osspriteop_header *) (&(bitmap->sprite_area) + 1);
	return (sprite->width + 1) * 4;
}


/**

 * Free a bitmap.

 *

 * \param  bitmap  a bitmap, as returned by bitmap_create()

 */

void bitmap_destroy(struct bitmap *bitmap)
{
	assert(bitmap);
	free(bitmap);
}


/**

 * Save a bitmap in the platform's native format.

 *

 * \param  bitmap  a bitmap, as returned by bitmap_create()

 * \param  path    pathname for file

 * \return true on success, false on error and error reported

 */

bool bitmap_save(struct bitmap *bitmap, const char *path)
{
	os_error *error;
	error = xosspriteop_save_sprite_file(osspriteop_USER_AREA,
					     &(bitmap->sprite_area), path);
	if (error) {
		LOG(("xosspriteop_save_sprite_file: 0x%x: %s",
				error->errnum, error->errmess));
		warn_user("SaveError", error->errmess);
		return false;
	}
	return true;
}