-
Notifications
You must be signed in to change notification settings - Fork 7
/
jbig2_arith_iaid.c
91 lines (75 loc) · 2.04 KB
/
jbig2_arith_iaid.c
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
/*
jbig2dec
Copyright (C) 2001 Artifex Software, Inc.
This software is provided AS-IS with no warranty,
either express or implied.
This software is distributed under license and may not
be copied, modified or distributed except as expressly
authorized under the terms of the license contained in
the file LICENSE in this distribution.
For further licensing information refer to http://artifex.com/ or
contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861.
*/
/* Annex A.3 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"
#include <stddef.h>
#include <string.h> /* memset() */
#ifdef VERBOSE
#include <stdio.h> /* for debug printing only */
#endif
#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_arith.h"
#include "jbig2_arith_iaid.h"
struct _Jbig2ArithIaidCtx {
int SBSYMCODELEN;
Jbig2ArithCx *IAIDx;
};
Jbig2ArithIaidCtx *
jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN)
{
Jbig2ArithIaidCtx *result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
int ctx_size = 1 << SBSYMCODELEN;
result->SBSYMCODELEN = SBSYMCODELEN;
result->IAIDx = jbig2_alloc(ctx->allocator, ctx_size);
memset(result->IAIDx, 0, ctx_size);
return result;
}
/* A.3 */
/* Return value: -1 on error, 0 on normal value */
int
jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as,
int32_t *p_result)
{
Jbig2ArithCx *IAIDx = ctx->IAIDx;
int SBSYMCODELEN = ctx->SBSYMCODELEN;
int PREV = 1;
int D;
int i;
/* A.3 (2) */
for (i = 0; i < SBSYMCODELEN; i++)
{
D = jbig2_arith_decode(as, &IAIDx[PREV]);
#ifdef VERBOSE
fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
#endif
PREV = (PREV << 1) | D;
}
/* A.3 (3) */
PREV -= 1 << SBSYMCODELEN;
#ifdef VERBOSE
fprintf(stderr, "IAID result: %d\n", PREV);
#endif
*p_result = PREV;
return 0;
}
void
jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
{
jbig2_free(ctx->allocator, iax->IAIDx);
jbig2_free(ctx->allocator, iax);
}