Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4680 right-hear 1
/*
2
    jbig2dec
3
 
4
    Copyright (C) 2002 Artifex Software, Inc.
5
 
6
    This software is distributed under license and may not
7
    be copied, modified or distributed except as expressly
8
    authorized under the terms of the license contained in
9
    the file LICENSE in this distribution.
10
 
11
    For further licensing information refer to http://artifex.com/ or
12
    contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
13
    San Rafael, CA  94903, U.S.A., +1(415)492-9861.
14
*/
15
 
16
/* library internals */
17
 
18
typedef uint8_t byte;
19
typedef int bool;
20
 
21
#ifndef TRUE
22
#define TRUE 1
23
#endif
24
#ifndef FALSE
25
#define FALSE 0
26
#endif
27
 
28
#ifndef NULL
29
#define NULL ((void*)0)
30
#endif
31
 
32
typedef enum {
33
  JBIG2_FILE_HEADER,
34
  JBIG2_FILE_SEQUENTIAL_HEADER,
35
  JBIG2_FILE_SEQUENTIAL_BODY,
36
  JBIG2_FILE_RANDOM_HEADERS,
37
  JBIG2_FILE_RANDOM_BODIES,
38
  JBIG2_FILE_EOF
39
} Jbig2FileState;
40
 
41
struct _Jbig2Ctx {
42
  Jbig2Allocator *allocator;
43
  Jbig2Options options;
44
  const Jbig2Ctx *global_ctx;
45
  Jbig2ErrorCallback error_callback;
46
  void *error_callback_data;
47
 
48
  byte *buf;
49
  size_t buf_size;
50
  unsigned int buf_rd_ix;
51
  unsigned int buf_wr_ix;
52
 
53
  Jbig2FileState state;
54
 
55
  uint8_t file_header_flags;
56
  int32_t n_pages;
57
 
58
  int n_segments_max;
59
  Jbig2Segment **segments;
60
  int n_segments;	/* index of last segment header parsed */
61
  int segment_index;    /* index of last segment body parsed */
62
 
63
  /* list of decoded pages, including the one in progress,
64
     currently stored as a contiguous, 0-indexed array. */
65
  int current_page;
66
  int max_page_index;
67
  Jbig2Page *pages;
68
};
69
 
70
int32_t
71
jbig2_get_int32 (const byte *buf);
72
 
73
int16_t
74
jbig2_get_int16 (const byte *buf);
75
 
76
/* dynamic memory management */
77
void *
78
jbig2_alloc (Jbig2Allocator *allocator, size_t size);
79
 
80
void
81
jbig2_free (Jbig2Allocator *allocator, void *p);
82
 
83
void *
84
jbig2_realloc (Jbig2Allocator *allocator, void *p, size_t size);
85
 
86
#define jbig2_new(ctx, t, size) ((t *)jbig2_alloc(ctx->allocator, (size) * sizeof(t)))
87
 
88
#define jbig2_renew(ctx, p, t, size) ((t *)jbig2_realloc(ctx->allocator, (p), (size) * sizeof(t)))
89
 
90
int
91
jbig2_error (Jbig2Ctx *ctx, Jbig2Severity severity, int32_t seg_idx,
92
	     const char *fmt, ...);
93
 
94
/* the page structure handles decoded page
95
   results. it's allocated by a 'page info'
96
   segement and marked complete by an 'end of page'
97
   segment.
98
*/
99
typedef enum {
100
    JBIG2_PAGE_FREE,
101
    JBIG2_PAGE_NEW,
102
    JBIG2_PAGE_COMPLETE,
103
    JBIG2_PAGE_RETURNED,
104
    JBIG2_PAGE_RELEASED
105
} Jbig2PageState;
106
 
107
struct _Jbig2Page {
108
    Jbig2PageState state;
109
    uint32_t number;
110
    uint32_t height, width;	/* in pixels */
111
    uint32_t x_resolution,
112
             y_resolution;	/* in pixels per meter */
113
    uint16_t stripe_size;
114
    bool striped;
115
    int end_row;
116
    uint8_t flags;
117
    Jbig2Image *image;
118
};
119
 
120
int jbig2_page_info (Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
121
int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
122
int jbig2_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
123
int jbig2_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
124
 
125
typedef enum {
126
    JBIG2_COMPOSE_OR = 0,
127
    JBIG2_COMPOSE_AND = 1,
128
    JBIG2_COMPOSE_XOR = 2,
129
    JBIG2_COMPOSE_XNOR = 3,
130
    JBIG2_COMPOSE_REPLACE = 4
131
} Jbig2ComposeOp;
132
 
133
int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op);
134
int jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *src, int x, int y, Jbig2ComposeOp op);
135
 
136
/* region segment info */
137
 
138
typedef struct {
139
  int32_t width;
140
  int32_t height;
141
  int32_t x;
142
  int32_t y;
143
  Jbig2ComposeOp op;
144
  uint8_t flags;
145
} Jbig2RegionSegmentInfo;
146
 
147
void jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const uint8_t *segment_data);
148
int jbig2_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
149
 
150
/* 7.4 */
151
int jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
152
			       const uint8_t *segment_data);
153
int jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
154
                               const byte *segment_data);
155
 
156
int jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment,
157
                               const byte *segment_data);
158
int jbig2_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
159
                               const byte *segment_data);
160
 
161
 
162
/* The word stream design is a compromise between simplicity and
163
   trying to amortize the number of method calls. Each ::get_next_word
164
   invocation pulls 4 bytes from the stream, packed big-endian into a
165
   32 bit word. The offset argument is provided as a convenience. It
166
   begins at 0 and increments by 4 for each successive invocation. */
167
typedef struct _Jbig2WordStream Jbig2WordStream;
168
 
169
struct _Jbig2WordStream {
170
  uint32_t (*get_next_word) (Jbig2WordStream *self, int offset);
171
};
172
 
173
Jbig2WordStream *
174
jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);
175
 
176
void
177
jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws);