aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnki_jbig.c
blob: acc43eb4880d853230ad403d5e0e0d8988e47a44 (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
37
38
39
40
41
42
43
/*
 * Copyright (c) 2020-2022, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

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

#include "cnki_jbig.h"
#include "cnki_jbig_dec.h"

int
cnki_jbig(char **bitmap, int *bitmap_size,
	int *bitmap_width, int *bitmap_height,
	const char * restrict jbig, int jbig_size)
{
	dib_t *dib = malloc(sizeof(dib_t));

	if (dib == NULL)
		return 1;

	memcpy(dib, jbig, 40);

	int width_padded = (dib->width * dib->depth + 7) / 8;

	*bitmap_size = dib->height * width_padded;
	*bitmap = malloc(*bitmap_size);

	if (*bitmap == NULL) {
		free(dib);
		return 1;
	}

	strdec_jbig(bitmap, dib->width, dib->height, jbig + 48, jbig_size - 48);

	*bitmap_width = dib->width;
	*bitmap_height = dib->height;

	free(dib);

	return 0;
}