jbig2_mmr.c
来自「SumatraPDF是一款小型开源的pdf阅读工具。虽然玲珑小巧(只有800多K」· C语言 代码 · 共 994 行 · 第 1/2 页
C
994 行
/*
jbig2dec
Copyright (C) 2001-2002 Artifex Software, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
$Id: jbig2_mmr.c 465 2008-05-16 23:48:20Z giles $
*/
/*
An implementation of MMR decoding. This is based on the implementation
in Fitz, which in turn is based on the one in Ghostscript.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_arith.h"
#include "jbig2_generic.h"
#include "jbig2_mmr.h"
typedef struct {
int width;
int height;
const byte *data;
size_t size;
int data_index;
int bit_index;
uint32_t word;
} Jbig2MmrCtx;
static void
jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size)
{
int i;
uint32_t word = 0;
mmr->width = width;
mmr->height = height;
mmr->data = data;
mmr->size = size;
mmr->data_index = 0;
mmr->bit_index = 0;
for (i = 0; i < size && i < 4; i++)
word |= (data[i] << ((3 - i) << 3));
mmr->word = word;
}
static void
jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits)
{
mmr->word <<= n_bits;
mmr->bit_index += n_bits;
while (mmr->bit_index >= 8) {
mmr->bit_index -= 8;
if (mmr->data_index + 4 < mmr->size)
mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index);
mmr->data_index++;
}
}
/*
<raph> the first 2^(initialbits) entries map bit patterns to decodes
<raph> let's say initial_bits is 8 for the sake of example
<raph> and that the code is 1001
<raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
<raph> because those are all the bytes that start with the code
<raph> and the 4 is the length of the code
... if (n_bits > initial_bits) ...
<raph> anyway, in that case, it basically points to a mini table
<raph> the n_bits is the maximum length of all codes beginning with that byte
<raph> so 2^(n_bits - initial_bits) is the size of the mini-table
<raph> peter came up with this, and it makes sense
*/
typedef struct {
short val;
short n_bits;
} mmr_table_node;
/* white decode table (runlength huffman codes) */
const mmr_table_node jbig2_mmr_white_decode[] = {
{ 256, 12 },
{ 272, 12 },
{ 29, 8 },
{ 30, 8 },
{ 45, 8 },
{ 46, 8 },
{ 22, 7 },
{ 22, 7 },
{ 23, 7 },
{ 23, 7 },
{ 47, 8 },
{ 48, 8 },
{ 13, 6 },
{ 13, 6 },
{ 13, 6 },
{ 13, 6 },
{ 20, 7 },
{ 20, 7 },
{ 33, 8 },
{ 34, 8 },
{ 35, 8 },
{ 36, 8 },
{ 37, 8 },
{ 38, 8 },
{ 19, 7 },
{ 19, 7 },
{ 31, 8 },
{ 32, 8 },
{ 1, 6 },
{ 1, 6 },
{ 1, 6 },
{ 1, 6 },
{ 12, 6 },
{ 12, 6 },
{ 12, 6 },
{ 12, 6 },
{ 53, 8 },
{ 54, 8 },
{ 26, 7 },
{ 26, 7 },
{ 39, 8 },
{ 40, 8 },
{ 41, 8 },
{ 42, 8 },
{ 43, 8 },
{ 44, 8 },
{ 21, 7 },
{ 21, 7 },
{ 28, 7 },
{ 28, 7 },
{ 61, 8 },
{ 62, 8 },
{ 63, 8 },
{ 0, 8 },
{ 320, 8 },
{ 384, 8 },
{ 10, 5 },
{ 10, 5 },
{ 10, 5 },
{ 10, 5 },
{ 10, 5 },
{ 10, 5 },
{ 10, 5 },
{ 10, 5 },
{ 11, 5 },
{ 11, 5 },
{ 11, 5 },
{ 11, 5 },
{ 11, 5 },
{ 11, 5 },
{ 11, 5 },
{ 11, 5 },
{ 27, 7 },
{ 27, 7 },
{ 59, 8 },
{ 60, 8 },
{ 288, 9 },
{ 290, 9 },
{ 18, 7 },
{ 18, 7 },
{ 24, 7 },
{ 24, 7 },
{ 49, 8 },
{ 50, 8 },
{ 51, 8 },
{ 52, 8 },
{ 25, 7 },
{ 25, 7 },
{ 55, 8 },
{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