aboutsummaryrefslogtreecommitdiffstats
path: root/rd/read-webp.c
blob: 0dfe3723b38af72b30797afe8fc4fb299adb8d79 (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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <webp/decode.h>

#include "readers.h"


struct webp_state {
	FILE *f;
	int width, height;
	uint8_t *data;
};

static void *
webp_init(FILE *fp, char *filename, unsigned int page,
          struct ida_image_info *i, int thumbnail)
{
	uint32_t img_size;
	void *img;
	struct webp_state *h;
	h = malloc(sizeof(*h));

	h->f = fp;
	if(fseek(fp, 0, SEEK_END) != 0)
	{
		free(h);
		return NULL;
	}
	img_size = ftell(fp);
	img = malloc(img_size);

	if(fseek(fp, 0, SEEK_SET) != 0)
	{
		free(img);
		free(h);
		return NULL;
	}
	if(fread(img, img_size, 1, fp) != 1)
	{
		free(img);
		free(h);
		return NULL;
	}

	h->data = WebPDecodeRGBA(img, img_size, &h->width, &h->height);

	i->width = h->width;
	i->height = h->height;
	i->dpi = 100;
	i->npages = 1;

	free(img);

	return h;
}


static void
webp_read(unsigned char *dst, unsigned int line, void *data)
{
	struct webp_state *h = data;

	load_rgba(dst, h->data + line * 4 * h->width, h->width);
}


static void
webp_done(void *data)
{
	struct webp_state *h = data;

	free(h->data);
	fclose(h->f);
	free(h);
}


static struct ida_loader webp_loader = {
    magic: "WEBPVP8",
    moff:  8,
    mlen:  7,
    name:  "libwebp",
    init:  webp_init,
    read:  webp_read,
    done:  webp_done,
};


static void __init init_rd(void)
{
    load_register(&webp_loader);
}