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

#include "lodepng.h"
#include "png.h"

unsigned lodepng_custom_zlib_decompress(unsigned char** out, size_t* outsize,
					const unsigned char* in, size_t insize,
					const LodePNGDecompressSettings* settings)
{
	int err;

	png_stream.next_in = in;
	png_stream.avail_in = insize;

	png_stream.next_out = *out;
	png_stream.avail_out = *outsize;

	err = zlib_inflateReset(&png_stream);
	if (err != Z_OK) {
		printk("zlib_inflateReset error %d\n", err);
		zlib_inflateEnd(&png_stream);
		zlib_inflateInit(&png_stream);
	}

	err = zlib_inflate(&png_stream, Z_FINISH);
	if (err != Z_STREAM_END)
		goto err;
	return 0;

err:
	printk("Error %d while decompressing!\n", err);
	printk("%p(%zd)->%p(%zd)\n", in, insize, *out, *outsize);
	return -EIO;
}

struct image *png_open(char *inbuf, int insize)
{
	LodePNGState state;
	int ret;
	unsigned error;
	struct image *img;
	unsigned char *png = NULL;

	ret = png_uncompress_init();
	if (ret)
		return ERR_PTR(ret);

	img = calloc(1, sizeof(struct image));
	if (!img)
		return ERR_PTR(-ENOMEM);

	lodepng_state_init(&state);

	state.info_raw.colortype = LCT_RGBA;
	state.info_raw.bitdepth = 8;

	error = lodepng_decode(&png, &img->width, &img->height, &state, inbuf, insize);

	if(error) {
		printf("error %u: %s\n", error, lodepng_error_text(error));
		ret = -EINVAL;
		goto err;
	}

	img->bits_per_pixel = 4 << 3;
	img->data = png;

	pr_debug("png: %d x %d data@0x%p\n", img->width, img->height, img->data);

	lodepng_state_cleanup(&state);

	return img;
err:
	free(png);
	free(img);
	return ERR_PTR(ret);
}

void png_close(struct image *img)
{
	free(img->data);
	png_uncompress_exit();
}