summaryrefslogtreecommitdiff
path: root/fitz/filt_jbig2d.c
blob: 122d41ce542f97a2f311d30e288a35c120c806be (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "fitz.h"

/* TODO: complete rewrite with error checking and use fitz memctx */

/*
<rillian> so to use a global_ctx, you run your global data through a normal ctx
<rillian> then call jbig2_make_global_ctx with the normal context
<rillian> that does the (currently null) conversion
<maskros> make_global_ctx after i fed it all the global stream data?
<rillian> maskros: yes
<rillian> and you pass the new global ctx object to jbig2_ctx_new() when you
+create the per-page ctx
*/

#ifdef _WIN32 /* Microsoft Visual C++ */

typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef __int64 int64_t;

typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;

#else
#include <inttypes.h>
#endif

#include <jbig2.h>

typedef struct fz_jbig2d_s fz_jbig2d;

struct fz_jbig2d_s
{
	fz_filter super;
	Jbig2Ctx *ctx;
	Jbig2GlobalCtx *gctx;
	Jbig2Image *page;
	int idx;
};

fz_filter *
fz_newjbig2d(fz_obj *params)
{
	FZ_NEWFILTER(fz_jbig2d, d, jbig2d);
	d->ctx = jbig2_ctx_new(nil, JBIG2_OPTIONS_EMBEDDED, nil, nil, nil);
	d->gctx = nil;
	d->page = nil;
	d->idx = 0;
	return (fz_filter*)d;
}

void
fz_dropjbig2d(fz_filter *filter)
{
	fz_jbig2d *d = (fz_jbig2d*)filter;
	if (d->gctx)
		jbig2_global_ctx_free(d->gctx);
	jbig2_ctx_free(d->ctx);
}

fz_error
fz_setjbig2dglobalstream(fz_filter *filter, unsigned char *buf, int len)
{
	fz_jbig2d *d = (fz_jbig2d*)filter;
	jbig2_data_in(d->ctx, buf, len);
	d->gctx = jbig2_make_global_ctx(d->ctx);
	d->ctx = jbig2_ctx_new(nil, JBIG2_OPTIONS_EMBEDDED, d->gctx, nil, nil);
	return fz_okay;
}

fz_error
fz_processjbig2d(fz_filter *filter, fz_buffer *in, fz_buffer *out)
{
	fz_jbig2d *d = (fz_jbig2d*)filter;
	int len;
	int i;

	while (1)
	{
		if (in->rp == in->wp) {
			if (!in->eof)
				return fz_ioneedin;

			if (!d->page) {
				jbig2_complete_page(d->ctx);
				d->page = jbig2_page_out(d->ctx);
				if (!d->page)
					return fz_throw("jbig2_page_out failed");
			}

			if (out->wp == out->ep)
				return fz_ioneedout;

			len = out->ep - out->wp;
			if (d->idx + len > d->page->height * d->page->stride)
				len = d->page->height * d->page->stride - d->idx;

			/* XXX memcpy(out->wp, d->page->data + d->idx, len); */
			{
				unsigned char * restrict p = &d->page->data[d->idx];
				unsigned char * restrict o = out->wp;
				for (i = 0; i < len; i++)
					*o++ = 0xff ^ *p++;
			}

			out->wp += len;
			d->idx += len;

			if (d->idx == d->page->height * d->page->stride) {
				jbig2_release_page(d->ctx, d->page);
				return fz_iodone;
			}
		}
		else {
			len = in->wp - in->rp;
			jbig2_data_in(d->ctx, in->rp, len);
			in->rp += len;
		}
	}
}