summaryrefslogtreecommitdiff
path: root/riscos/png.c
blob: 73d581dc5b3fb9b6769d53dc65321ef0fb74fe91 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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>
 * Copyright 2004 Richard Wilson <not_ginger_matt@hotmail.com>
 */

#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <swis.h>
#include "libpng/png.h"
#include "oslib/osspriteop.h"
#include "netsurf/utils/config.h"
#include "netsurf/content/content.h"
#include "netsurf/riscos/options.h"
#include "netsurf/riscos/png.h"
#include "netsurf/riscos/tinct.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/utils.h"

#ifdef WITH_PNG
/* libpng uses names starting png_, so use nspng_ here to avoid clashes */

static void info_callback(png_structp png, png_infop info);
static void row_callback(png_structp png, png_bytep new_row,
		png_uint_32 row_num, int pass);
static void end_callback(png_structp png, png_infop info);


void nspng_init(void)
{
}


void nspng_create(struct content *c, const char *params[])
{
	c->data.png.sprite_area = 0;
	c->data.png.png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
			0, 0, 0);
	assert(c->data.png.png != 0);
	c->data.png.info = png_create_info_struct(c->data.png.png);
	assert(c->data.png.info != 0);

	if (setjmp(png_jmpbuf(c->data.png.png))) {
		png_destroy_read_struct(&c->data.png.png,
				&c->data.png.info, 0);
		assert(0);
	}

	png_set_progressive_read_fn(c->data.png.png, c,
			info_callback, row_callback, end_callback);
}


void nspng_process_data(struct content *c, char *data, unsigned long size)
{
	if (setjmp(png_jmpbuf(c->data.png.png))) {
		png_destroy_read_struct(&c->data.png.png,
				&c->data.png.info, 0);
		assert(0);
	}

	LOG(("data %p, size %li", data, size));
	png_process_data(c->data.png.png, c->data.png.info,
			data, size);

	c->size += size;
}


/**
 * info_callback -- PNG header has been completely received, prepare to process
 * image data
 */

void info_callback(png_structp png, png_infop info)
{
	int bit_depth, color_type, interlace;
	unsigned int rowbytes, sprite_size;
	unsigned long width, height;
	struct content *c = png_get_progressive_ptr(png);
	osspriteop_area *sprite_area;
	osspriteop_header *sprite;

	/*	Read the PNG details
	*/
	png_get_IHDR(png, info, &width, &height, &bit_depth,
			&color_type, &interlace, 0, 0);

	/*	Claim the required memory for the converted PNG
	*/
	sprite_size = sizeof(*sprite_area) + sizeof(*sprite) + (height * width * 4);
	sprite_area = xcalloc(sprite_size, 1);

	/*	Fill in the sprite area header information
	*/
	sprite_area->size = sprite_size;
	sprite_area->sprite_count = 1;
	sprite_area->first = sizeof(*sprite_area);
	sprite_area->used = sprite_size;

	/*	Fill in the sprite header information
	*/
	sprite = (osspriteop_header *) (sprite_area + 1);
	sprite->size = sprite_size - sizeof(*sprite_area);
	strcpy(sprite->name, "png");
	sprite->width = width - 1;
	sprite->height = height - 1;
	sprite->left_bit = 0;
	sprite->right_bit = 31;
	sprite->mask = sprite->image = sizeof(*sprite);
	sprite->mode = (os_mode) 0x301680b5;

	/*	Store the sprite area
	*/
	c->data.png.sprite_area = sprite_area;

	/*	Set up our transformations
	*/
	if (color_type == PNG_COLOR_TYPE_PALETTE)
		png_set_palette_to_rgb(png);
	if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
		png_set_gray_1_2_4_to_8(png);
	if (png_get_valid(png, info, PNG_INFO_tRNS))
		png_set_tRNS_to_alpha(png);
	if (bit_depth == 16)
		png_set_strip_16(png);
	if (color_type == PNG_COLOR_TYPE_GRAY ||
			color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
		png_set_gray_to_rgb(png);
	if (!(color_type & PNG_COLOR_MASK_ALPHA))
		png_set_filler(png, 0xff, PNG_FILLER_AFTER);

	png_read_update_info(png, info);

	c->data.png.rowbytes = rowbytes = png_get_rowbytes(png, info);
	c->data.png.interlace = (interlace == PNG_INTERLACE_ADAM7);
	c->data.png.sprite_image = ((char *) sprite) + sprite->image;
	c->width = width;
	c->height = height;

	LOG(("size %li * %li, bpp %i, rowbytes %u", width,
				height, bit_depth, rowbytes));
}


static unsigned int interlace_start[8] = {0, 16, 0, 8, 0, 4, 0};
static unsigned int interlace_step[8] = {28, 28, 12, 12, 4, 4, 0};
static unsigned int interlace_row_start[8] = {0, 0, 4, 0, 2, 0, 1};
static unsigned int interlace_row_step[8] = {8, 8, 8, 4, 4, 2, 2};

void row_callback(png_structp png, png_bytep new_row,
		png_uint_32 row_num, int pass)
{
	struct content *c = png_get_progressive_ptr(png);
	unsigned long i, j, rowbytes = c->data.png.rowbytes;
	unsigned int start, step;
	char *row = c->data.png.sprite_image + row_num * (c->width * 4);

	/*	Abort if we've not got any data
	*/
	if (new_row == 0)
		return;

	/*	Handle interlaced sprites using the Adam7 algorithm
	*/
	if (c->data.png.interlace) {
		start = interlace_start[pass];
 		step = interlace_step[pass];
		row_num = interlace_row_start[pass] +
				interlace_row_step[pass] * row_num;

		/*	Copy the data to our current row taking into consideration interlacing
		*/
		row = c->data.png.sprite_image + row_num * (c->width * 4);
		for (j = 0, i = start; i < rowbytes; i += step) {
			row[i++] = new_row[j++];
			row[i++] = new_row[j++];
			row[i++] = new_row[j++];
			row[i++] = new_row[j++];
		}
	} else {
		/*	Do a fast memcpy of the row data
		*/
		memcpy(row, new_row, rowbytes);
	}
}


void end_callback(png_structp png, png_infop info)
{
	/*struct content *c = png_get_progressive_ptr(png);*/

	LOG(("PNG end"));

	/*xosspriteop_save_sprite_file(osspriteop_USER_AREA, c->data.png.sprite_area,
			"png");*/
}



int nspng_convert(struct content *c, unsigned int width, unsigned int height)
{
	png_destroy_read_struct(&c->data.png.png, &c->data.png.info, 0);

	c->title = xcalloc(100, 1);
	sprintf(c->title, messages_get("PNGTitle"), c->width, c->height);
	c->status = CONTENT_STATUS_DONE;
	return 0;
}


void nspng_destroy(struct content *c)
{
	xfree(c->title);
	xfree(c->data.png.sprite_area);
}


void nspng_redraw(struct content *c, long x, long y,
		unsigned long width, unsigned long height,
		long clip_x0, long clip_y0, long clip_x1, long clip_y1,
		float scale)
{

	/*	Tinct currently only handles 32bpp sprites that have an embedded alpha mask. Any
		sprites not matching the required specifications are ignored. See the Tinct
		documentation for further information.
	*/
	_swix(Tinct_PlotScaledAlpha, _IN(2) | _IN(3) | _IN(4) | _IN(5) | _IN(6) | _IN(7),
			((char *) c->data.png.sprite_area + c->data.png.sprite_area->first),
			x, (int)(y - height),
			width / 2, height / 2,	// [hack until Tinct is fixed] - rjw
			(option_filter_sprites?0:(1<<1)) | (option_dither_sprites?0:(1<<2)));
}
#endif