aboutsummaryrefslogtreecommitdiffstats
path: root/src/jbig2.c
blob: ea9233c125d3306f3037258781093ba2c5fade5b (plain) (blame)
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
/*
 * Copyright (c) 2022-2023, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <jbig2.h>

int
strdec_jbig2(char **bitmap,
	const char * restrict jbig2, int jbig2_size)
{
	Jbig2Ctx *ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, NULL, NULL);

	jbig2_data_in(ctx, (unsigned char *) jbig2, jbig2_size);

	jbig2_complete_page(ctx);

	Jbig2Image *image = jbig2_page_out(ctx);

	int width_padded = (image->width + 7) / 8;
	unsigned char *data = image->data;

	for (unsigned int i = 0; i < image->height; i++) {
		memcpy(*bitmap + i * width_padded, data, width_padded);
		data += image->stride;
	}

	jbig2_release_page(ctx, image);
	jbig2_ctx_free(ctx);
	return 0;
}