t82.h

00001 /*
00002  *  Header file for the portable free JBIG compression library
00003  *
00004  *  Markus Kuhn -- http://www.cl.cam.ac.uk/~mgk25/
00005  *
00006  *  $Id: t82.h,v 1.5 2008/03/14 14:28:17 steveu Exp $
00007  */
00008 
00009 #if !defined(_SPANDSP_T82_H_)
00010 #define _SPANDSP_T82_H_
00011 
00012 /*
00013  * JBIG-KIT version number
00014  */
00015 #define T82_VERSION    "1.6"
00016 
00017 /*
00018  * Buffer block for SDEs which are temporarily stored by encoder
00019  */
00020 
00021 #define T82_BUFSIZE 4000
00022 
00023 /*! T.82 buffer */
00024 struct t82_buf
00025 {
00026     uint8_t d[T82_BUFSIZE];         /* one block of a buffer list */
00027     int len;                        /* length of the data in this block */
00028     struct t82_buf *next;           /* pointer to next block */
00029     struct t82_buf *previous;       /* pointer to previous block *
00030                                      * (unused in freelist)      */
00031     struct t82_buf *last;           /* only used in list head: final block of list */
00032     struct t82_buf **free_list;     /* pointer to pointer to head of free list */
00033 };
00034 
00035 /*
00036  * Maximum number of ATMOVEs per stripe that decoder can handle
00037  */
00038 #define T82_ATMOVES_MAX  64
00039 
00040 /*
00041  * Option and order flags
00042  */
00043 #define T82_HITOLO     0x08
00044 #define T82_SEQ        0x04
00045 #define T82_ILEAVE     0x02
00046 #define T82_SMID       0x01
00047 
00048 #define T82_LRLTWO     0x40
00049 #define T82_VLENGTH    0x20
00050 #define T82_TPDON      0x10
00051 #define T82_TPBON      0x08
00052 #define T82_DPON       0x04
00053 #define T82_DPPRIV     0x02
00054 #define T82_DPLAST     0x01
00055 
00056 #define T82_DELAY_AT   0x100  /* delay ATMOVE until the first line of the next
00057                                * stripe. Option available for compatibility
00058                                * with conformance test example in clause 7.2.*/
00059 
00060 /*
00061  * Possible error code return values
00062  */
00063 #define T82_EOK        0
00064 #define T82_EOK_INTR   1
00065 #define T82_EAGAIN     2
00066 #define T82_ENOMEM     3
00067 #define T82_EABORT     4
00068 #define T82_EMARKER    5
00069 #define T82_ENOCONT    6
00070 #define T82_EINVAL     7
00071 #define T82_EIMPL      8
00072 
00073 /*
00074  * Language code for error message strings (based on ISO 639 2-letter
00075  * standard language name abbreviations).
00076  */
00077 #define T82_EN         0        /* English */
00078 #define T82_DE_8859_1  1        /* German in ISO Latin 1 character set */
00079 #define T82_DE_UTF_8   2        /* German in Unicode UTF-8 encoding */
00080 
00081 /*
00082  * Status description of an arithmetic encoder
00083  */
00084 struct t82_arenc_state
00085 {
00086     uint8_t st[4096];          /* probability status for contexts, MSB = MPS */
00087     uint32_t c;                     /* C register, base of coding intervall, *
00088                                      * layout as in Table 23                 */
00089     uint32_t a;           /* A register, normalized size of coding intervall */
00090     int32_t sc;     /* counter for buffered 0xff values which might overflow */
00091     int ct;  /* bit shift counter, determines when next byte will be written */
00092     int buffer;                /* buffer for most recent output byte != 0xff */
00093     void (*byte_out)(int, void *); /* function which receives all PSCD bytes */
00094     void *file;                              /* parameter passed to byte_out */
00095 };
00096 
00097 /*
00098  * Status description of an arithmetic decoder
00099  */
00100 enum t82_ardec_result
00101 {
00102     T82_OK,         /* symbol has been successfully decoded */
00103     T82_READY,      /* no more bytes of this PSCD required, marker  *
00104                      * encountered, probably more symbols available */
00105     T82_MORE,       /* more PSCD data bytes required to decode a symbol */
00106     T82_MARKER      /* more PSCD data bytes required, ignored final 0xff byte */
00107 };
00108 
00109 struct t82_ardec_state
00110 {
00111     uint8_t st[4096];          /* probability status for contexts, MSB = MPS */
00112     uint32_t c;                     /* C register, base of coding interval,  *
00113                                      * layout as in Table 25                 */
00114     uint32_t a;            /* A register, normalized size of coding interval */
00115     int ct;     /* bit shift counter, determines when next byte will be read */
00116     const uint8_t *pscd_ptr;               /* pointer to next PSCD data byte */
00117     const uint8_t *pscd_end;                   /* pointer to byte after PSCD */
00118     enum t82_ardec_result result;          /* result of previous decode call */
00119     int startup;                            /* controls initial fill of s->c */
00120 };
00121 
00122 /*! State of a single instance of a T.82 JBIG encoder */
00123 typedef struct
00124 {
00125     int d;                            /* resolution layer of the input image */
00126     uint32_t xd;             /* size of the input image (resolution layer d) */
00127     uint32_t yd;
00128     uint32_t yd1;         /* BIH announced height of image, use yd1 != yd to
00129                           emulate T.85-style NEWLEN height updates for tests */
00130     int planes;                         /* number of different bitmap planes */
00131     int dl;                       /* lowest resolution layer in the next BIE */
00132     int dh;                      /* highest resolution layer in the next BIE */
00133     uint32_t l0;                     /* number of lines per stripe at lowest *
00134                                       * resolution layer 0                   */
00135     uint32_t stripes;         /* number of stripes required  (determ. by l0) */
00136     uint8_t **lhp[2];          /* pointers to lower/higher resolution images */
00137     int *highres;                 /* index [plane] of highres image in lhp[] */
00138     int order;                                    /* SDE ordering parameters */
00139     int options;                                      /* encoding parameters */
00140     unsigned mx;                               /* maximum ATMOVE window size */
00141     unsigned my;
00142     int *tx;       /* array [plane] with x-offset of adaptive template pixel */
00143     const uint8_t *dppriv;/* optional private deterministic prediction table */
00144     const char *res_tab;     /* table for the resolution reduction algorithm */
00145     struct t82_buf ****sde;      /* array [stripe][layer][plane] pointers to *
00146                                   * buffers for stored SDEs                  */
00147     struct t82_arenc_state *s;  /* array [planes] for arithm. encoder status */
00148     struct t82_buf *free_list; /* list of currently unused SDE block buffers */
00149     void (*data_out)(void *file, uint8_t *start, size_t len); /* data write callback */
00150     void *file;                            /* parameter passed to data_out() */
00151     char *tp;    /* buffer for temp. values used by diff. typical prediction */
00152 } t82_tx_state_t;
00153 
00154 
00155 /*! State of a single instance of a T.82 JBIG decoder */
00156 typedef struct
00157 {
00158     /* data from BIH */
00159     int d;                             /* resolution layer of the full image */
00160     int dl;                            /* first resolution layer in this BIE */
00161     uint32_t xd;              /* size of the full image (resolution layer d) */
00162     uint32_t yd;
00163     int planes;                         /* number of different bitmap planes */
00164     uint32_t l0;                     /* number of lines per stripe at lowest *
00165                                       * resolution layer 0                   */
00166     uint32_t stripes;         /* number of stripes required  (determ. by l0) */
00167     int order;                                    /* SDE ordering parameters */
00168     int options;                                      /* encoding parameters */
00169     int mx;                                    /* maximum ATMOVE window size */
00170     int my;
00171     const uint8_t *dppriv;/* optional private deterministic prediction table */
00172 
00173     /* loop variables */
00174     uint32_t ii[3];  /* current stripe, layer, plane (outer loop first) */
00175 
00176     /*
00177      * Pointers to array [planes] of lower/higher resolution images.
00178      * lhp[d & 1] contains image of layer d.
00179      */
00180     uint8_t **lhp[2];
00181 
00182     /* status information */
00183     int **tx;         /* array [plane][layer-dl] with x,y-offset of AT pixel */
00184     int **ty;
00185     struct t82_ardec_state **s;    /* array [plane][layer-dl] for arithmetic *
00186                                     * decoder status                         */
00187     int **reset;     /* array [plane][layer-dl] remembers if previous stripe *
00188                       * in that plane/resolution ended with SDRST.           */
00189     uint32_t bie_len;                         /* number of bytes read so far */
00190     uint8_t buffer[20];       /* used to store BIH or marker segments fragm. */
00191     int buf_len;                                /* number of bytes in buffer */
00192     uint32_t comment_skip;           /* remaining bytes of a COMMENT segment */
00193     uint32_t x;                   /* x position of next pixel in current SDE */
00194     uint32_t i;      /* line in current SDE (first line of each stripe is 0) */
00195     int at_moves;                /* number of AT moves in the current stripe */
00196     uint32_t at_line[T82_ATMOVES_MAX];                /* lines at which an   *
00197                                                        * AT move will happen */
00198     int at_tx[T82_ATMOVES_MAX], at_ty[T82_ATMOVES_MAX]; /* ATMOVE offsets in *
00199                                                          * current stripe    */
00200     uint32_t line_h1;                            /* variables of decode_pscd */
00201     uint32_t line_h2;
00202     uint32_t line_h3;
00203     uint32_t line_l1;
00204     uint32_t line_l2;
00205     uint32_t line_l3;
00206     int pseudo;         /* flag for TPBON/TPDON:  next pixel is pseudo pixel */
00207     int **lntp;        /* flag [plane][layer-dl] for TP: line is not typical */
00208 
00209     uint32_t xmax;                    /* if possible abort before image gets */
00210     uint32_t ymax;                    /* larger than this size               */
00211     int dmax;                                      /* abort after this layer */
00212 } t82_rx_state_t;
00213 
00214 #if defined(__cplusplus)
00215 extern "C" {
00216 #endif
00217 
00218 #ifdef TEST_CODEC
00219 void arith_encode_init(struct t82_arenc_state *s, int reuse_st);
00220 void arith_encode_flush(struct t82_arenc_state *s);
00221 void arith_encode(struct t82_arenc_state *s, int cx, int pix);
00222 void arith_decode_init(struct t82_ardec_state *s, int reuse_st);
00223 int arith_decode(struct t82_ardec_state *s, int cx);
00224 #endif
00225 
00226 /* some macros (too trivial for a function) */
00227 #define t82_dec_getplanes(s)     ((s)->planes)
00228 
00229 void t82_enc_init(t82_tx_state_t *s,
00230                   uint32_t x,
00231                   uint32_t y,
00232                   int planes,
00233                   uint8_t **p,
00234                   void (*data_out)(void *file, uint8_t *start, size_t len),
00235                   void *file);
00236 int t82_enc_lrlmax(t82_tx_state_t *s,
00237                    uint32_t mwidth,
00238                    uint32_t mheight);
00239 void t82_enc_layers(t82_tx_state_t *s, int d);
00240 int  t82_enc_lrange(t82_tx_state_t *s, int dl, int dh);
00241 void t82_enc_options(t82_tx_state_t *s,
00242                      int order,
00243                      int options,
00244                      uint32_t l0,
00245                      int mx,
00246                      int my);
00247 void t82_enc_out(t82_tx_state_t *s);
00248 void t82_enc_free(t82_tx_state_t *s);
00249 
00250 void t82_dec_init(t82_rx_state_t *s);
00251 void t82_dec_maxsize(t82_rx_state_t *s,
00252                      uint32_t xmax,
00253                      uint32_t ymax);
00254 int  t82_dec_in(t82_rx_state_t *s, uint8_t *data, size_t len,
00255                 size_t *cnt);
00256 long t82_dec_getwidth(const t82_rx_state_t *s);
00257 long t82_dec_getheight(const t82_rx_state_t *s);
00258 uint8_t *t82_dec_getimage(const t82_rx_state_t *s, int plane);
00259 long t82_dec_getsize(const t82_rx_state_t *s);
00260 void t82_dec_merge_planes(const t82_rx_state_t *s,
00261                           int use_graycode,
00262                           void (*data_out)(void *file, uint8_t *start, size_t len),
00263                           void *file);
00264 long t82_dec_getsize_merged(const t82_rx_state_t *s);
00265 void t82_dec_free(t82_rx_state_t *s);
00266 
00267 const char *t82_strerror(int errnum, int language);
00268 void t82_int2dppriv(uint8_t *dptable, const uint8_t *internal);
00269 void t82_dppriv2int(uint8_t *internal, const uint8_t *dptable);
00270 uint32_t t82_ceil_half(uint32_t x, int n);
00271 void t82_split_planes(uint32_t x,
00272                       uint32_t y,
00273                       int has_planes,
00274                       int encode_planes,
00275                       const uint8_t *src,
00276                       const uint8_t **dest,
00277                       int use_graycode);
00278 int t82_newlen(uint8_t bie[], size_t len);
00279 
00280 #if defined(__cplusplus)
00281 }
00282 #endif
00283 
00284 #endif
00285 /*- End of file ------------------------------------------------------------*/

Generated on Tue Oct 7 20:25:49 2008 for spandsp by  doxygen 1.5.6