|
Packit |
3f21c4 |
/* Copyright (C) 2001-2012 Artifex Software, Inc.
|
|
Packit |
3f21c4 |
All Rights Reserved.
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
This software is provided AS-IS with no warranty, either express or
|
|
Packit |
3f21c4 |
implied.
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
This software is distributed under license and may not be copied,
|
|
Packit |
3f21c4 |
modified or distributed except as expressly authorized under the terms
|
|
Packit |
3f21c4 |
of the license contained in the file LICENSE in this distribution.
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
Refer to licensing information at http://www.artifex.com or contact
|
|
Packit |
3f21c4 |
Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
|
|
Packit |
3f21c4 |
CA 94903, U.S.A., +1(415)492-9861, for further information.
|
|
Packit |
3f21c4 |
*/
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/*
|
|
Packit |
3f21c4 |
jbig2dec
|
|
Packit |
3f21c4 |
*/
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* To enable Memento, either uncomment the following, or arrange to
|
|
Packit |
3f21c4 |
* predefine MEMENTO whilst building. */
|
|
Packit |
3f21c4 |
/* #define MEMENTO */
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* If we are being compiled as part of a larger project that includes
|
|
Packit |
3f21c4 |
* Memento, that project should define JBIG_EXTERNAL_MEMENTO_H to point
|
|
Packit |
3f21c4 |
* to the include file to use.
|
|
Packit |
3f21c4 |
*/
|
|
Packit |
3f21c4 |
#ifdef JBIG_EXTERNAL_MEMENTO_H
|
|
Packit |
3f21c4 |
#include JBIG_EXTERNAL_MEMENTO_H
|
|
Packit |
3f21c4 |
#else
|
|
Packit |
3f21c4 |
#include "memento.h"
|
|
Packit |
3f21c4 |
#endif
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* library internals */
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
typedef uint8_t byte;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#define bool int
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#ifdef __cplusplus
|
|
Packit |
3f21c4 |
#define template template_C
|
|
Packit |
3f21c4 |
#define new new_C
|
|
Packit |
3f21c4 |
#endif
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#ifndef TRUE
|
|
Packit |
3f21c4 |
#define TRUE 1
|
|
Packit |
3f21c4 |
#endif
|
|
Packit |
3f21c4 |
#ifndef FALSE
|
|
Packit |
3f21c4 |
#define FALSE 0
|
|
Packit |
3f21c4 |
#endif
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#ifndef NULL
|
|
Packit |
3f21c4 |
#define NULL ((void*)0)
|
|
Packit |
3f21c4 |
#endif
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
typedef enum {
|
|
Packit |
3f21c4 |
JBIG2_FILE_HEADER,
|
|
Packit |
3f21c4 |
JBIG2_FILE_SEQUENTIAL_HEADER,
|
|
Packit |
3f21c4 |
JBIG2_FILE_SEQUENTIAL_BODY,
|
|
Packit |
3f21c4 |
JBIG2_FILE_RANDOM_HEADERS,
|
|
Packit |
3f21c4 |
JBIG2_FILE_RANDOM_BODIES,
|
|
Packit |
3f21c4 |
JBIG2_FILE_EOF
|
|
Packit |
3f21c4 |
} Jbig2FileState;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
struct _Jbig2Ctx {
|
|
Packit |
3f21c4 |
Jbig2Allocator *allocator;
|
|
Packit |
3f21c4 |
Jbig2Options options;
|
|
Packit |
3f21c4 |
const Jbig2Ctx *global_ctx;
|
|
Packit |
3f21c4 |
Jbig2ErrorCallback error_callback;
|
|
Packit |
3f21c4 |
void *error_callback_data;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
byte *buf;
|
|
Packit |
3f21c4 |
size_t buf_size;
|
|
Packit |
3f21c4 |
unsigned int buf_rd_ix;
|
|
Packit |
3f21c4 |
unsigned int buf_wr_ix;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
Jbig2FileState state;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
uint8_t file_header_flags;
|
|
Packit |
3f21c4 |
uint32_t n_pages;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int n_segments_max;
|
|
Packit |
3f21c4 |
Jbig2Segment **segments;
|
|
Packit |
3f21c4 |
int n_segments; /* index of last segment header parsed */
|
|
Packit |
3f21c4 |
int segment_index; /* index of last segment body parsed */
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* list of decoded pages, including the one in progress,
|
|
Packit |
3f21c4 |
currently stored as a contiguous, 0-indexed array. */
|
|
Packit |
3f21c4 |
int current_page;
|
|
Packit |
3f21c4 |
int max_page_index;
|
|
Packit |
3f21c4 |
Jbig2Page *pages;
|
|
Packit |
3f21c4 |
};
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
uint32_t jbig2_get_uint32(const byte *bptr);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int32_t jbig2_get_int32(const byte *buf);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
uint16_t jbig2_get_uint16(const byte *bptr);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int16_t jbig2_get_int16(const byte *buf);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* dynamic memory management */
|
|
Packit |
3f21c4 |
void *jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
void jbig2_free(Jbig2Allocator *allocator, void *p);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
void *jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#define jbig2_new(ctx, t, size) ((t *)jbig2_alloc(ctx->allocator, size, sizeof(t)))
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#define jbig2_renew(ctx, p, t, size) ((t *)jbig2_realloc(ctx->allocator, (p), size, sizeof(t)))
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t seg_idx, const char *fmt, ...);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* the page structure handles decoded page
|
|
Packit |
3f21c4 |
results. it's allocated by a 'page info'
|
|
Packit |
3f21c4 |
segement and marked complete by an 'end of page'
|
|
Packit |
3f21c4 |
segment.
|
|
Packit |
3f21c4 |
*/
|
|
Packit |
3f21c4 |
typedef enum {
|
|
Packit |
3f21c4 |
JBIG2_PAGE_FREE,
|
|
Packit |
3f21c4 |
JBIG2_PAGE_NEW,
|
|
Packit |
3f21c4 |
JBIG2_PAGE_COMPLETE,
|
|
Packit |
3f21c4 |
JBIG2_PAGE_RETURNED,
|
|
Packit |
3f21c4 |
JBIG2_PAGE_RELEASED
|
|
Packit |
3f21c4 |
} Jbig2PageState;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
struct _Jbig2Page {
|
|
Packit |
3f21c4 |
Jbig2PageState state;
|
|
Packit |
3f21c4 |
uint32_t number;
|
|
Packit |
3f21c4 |
uint32_t height, width; /* in pixels */
|
|
Packit |
3f21c4 |
uint32_t x_resolution, y_resolution; /* in pixels per meter */
|
|
Packit |
3f21c4 |
uint16_t stripe_size;
|
|
Packit |
3f21c4 |
bool striped;
|
|
Packit |
3f21c4 |
uint32_t end_row;
|
|
Packit |
3f21c4 |
uint8_t flags;
|
|
Packit |
3f21c4 |
Jbig2Image *image;
|
|
Packit |
3f21c4 |
};
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
int jbig2_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
int jbig2_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
typedef enum {
|
|
Packit |
3f21c4 |
JBIG2_COMPOSE_OR = 0,
|
|
Packit |
3f21c4 |
JBIG2_COMPOSE_AND = 1,
|
|
Packit |
3f21c4 |
JBIG2_COMPOSE_XOR = 2,
|
|
Packit |
3f21c4 |
JBIG2_COMPOSE_XNOR = 3,
|
|
Packit |
3f21c4 |
JBIG2_COMPOSE_REPLACE = 4
|
|
Packit |
3f21c4 |
} Jbig2ComposeOp;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op);
|
|
Packit |
3f21c4 |
int jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *src, int x, int y, Jbig2ComposeOp op);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* region segment info */
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
typedef struct {
|
|
Packit |
3f21c4 |
int32_t width;
|
|
Packit |
3f21c4 |
int32_t height;
|
|
Packit |
3f21c4 |
int32_t x;
|
|
Packit |
3f21c4 |
int32_t y;
|
|
Packit |
3f21c4 |
Jbig2ComposeOp op;
|
|
Packit |
3f21c4 |
uint8_t flags;
|
|
Packit |
3f21c4 |
} Jbig2RegionSegmentInfo;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
void jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
int jbig2_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* 7.4 */
|
|
Packit |
3f21c4 |
int jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
|
|
Packit |
3f21c4 |
int jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
int jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
|
|
Packit |
3f21c4 |
int jbig2_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* The word stream design is a compromise between simplicity and
|
|
Packit |
3f21c4 |
trying to amortize the number of method calls. Each ::get_next_word
|
|
Packit |
3f21c4 |
invocation pulls 4 bytes from the stream, packed big-endian into a
|
|
Packit |
3f21c4 |
32 bit word. The offset argument is provided as a convenience. It
|
|
Packit |
3f21c4 |
begins at 0 and increments by 4 for each successive invocation. */
|
|
Packit |
3f21c4 |
typedef struct _Jbig2WordStream Jbig2WordStream;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
struct _Jbig2WordStream {
|
|
Packit |
3f21c4 |
void (*get_next_word)(Jbig2WordStream *self, size_t offset, uint32_t *word);
|
|
Packit |
3f21c4 |
};
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
Jbig2WordStream *jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
void jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws);
|