summaryrefslogtreecommitdiffstats
path: root/lib/png.c
blob: f71fc6abc8b451aeec7ea95c39687feda7502737 (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
#include <common.h>
#include <errno.h>
#include <malloc.h>
#include <fb.h>
#include <asm/byteorder.h>
#include <init.h>
#include <image_renderer.h>
#include <graphic_utils.h>
#include <linux/zlib.h>

#include "png.h"

z_stream png_stream;
static int initialized;

int png_uncompress_init(void)
{
	if (!initialized++) {
		png_stream.workspace = malloc(zlib_inflate_workspacesize());
		if (!png_stream.workspace) {
			initialized = 0;
			return -ENOMEM;
		}
		png_stream.next_in = NULL;
		png_stream.avail_in = 0;
		zlib_inflateInit(&png_stream);
	}
	return 0;
}

void png_uncompress_exit(void)
{
	if (!--initialized) {
		zlib_inflateEnd(&png_stream);
		vfree(png_stream.workspace);
	}
}

static int png_renderer(struct fb_info *info, struct image *img, void* fb,
		int startx, int starty, void* offscreenbuf)
{
	int width, height;
	void *buf;
	int xres, yres;

	xres = info->xres;
	yres = info->yres;

	if (startx < 0) {
		startx = (xres - img->width) / 2;
		if (startx < 0)
			startx = 0;
	}

	if (starty < 0) {
		starty = (yres - img->height) / 2;
		if (starty < 0)
			starty = 0;
	}

	width = min(img->width, xres - startx);
	height = min(img->height, yres - starty);

	buf = offscreenbuf ? offscreenbuf : fb;

	rgba_blend(info, img->data, buf, height, width, startx, starty, true);

	if (offscreenbuf) {
		int fbsize;

		fbsize = xres * yres * (info->bits_per_pixel >> 3);
		memcpy(fb, offscreenbuf, fbsize);
	}

	return img->height;
}

static struct image_renderer png = {
	.type = filetype_png,
	.open = png_open,
	.close = png_close,
	.renderer = png_renderer,
};

static int png_init(void)
{
	return image_renderer_register(&png);
}
fs_initcall(png_init);