aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnki_jbig.c
blob: 02040bea6ea2632837791e3d50fd96d0ef084e4b (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
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
/*
 * Copyright (c) 2020-2021, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

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

#include "cnki_jbig.h"
#include "jbig.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);

	bih_t *bih = malloc(sizeof(bih_t));

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

	memset(bih, 0, sizeof(bih_t));

	bih->d_l = 0;
	bih->d = 0;

	bih->p = 1;

	bih->fill = 0;

	bih->x_d = dib->width;
	bih->y_d = dib->height;
	bih->l_0 = bih->y_d / 35;

	while (bih->l_0 > 128)
		bih->l_0--;
	if (bih->l_0 < 2)
		bih->l_0 = 2;

	bih->m_x = 8;
	bih->m_y = 0;

	bih->order |= 1 << 1;
	bih->order |= 1 << 0;

	bih->options |= 1 << 4;
	bih->options |= 1 << 3;
	bih->options |= 1 << 2;

	bih->dptable = NULL;

	int bie_size = jbig_size - 28; /* - 40 - 8 + 20 */
	char *bie = malloc(bie_size);

	if (bie == NULL) {
		free(dib);
		free(bih);
		return 1;
	}

	memcpy(bie, bih, 20);
	memcpy(bie + 20, jbig + 48, jbig_size - 48);

	int ret = strdec_jbig(bitmap, bitmap_size, bie, bie_size);

	if (ret == 0) {
		*bitmap_width = bih->x_d;
		*bitmap_height = bih->y_d;
	}

	free(dib);
	free(bih);
	free(bie);

	if (ret != 0)
		return 1;

	return 0;
}