shithub: jbig2

ref: acf706d0e91ebcf8b3d73c83d168af6e2de05a25
dir: /jbig2_priv.h/

View raw version
void *
jbig2_alloc (Jbig2Allocator *allocator, size_t size);

void
jbig2_free (Jbig2Allocator *allocator, void *p);

void *
jbig2_realloc (Jbig2Allocator *allocator, void *p, size_t size);

#define jbig2_new(ctx, t, size) ((t *)jbig2_alloc(ctx->allocator, (size) * sizeof(t)))

#define jbig2_renew(ctx, p, t, size) ((t *)jbig2_realloc(ctx->allocator, (p), (size) * sizeof(t)))

int
jbig2_error (Jbig2Ctx *ctx, Jbig2Severity severity, int32_t seg_idx,
	     const char *fmt, ...);

typedef uint8_t byte;
typedef int bool;

#define TRUE 1
#define FALSE 0

typedef struct _Jbig2Result Jbig2Result;

/* The result of decoding a segment. See 0.1.5 */
struct _Jbig2Result {
  int32_t segment_number;
  int segment_type;
  void (*free)(const Jbig2Result *self, Jbig2Ctx *ctx);
};

typedef enum {
  JBIG2_FILE_HEADER,
  JBIG2_FILE_SEQUENTIAL_HEADER,
  JBIG2_FILE_SEQUENTIAL_BODY,
  JBIG2_FILE_RANDOM_HEADERS,
  JBIG2_FILE_RANDOM_BODIES,
  JBIG2_FILE_EOF
} Jbig2FileState;

struct _Jbig2Ctx {
  Jbig2Allocator *allocator;
  Jbig2Options options;
  const Jbig2Ctx *global_ctx;
  Jbig2ErrorCallback error_callback;
  void *error_callback_data;

  byte *buf;
  int buf_size;
  int buf_rd_ix;
  int buf_wr_ix;

  Jbig2FileState state;

  byte file_header_flags;
  int32_t n_pages;

  int n_sh;
  int n_sh_max;
  Jbig2SegmentHeader **sh_list;
  int sh_ix;

  /* The map from segment numbers to decoding results, currently
     stored as a contiguous, 0-indexed array. */
  int n_results;
  int n_results_max;
  const Jbig2Result **results;
  
  /* list of decoded pages, including the one in progress,
     currently stored as a contiguous, 0-indexed array. */
  int current_page;
  int max_page_index;
  Jbig2Page *pages;
};

int32_t
jbig2_get_int32 (const byte *buf);

int16_t
jbig2_get_int16 (const byte *buf);

/* the page structure handles decoded page
   results. it's allocated by a 'page info'
   segement and marked complete by an 'end of page'
   segment.
*/

typedef enum {
    JBIG2_PAGE_FREE,
    JBIG2_PAGE_NEW,
    JBIG2_PAGE_COMPLETE,
    JBIG2_PAGE_RETURNED,
    JBIG2_PAGE_RELEASED
} Jbig2PageState;

struct _Jbig2Page {
    Jbig2PageState state;
    uint32_t number;
    uint32_t height, width;	/* in pixels */
    uint32_t x_resolution,
             y_resolution;	/* in pixels per meter */
    uint16_t stripe_size;
    bool striped;
    uint8_t flags;
    Jbig2Image *image;
};

int jbig2_read_page_info (Jbig2Ctx *ctx, Jbig2SegmentHeader *sh, const byte *segment_data);
int jbig2_complete_page (Jbig2Ctx *ctx, Jbig2SegmentHeader *sh, const byte *segment_data);

int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y);

/* region segment info */

typedef struct {
  int32_t width;
  int32_t height;
  int32_t x;
  int32_t y;
  byte flags;
} Jbig2RegionSegmentInfo;

void jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const byte *segment_data);
int jbig2_read_text_info(Jbig2Ctx *ctx, Jbig2SegmentHeader *sh, const byte *segment_data);

/* The word stream design is a compromise between simplicity and
   trying to amortize the number of method calls. Each ::get_next_word
   invocation pulls 4 bytes from the stream, packed big-endian into a
   32 bit word. The offset argument is provided as a convenience. It
   begins at 0 and increments by 4 for each successive invocation. */
typedef struct _Jbig2WordStream Jbig2WordStream;

struct _Jbig2WordStream {
  uint32_t (*get_next_word) (Jbig2WordStream *self, int offset);
};

Jbig2WordStream *
jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);

void
jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws);

const Jbig2Result *
jbig2_get_result(Jbig2Ctx *ctx, int32_t segment_number);

int
jbig2_put_result(Jbig2Ctx *ctx, const Jbig2Result *result);