home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / ctenari / Krutak / univiewi_jbig.exe / plugins / JBIG / jbig_lib / jbig.h < prev    next >
C/C++ Source or Header  |  2000-08-02  |  11KB  |  268 lines

  1. /*
  2.  *  Header file for the portable free JBIG compression library
  3.  *
  4.  *  Markus Kuhn -- mkuhn@acm.org
  5.  *
  6.  *  $Id: jbig.h,v 1.10 2000-08-02 23:54:28+01 mgk25 Exp $
  7.  */
  8.  
  9. #ifndef JBG_H
  10. #define JBG_H
  11.  
  12. #include <stddef.h>
  13.  
  14. /*
  15.  * JBIG-KIT version number
  16.  */
  17.  
  18. #define JBG_VERSION    "1.3"
  19.  
  20. /*
  21.  * Buffer block for SDEs which are temporarily stored by encoder
  22.  */
  23.  
  24. #define JBG_BUFSIZE 4000
  25.  
  26. struct jbg_buf {
  27.   unsigned char d[JBG_BUFSIZE];              /* one block of a buffer list */
  28.   int len;                             /* length of the data in this block */
  29.   struct jbg_buf *next;                           /* pointer to next block */
  30.   struct jbg_buf *previous;                   /* pointer to previous block *
  31.                            * (unused in freelist)      */
  32.   struct jbg_buf *last;     /* only used in list head: final block of list */
  33.   struct jbg_buf **free_list;   /* pointer to pointer to head of free list */
  34. };
  35.  
  36. /*
  37.  * Maximum number of allowed ATMOVEs per stripe
  38.  */
  39.  
  40. #define JBG_ATMOVES_MAX  64
  41.  
  42. /*
  43.  * Option and order flags
  44.  */
  45.  
  46. #define JBG_HITOLO     0x08
  47. #define JBG_SEQ        0x04
  48. #define JBG_ILEAVE     0x02
  49. #define JBG_SMID       0x01
  50.  
  51. #define JBG_LRLTWO     0x40
  52. #define JBG_VLENGTH    0x20
  53. #define JBG_TPDON      0x10
  54. #define JBG_TPBON      0x08
  55. #define JBG_DPON       0x04
  56. #define JBG_DPPRIV     0x02
  57. #define JBG_DPLAST     0x01
  58.  
  59. #define JBG_DELAY_AT   0x100  /* delay ATMOVE until the first line of the next
  60.                    * stripe. Option available for compatibility
  61.                    * with conformance test example in clause 7.2.*/
  62.  
  63.  
  64. /*
  65.  * Possible error code return values
  66.  */
  67.  
  68. #define JBG_EOK        0
  69. #define JBG_EOK_INTR   1
  70. #define JBG_EAGAIN     2
  71. #define JBG_ENOMEM     3
  72. #define JBG_EABORT     4
  73. #define JBG_EMARKER    5
  74. #define JBG_ENOCONT    6
  75. #define JBG_EINVAL     7
  76. #define JBG_EIMPL      8
  77.  
  78. /*
  79.  * Language code for error message strings (based on ISO 639 2-letter
  80.  * standard language name abbreviations).
  81.  */ 
  82.  
  83. #define JBG_EN         0        /* English */
  84. #define JBG_DE_8859_1  1        /* German in ISO Latin 1 character set */
  85. #define JBG_DE_UTF_8   2        /* German in Unicode UTF-8 encoding */
  86.  
  87. /*
  88.  * Status description of an arithmetic encoder
  89.  */
  90.  
  91. struct jbg_arenc_state {
  92.   unsigned char st[4096];    /* probability status for contexts, MSB = MPS */
  93.   unsigned long c;                /* C register, base of coding intervall, *
  94.                                    * layout as in Table 23                 */
  95.   unsigned long a;      /* A register, normalized size of coding intervall */
  96.   long sc;        /* counter for buffered 0xff values which might overflow */
  97.   int ct;  /* bit shift counter, determines when next byte will be written */
  98.   int buffer;                /* buffer for most recent output byte != 0xff */
  99.   void (*byte_out)(int, void *); /* function which receives all PSCD bytes */
  100.   void *file;                              /* parameter passed to byte_out */
  101. };
  102.  
  103.  
  104. /*
  105.  * Status description of an arithmetic decoder
  106.  */
  107.  
  108. struct jbg_ardec_state {
  109.   unsigned char st[4096];    /* probability status for contexts, MSB = MPS */
  110.   unsigned long c;                /* C register, base of coding intervall, *
  111.                                    * layout as in Table 25                 */
  112.   unsigned long a;      /* A register, normalized size of coding intervall */
  113.   int ct;     /* bit shift counter, determines when next byte will be read */
  114.   unsigned char *pscd_ptr;               /* pointer to next PSCD data byte */
  115.   unsigned char *pscd_end;                   /* pointer to byte after PSCD */
  116.   enum {
  117.     JBG_OK,                        /* symbol has been successfully decoded */
  118.     JBG_READY,             /* no more bytes of this PSCD required, marker  *
  119.                     * encountered, probably more symbols available */
  120.     JBG_MORE,          /* more PSCD data bytes required to decode a symbol */
  121.     JBG_MARKER   /* more PSCD data bytes required, ignored final 0xff byte */
  122.   } result;                              /* result of previous decode call */
  123.   int startup;                            /* controls initial fill of s->c */
  124. };
  125.  
  126. #ifdef TEST_CODEC
  127. void arith_encode_init(struct jbg_arenc_state *s, int reuse_st);
  128. void arith_encode_flush(struct jbg_arenc_state *s);
  129. void arith_encode(struct jbg_arenc_state *s, int cx, int pix);
  130. void arith_decode_init(struct jbg_ardec_state *s, int reuse_st);
  131. int arith_decode(struct jbg_ardec_state *s, int cx);
  132. #endif
  133.  
  134.  
  135. /*
  136.  * Status of a JBIG encoder
  137.  */
  138.  
  139. struct jbg_enc_state {
  140.   int d;                            /* resolution layer of the input image */
  141.   unsigned long xd, yd;    /* size of the input image (resolution layer d) */
  142.   int planes;                         /* number of different bitmap planes */
  143.   int dl;                       /* lowest resolution layer in the next BIE */
  144.   int dh;                      /* highest resolution layer in the next BIE */
  145.   unsigned long l0;                /* number of lines per stripe at lowest *
  146.                                     * resolution layer 0                   */
  147.   unsigned long stripes;    /* number of stripes required  (determ. by l0) */
  148.   unsigned char **lhp[2];    /* pointers to lower/higher resolution images */
  149.   int *highres;                 /* index [plane] of highres image in lhp[] */
  150.   int order;                                    /* SDE ordering parameters */
  151.   int options;                                      /* encoding parameters */
  152.   unsigned mx, my;                           /* maximum ATMOVE window size */
  153.   int *tx;       /* array [plane] with x-offset of adaptive template pixel */
  154.   char *dppriv;         /* optional private deterministic prediction table */
  155.   char *res_tab;           /* table for the resolution reduction algorithm */
  156.   struct jbg_buf ****sde;      /* array [stripe][layer][plane] pointers to *
  157.                 * buffers for stored SDEs                  */
  158.   struct jbg_arenc_state *s;  /* array [planes] for arithm. encoder status */
  159.   struct jbg_buf *free_list; /* list of currently unused SDE block buffers */
  160.   void (*data_out)(unsigned char *start, size_t len, void *file);
  161.                                                     /* data write callback */
  162.   void *file;                            /* parameter passed to data_out() */
  163.   char *tp;    /* buffer for temp. values used by diff. typical prediction */
  164. };
  165.  
  166.  
  167. /*
  168.  * Status of a JBIG decoder
  169.  */
  170.  
  171. struct jbg_dec_state {
  172.   /* data from BIH */
  173.   int d;                             /* resolution layer of the full image */
  174.   int dl;                            /* first resolution layer in this BIE */
  175.   unsigned long xd, yd;     /* size of the full image (resolution layer d) */
  176.   int planes;                         /* number of different bitmap planes */
  177.   unsigned long l0;                /* number of lines per stripe at lowest *
  178.                     * resolution layer 0                   */
  179.   unsigned long stripes;    /* number of stripes required  (determ. by l0) */
  180.   int order;                                    /* SDE ordering parameters */
  181.   int options;                                      /* encoding parameters */
  182.   int mx, my;                                /* maximum ATMOVE window size */
  183.   char *dppriv;         /* optional private deterministic prediction table */
  184.  
  185.   /* loop variables */
  186.   unsigned long ii[3];  /* current stripe, layer, plane (outer loop first) */
  187.  
  188.   /*
  189.    * Pointers to array [planes] of lower/higher resolution images.
  190.    * lhp[d & 1] contains image of layer d. 
  191.    */
  192.   unsigned char **lhp[2];
  193.  
  194.   /* status information */
  195.   int **tx, **ty;   /* array [plane][layer-dl] with x,y-offset of AT pixel */
  196.   struct jbg_ardec_state **s;    /* array [plane][layer-dl] for arithmetic *
  197.                   * decoder status */
  198.   int **reset;     /* array [plane][layer-dl] remembers if previous stripe *
  199.             * in that plane/resolution ended with SDRST.           */
  200.   unsigned long bie_len;                    /* number of bytes read so far */
  201.   unsigned char buffer[20]; /* used to store BIH or marker segments fragm. */
  202.   int buf_len;                                /* number of bytes in buffer */
  203.   unsigned long comment_skip;      /* remaining bytes of a COMMENT segment */
  204.   unsigned long x;              /* x position of next pixel in current SDE */
  205.   unsigned long i; /* line in current SDE (first line of each stripe is 0) */ 
  206.   int at_moves;                /* number of AT moves in the current stripe */
  207.   unsigned long at_line[JBG_ATMOVES_MAX];           /* lines at which an   *
  208.                                  * AT move will happen */
  209.   int at_tx[JBG_ATMOVES_MAX], at_ty[JBG_ATMOVES_MAX]; /* ATMOVE offsets in *
  210.                                * current stripe    */
  211.   unsigned long line_h1, line_h2, line_h3;     /* variables of decode_pscd */
  212.   unsigned long line_l1, line_l2, line_l3;
  213.   int pseudo;         /* flag for TPBON/TPDON:  next pixel is pseudo pixel */
  214.   int **lntp;        /* flag [plane][layer-dl] for TP: line is not typical */
  215.  
  216.   unsigned long xmax, ymax;         /* if possible abort before image gets *
  217.                      * larger than this size */
  218.   int dmax;                                      /* abort after this layer */
  219. };
  220.  
  221.  
  222. /* some macros (too trivial for a function) */
  223.  
  224. #define jbg_dec_getplanes(s)     ((s)->planes)
  225.  
  226.  
  227. /* function prototypes */
  228.  
  229. void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y,
  230.           int planes, unsigned char **p,
  231.           void (*data_out)(unsigned char *start, size_t len,
  232.                    void *file),
  233.           void *file);
  234. int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long mwidth,
  235.            unsigned long mheight);
  236. void jbg_enc_layers(struct jbg_enc_state *s, int d);
  237. int  jbg_enc_lrange(struct jbg_enc_state *s, int dl, int dh);
  238. void jbg_enc_options(struct jbg_enc_state *s, int order, int options,
  239.              long l0, int mx, int my);
  240. void jbg_enc_out(struct jbg_enc_state *s);
  241. void jbg_enc_free(struct jbg_enc_state *s);
  242.  
  243. void jbg_dec_init(struct jbg_dec_state *s);
  244. void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax,
  245.              unsigned long ymax);
  246. int  jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
  247.         size_t *cnt);
  248. long jbg_dec_getwidth(const struct jbg_dec_state *s);
  249. long jbg_dec_getheight(const struct jbg_dec_state *s);
  250. unsigned char *jbg_dec_getimage(const struct jbg_dec_state *s, int plane);
  251. long jbg_dec_getsize(const struct jbg_dec_state *s);
  252. void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
  253.               void (*data_out)(unsigned char *start, size_t len,
  254.                        void *file), void *file);
  255. long jbg_dec_getsize_merged(const struct jbg_dec_state *s);
  256. void jbg_dec_free(struct jbg_dec_state *s);
  257.  
  258. const char *jbg_strerror(int errnum, int language);
  259. void jbg_int2dppriv(unsigned char *dptable, const char *internal);
  260. void jbg_dppriv2int(char *internal, const unsigned char *dptable);
  261. unsigned long jbg_ceil_half(unsigned long x, int n);
  262. void jbg_split_planes(unsigned long x, unsigned long y, int has_planes,
  263.               int encode_planes,
  264.               const unsigned char *src, unsigned char **dest,
  265.               int use_graycode);
  266.  
  267. #endif /* JBG_H */
  268.