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.doc < prev    next >
Text File  |  2000-08-02  |  33KB  |  728 lines

  1.  
  2. Using the JBIG-KIT library
  3. --------------------------
  4.  
  5. Markus Kuhn -- 1998-04-10
  6.  
  7.  
  8. This text explains how to include the functions provided by the
  9. JBIG-KIT portable image compression library into your application
  10. software.
  11.  
  12.  
  13. 1  Introduction to JBIG
  14.  
  15. Below follows a short introduction into some technical aspects of the
  16. JBIG standard. More detailed information is provided in the
  17. "Introduction and overview" section of the JBIG standard. Information
  18. about how to obtain a copy of the standard is available on the
  19. Internet from <http://www.itu.ch/> or <http://www.iso.ch/>.
  20.  
  21. Image data encoded with the JBIG algorithm is separated into planes,
  22. layers, and stripes. Each plane contains one bit per pixel. The number
  23. of planes stored in a JBIG data stream is the number of bits per
  24. pixel. Resolution layers are numbered from 0 to D with 0 being the
  25. layer with the lowest resolution and D the layer with the highest one.
  26. Each next higher resolution layer has exactly twice the number of rows
  27. and columns than the previous one. Layer 0 is encoded independently of
  28. any other data, all other resolution layers are encoded as only the
  29. difference between the next lower and the current layer. For
  30. applications that require very quick access to parts of an image it is
  31. possible to divide an image into several horizontal stripes. All
  32. stripes of one resolution layer have equal size except perhaps the
  33. final one. The number of stripes of an image is equal in all
  34. resolution layers and in all bit planes.
  35.  
  36. The compressed data stream specified by the JBIG standard is called a
  37. bi-level image entity (BIE). A BIE consists of a 20-byte header,
  38. followed by an optional 1728-byte table (usually not present, except
  39. in special applications) followed by a sequence of stripe data
  40. entities (SDE). SDEs are the data blocks of which each encodes the
  41. content of one single stripe in one plane and resolution layer.
  42. Between the SDEs, other information blocks (called floating marker
  43. segments) can also be present, which change certain parameters of the
  44. algorithm in the middle of an image or contain additional application
  45. specific information. A BIE looks like this:
  46.  
  47.  
  48.           +------------------------------------------------+
  49.           |                                                |
  50.           |  20-byte header (with image size, #planes,     |
  51.           |  #layers, stripe size, first layer, options,   |
  52.           |  SDE ordering, ...)                            |
  53.           |                                                |
  54.           +------------------------------------------------+
  55.           |                                                |
  56.           |           optional 1728-byte table             |
  57.           |                                                |
  58.           +------------------------------------------------+
  59.           |                                                |
  60.           |              stripe data entity                |
  61.           |                                                |
  62.           +------------------------------------------------+
  63.           |                                                |
  64.           |       optional floating marker segments        |
  65.           |                                                |
  66.           +------------------------------------------------+
  67.           |                                                |
  68.           |              stripe data entity                |
  69.           |                                                |
  70.           +------------------------------------------------+
  71.             ...
  72.           +------------------------------------------------+
  73.           |                                                |
  74.           |              stripe data entity                |
  75.           |                                                |
  76.           +------------------------------------------------+
  77.  
  78.  
  79. One BIE can contain all resolution layers of an image, but it is also
  80. possible to store various resolution layers in several BIEs. The BIE
  81. header contains the number of the first and the last resolution layer
  82. stored in this BIE, as well as the size of the highest resolution
  83. layer stored in this BIE. Progressive coding is deactivated by simply
  84. storing the image in one single resolution layer.
  85.  
  86. Different applications might have different requirements for the order
  87. in which the SDEs for stripes of various planes and layers are stored
  88. in the BIE, so all possible sensible orderings are allowed and
  89. indicated by four bits in the header.
  90.  
  91. It is possible to use the raw BIE data stream as specified by the JBIG
  92. standard directly as the format of a file used for storing images.
  93. This is what the JBIG<->PBM conversion tools that are provided in this
  94. package as demonstration applications do. However as the BIE format
  95. has been designed for a large number of very different applications
  96. and also in order to allow efficient direct processing by special JBIG
  97. hardware chip implementations, the BIE header contains only the
  98. minimum amount of information absolutely required by the decompression
  99. algorithm. A large number of features expected from a good file format
  100. are missing in the BIE data stream:
  101.  
  102.   - no "magic code" in the first few bytes to allow identification
  103.     of the file on a typeless file system as JBIG encoded and to allow
  104.     automatic distinction from other compression algorithms
  105.  
  106.   - no standardized way to encode additional information like a textual
  107.     description, information about the meaning of various bit planes,
  108.     the physical size and resolution of the document, etc.
  109.  
  110.   - a checksum that ensures image integrity
  111.  
  112.   - encryption and signature mechanisms
  113.  
  114.   - many things more
  115.  
  116. Raw BIE data streams alone are consequently no suitable format for
  117. document archiving and exchange. A standard format for this purpose
  118. would typically combine a BIE representing the image data together
  119. with an additional header providing auxiliary information into one
  120. file. Existing established multi-purpose file formats with a rich set
  121. of auxiliary information attributes like TIFF could be extended easily
  122. so that they can also contain JBIG compressed data.
  123.  
  124. On the other hand, in database applications for instance, a BIE might
  125. be directly stored in a variable length field. Auxiliary information
  126. on which efficient search operations are required would then be stored
  127. in other fields of the same record.
  128.  
  129.  
  130. 2  Compressing an image
  131.  
  132. 2.1  Format of the source image
  133.  
  134. For processing by the library, the image has to be present in memory
  135. as separate bitmap planes. Each byte of a bitmap contains eight
  136. pixels, the most significant bit represents the leftmost of these
  137. pixels. Each line of a bitmap has to be stored in an integral number
  138. of bytes. If the image width is not an integral multiple of eight,
  139. then the final byte has to be padded with zero bits.
  140.  
  141. For example the 23x5 pixels large single plane image:
  142.  
  143.    .XXXXX..XXX...X...XXX..
  144.    .....X..X..X..X..X.....
  145.    .....X..XXX...X..X.XXX.
  146.    .X...X..X..X..X..X...X.
  147.    ..XXX...XXX...X...XXX..
  148.  
  149. is represented by the 15 bytes
  150.  
  151.    01111100 11100010 00111000
  152.    00000100 10010010 01000000
  153.    00000100 11100010 01011100
  154.    01000100 10010010 01000100
  155.    00111000 11100010 00111000
  156.  
  157. or in hexadecimal notation
  158.  
  159.    7c e2 38 04 92 40 04 e2 5c 44 92 44 38 e2 38
  160.  
  161. This is the format used in binary PBM files and it can also be be
  162. handled directly by the Xlib library of the X Window System.
  163.  
  164. As JBIG can also handle images with several bit planes, the JBIG-KIT
  165. library functions accept and return always arrays of pointers to
  166. bitmaps with one pointer per plane.
  167.  
  168. For single plane images, the standard recommends that a 0 pixel
  169. represents the background and a 1 pixel represents the foreground
  170. color of an image, i.e. 0 is white and 1 is black for scanned paper
  171. documents. For images with several bits per pixel, the JBIG standard
  172. makes no recommendations about how various colors should be encoded.
  173.  
  174. For greyscale images, by using a Gray code instead of a simple binary
  175. weighted representation of the pixel intensity, some increase in
  176. coding efficiency can be reached.
  177.  
  178. A Gray code is also a binary representation of integer numbers, but
  179. has the property that the representations of the integer numbers i and
  180. (i+1) differ always in exactly one single bit. For example, the
  181. numbers 0 to 7 can be represented in normal binary code and Gray code
  182. as in the following table:
  183.  
  184.                            normal
  185.               number    binary code     Gray code
  186.             ---------------------------------------
  187.                 0           000            000
  188.                 1           001            001
  189.                 2           010            011
  190.                 3           011            010
  191.                 4           100            110
  192.                 5           101            111
  193.                 6           110            101
  194.                 7           111            100
  195.  
  196. The form of Gray code shown above has the property that the second
  197. half of the code (numbers 4 - 7) is simply the mirrored first half
  198. (numbers 3 - 0) with the first bit set to one. This way, arbitrarily
  199. large Gray codes can be generated quickly by mirroring the above
  200. example and prefixing the first half with zeros and the second half
  201. with ones as often as required. In greyscale images, it is common
  202. practise to use the all-0 code for black and the all-1 code for white.
  203.  
  204. No matter whether a Gray code or a binary code is used for encoding a
  205. pixel intensity in several bit planes, it always makes sense to store
  206. the most significant (leftmost) bit in plane 0, which is transmitted
  207. first. This way, a decoder could increase the precision of the
  208. displayed pixel intensities while data is still being received and the
  209. basic structure of the image will become visible as early as possible
  210. during the transmission.
  211.  
  212.  
  213. 2.2  A simple compression application
  214.  
  215. In order to use JBIG-KIT in your application, just link libjbig.a to
  216. your executable (on Unix systems just add -ljbig and -L. to the
  217. command line options of your compiler, on other systems you will have
  218. to write a new Makefile anyway), copy the file jbig.h into your source
  219. directory and put the line
  220.  
  221.   #include "jbig.h"
  222.  
  223. into your source code.
  224.  
  225. The library interface follows the concepts of object-oriented
  226. programming. You have to declare a variable (object)
  227.  
  228.   struct jbg_enc_state se;
  229.  
  230. which contains the current status of an encoder. Then you initialize
  231. the encoder by calling the constructor function
  232.  
  233.   void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y,
  234.                     int pl, unsigned char **p,
  235.                     void (*data_out)(unsigned char *start, size_t len,
  236.                                      void *file),
  237.                     void *file);
  238.  
  239. The parameters have the following meaning:
  240.  
  241.   s             A pointer to the jbg_enc_state structure which you want
  242.                 to initialize.
  243.  
  244.   x             The width of your image.
  245.  
  246.   y             The height of your image.
  247.  
  248.   pl            the number of bitmap planes you want to encode.
  249.  
  250.   p             A pointer to an array of pl pointers, where each is again
  251.                 pointing to the first byte of a bitmap as described in
  252.                 section 2.1.
  253.  
  254.   data_out      This is a call-back function which will be called during
  255.                 the compression process by libjbig in order to deliver
  256.                 the BIE data to the application. The parameters of the
  257.                 function data_out are a pointer start to the new block of
  258.                 data to be delivered as well as the number len of delivered
  259.                 bytes. The pointer file is transparently delivered to
  260.                 data_out as specified in jbg_enc_init(). Usually, data_out
  261.                 will write the BIE portion to a file, send it to a
  262.                 network connection or append it to some memory buffer.
  263.  
  264.   file          A pointer parameter which is transparently passed to
  265.                 data_out() and allows data_out() to distinguish by which
  266.                 compression task it has been called in multi-threaded
  267.                 applications.
  268.  
  269. In the simplest case, the compression is then started by calling the
  270. function
  271.  
  272.   void jbg_enc_out(struct jbg_enc_state *s);
  273.  
  274. which will deliver the complete BIE to data_out(). After this, a call
  275. to the destructor function
  276.  
  277.   void jbg_enc_free(struct jbg_enc_state *s);
  278.  
  279. will release any memory allocated by the previous functions.
  280.  
  281.  
  282. A minimal example application which sends the BIE of the above example
  283. bitmap to stdout looks like this:
  284.  
  285. ---------------------------------------------------------------------------
  286. /* A sample JBIG encoding application */
  287.  
  288. #include <stdio.h>
  289. #include "jbig.h"
  290.  
  291. void output_bie(unsigned char *start, size_t len, void *file)
  292. {
  293.   fwrite(start, 1, len, (FILE *) file);
  294.   
  295.   return;
  296. }
  297.  
  298. int main()
  299. {
  300.   unsigned char bitmap[15] = {
  301.     /* 23 x 5 pixels, "JBIG" */
  302.     0x7c, 0xe2, 0x38, 0x04, 0x92, 0x40, 0x04, 0xe2,
  303.     0x5c, 0x44, 0x92, 0x44, 0x38, 0xe2, 0x38
  304.   };
  305.   unsigned char *bitmaps[1] = { bitmap };
  306.   struct jbg_enc_state se;
  307.   
  308.   jbg_enc_init(&se, 23, 5, 1, bitmaps, 
  309.            output_bie, stdout);              /* initialize encoder */
  310.   jbg_enc_out(&se);                                    /* encode image */
  311.   jbg_enc_free(&se);                    /* release allocated resources */
  312.   
  313.   return 0;
  314. }
  315. ---------------------------------------------------------------------------
  316.  
  317. This software produces a 42 byte long BIE. (JBIG is not very good at
  318. compressing extremely small images like in this example, because the
  319. arithmetic encoder requires some startup data in order to generate
  320. reasonable statistics which influence the compression process and
  321. because there is some header overhead.)
  322.  
  323.  
  324. 2.3  More about compression
  325.  
  326. If jbg_enc_out() is called directly after jbg_enc_init(), the
  327. following default values are used for various compression parameters:
  328.  
  329.   - Only one single resolution layer is used, i.e. no progressive
  330.     mode.
  331.  
  332.   - The number of lines per stripe is selected so that approximately
  333.     35 stripes per image are used (as recommended in annex C of the
  334.     standard together with the suggested adaptive template change
  335.     algorithm). However not less than 2 and not more than 128 lines
  336.     are used in order to stay within the suggested minimum parameter
  337.     support range specified in annex A of the standard).
  338.  
  339.   - All optional parts of the JBIG algorithm are activated (TPBON,
  340.     TPDON and DPON).
  341.  
  342.   - The default resolution reduction table and the default deterministic
  343.     prediction tables are used
  344.  
  345.   - The maximal vertical offset of the adaptive template pixel is 0
  346.     and the maximal horizontal offset is 8 (mx = 8, my = 0).
  347.  
  348. In order to change any of these default parameters, additional
  349. functions have to be called between jbg_enc_init() and jbg_enc_out().
  350.  
  351. In order to activate progressive encoding, it is possible to specify
  352. with 
  353.  
  354.   void jbg_enc_layers(struct jbg_enc_state *s, int d);
  355.  
  356. the number d of differential resolution layers which shall be encoded
  357. in addition to the lowest resolution layer 0. For example, if a 300
  358. dpi document has to be stored and the lowest resolution layer shall
  359. have 75 dpi so that a screen previewer can directly decompress only
  360. the required resolution, then a call
  361.  
  362.   jbg_enc_layers(&se, 2);
  363.  
  364. will cause three resolution layers with 75, 150 and 300 dots per inch.
  365.  
  366. If the application does not know what typical resolutions are used and
  367. simply wants to ensure that the lowest resolution layer will fit into
  368. a given maximal window size, then as an alternative, a call to
  369.  
  370.   int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long mwidth,
  371.                      unsigned long mheight);
  372.  
  373. will cause the library to automatically determine the suitable number
  374. of resolutions so that the lowest resolution layer 0 will not be
  375. larger than mwidth x mheight pixels. E.g. if one wants to ensure that
  376. systems with a 640 x 480 pixel large screen can decode the required
  377. resolution directly, then call
  378.  
  379.   jbg_enc_lrlmax(&se, 640, 480);
  380.  
  381. The return value is the number of differential layers selected.
  382.  
  383. After the number of resolution layers has been specified by calls to
  384. jbg_enc_layers() or jbg_enc_lrlmax(), by default all these layers will
  385. be written into the BIE. This can be changed with a call to
  386.  
  387.   int  jbg_enc_lrange(struct jbg_enc_state *s, int dl, int dh);
  388.  
  389. Parameter dl specifies the lowest resolution layer and dh the highest
  390. resolution layer that will appear in the BIE. If e.g. only layer 0
  391. shall be written to the first BIE and layer 1 and 2 shall be written
  392. to a second one, then before writing the first BIE, one calls
  393.  
  394.   jbg_enc_lrange(&se, 0, 0);
  395.  
  396. and before writing the second BIE with jbg_enc_out(), one calls
  397.  
  398.   jbg_enc_lrange(&se, 1, 2);
  399.  
  400. If any of the parameters is negative, it will be ignored. The return
  401. value is the total number of differential layers which will represent
  402. the input image. This way, jbg_enc_lrange(&se, -1, -1) can be used to
  403. query the layer of the full image.
  404.  
  405. A number of other more exotic options of the JBIG algorithm can be
  406. modified by calling
  407.  
  408.   void jbg_enc_options(struct jbg_enc_state *s, int order, int options,
  409.                        long l0, int mx, int my);
  410.  
  411. before calling jbg_enc_out().
  412.  
  413. The order parameter can be a combination of the bits JBG_HITOLO,
  414. JBG_SEQ, JBG_ILEAVE and JBG_SMID and it determines in which order
  415. the SDEs are stored in the BIE. The bits have the following meaning:
  416.  
  417.   JBG_HITOLO   Usually, the lower resolution layers are stored before
  418.                the higher resolution layers, so that a decoder can
  419.                already start to display a low resolution version of
  420.                the full image once a prefix of the BIE has been
  421.                received. When this bit is set however, the BIE will
  422.                contain the higher layers before the lower layers. This
  423.                avoids additional buffer memory in the encoder and is
  424.                intended for applications where the encoder is connected
  425.                to a database which can easily reorder the SDEs before
  426.                sending them to a decoder. Warning: JBIG decoders are
  427.                not expected to support the HITOLO option (e.g. the
  428.                JBIG-KIT decoder does currently not) so you should
  429.                normally not use it.
  430.  
  431.   JBG_SEQ      Usually, at first all stripes of one resolution layer
  432.                are written to the BIE and then all stripes of the next
  433.                layer, and so on. When the SEQ bit is set however, then
  434.                all layers of the first stripe will be written,
  435.                followed by all layers of the second stripe, etc. This
  436.                option also should normally never be required and is
  437.                not supported by the current JBIG-KIT decoder.
  438.  
  439.   JBG_SMID     In case there exist several bit planes, then the order of
  440.                the stripes is determined by 3 loops over all stripes,
  441.                all planes and all layers. When SMID is set, the loop
  442.                over all stripes is the middle loop.
  443.  
  444.   JBG_ILEAVE   If this bit is set, then at first all layers of one
  445.                plane are written before the encoder starts with the next
  446.                plane.
  447.  
  448. The above description might be somewhat confusing, but the following
  449. table (see also Table 11 in ITU-T T.82) makes clear how the three bits
  450. JBG_SEQ, JBIG_ILEAVE and JBG_SMID influence the ordering of the loops
  451. over all stripes, planes and layers:
  452.  
  453.  
  454.                                                  Loops:
  455.     JBG_SEQ   JBG_ILEAVE   JBG_SMID   |  Outer   Middle    Inner
  456.   ------------------------------------+---------------------------
  457.        0           0           0      |    p        d        s
  458.        0           1           0      |    d        p        s
  459.        0           1           1      |    d        s        p
  460.        1           0           0      |    s        p        d
  461.        1           0           1      |    p        s        d
  462.        1           1           0      |    s        d        p
  463.  
  464.                                        p: plane, s: stripe, d: layer
  465.  
  466.  
  467. By default, the order combination JBG_ILEAVE | JBG_SMID is used.
  468.  
  469. The options value can contain the following bits, which activate
  470. some of the optional algorithms defined by JBIG:
  471.  
  472.   JBG_LRLTWO     Normally, in the lowest resolution layer, pixels
  473.                  from three lines around the next pixel are used
  474.                  in order to determine the context in which the next
  475.                  pixel is encoded. Some people in the JBIG committee
  476.                  seem to have argued that using only 2 lines will
  477.                  make software implementations a little bit faster,
  478.                  however others have argued that using only two lines
  479.                  will decrease compression efficiency by around 5%.
  480.                  As you might expect from a committee, now both
  481.                  alternatives are allowed and if JBG_LRLTWO is set,
  482.                  the slightly faster but 5% less well compressing two
  483.                  line alternative is selected. God bless the committees.
  484.                  Although probably nobody will ever need this option,
  485.                  it has been implemented in JBIG-KIT and is off by
  486.                  default.
  487.  
  488.   JBG_TPDON      This activates the "typical prediction" algorithm
  489.                  for differential layers which avoids that large
  490.                  areas of equal color have to be encoded at all.
  491.                  This is on by default and there is no good reason to
  492.                  switch it off except for debugging or preparing data
  493.                  for cheap JBIG hardware which does not support this
  494.                  option.
  495.  
  496.   JBG_TPBON      Like JBG_TPDON this activates the "typical prediction"
  497.                  algorithm in the lowest resolution layer. Also activated
  498.                  by default.
  499.  
  500.   JBG_DPON       This bit activates for the differential resolution
  501.                  layers the "deterministic prediction" algorithm,
  502.                  which avoids that higher resolution layer pixels are
  503.                  encoded when their value can already be determined
  504.                  with the knowledge of the neighbor pixels, the
  505.                  corresponding lower resolution pixels and the
  506.                  resolution reduction algorithm. This is also
  507.                  activated by default and one only might perhaps want
  508.                  to deactivate it if the default resolution reduction
  509.                  algorithm is replaced by a new one.
  510.  
  511.   JBG_DELAY_AT   Use a slightly less efficient algorithm to determine
  512.                  when an adaptive template change is necessary. With
  513.                  this bit set, the encoder output is compatible to the
  514.                  conformance test examples in cause 7.2 of ITU-T T.82.
  515.                  Then all adaptive template changes are delayed until
  516.                  the first line of the next stripe. This option is by
  517.                  default deactivated and only required for passing a
  518.                  special compatibility test suite.
  519.  
  520. In addition, parameter l0 in jbg_enc_options() allows you to specify
  521. the number of lines per stripe in resolution layer 0. The parameters
  522. mx and my change the maximal offset allowed for the adaptive template
  523. pixel. The JBIG-KIT implementation allows currently a maximal mx value
  524. of 23 in the encoder and 32 in the decoder. Parameter my is at the
  525. moment ignored and always set to 0. As the standard requires of all
  526. decoder implementations only a maximum supported mx = 16 and my = 0,
  527. higher values should normally be avoided in order to guarantee
  528. interoperability. Default is mx = 8 and my = 0. If any of the
  529. parameters order, options, l0, mx or my is negative, then this value
  530. is ignored and the current value stays unmodified.
  531.  
  532. The resolution reduction and deterministic prediction tables can also
  533. be replaced. However as these options are anyway only for experts,
  534. please have a look at the source code of jbg_enc_out() and the struct
  535. members dppriv and res_tab of struct jbg_enc_state for the details of
  536. how to do this in case you really need it. The functions
  537. jbg_int2dppriv and jbg_dppriv2int are provided in order to convert the
  538. DPTABLE data from the format used in the standard into the more
  539. efficient format used internally by JBIG-KIT.
  540.  
  541. If you want to encode a greyscale image, you can use the library
  542. function
  543.  
  544.   void jbg_split_planes(unsigned long x, unsigned long y, int has_planes,
  545.                         int encode_planes,
  546.                         const unsigned char *src, unsigned char **dest,
  547.                         int use_graycode);
  548.  
  549. It separates an image in which each pixel is represented by one or
  550. more bytes into separate bitplanes. The dest array of pointers to
  551. these bitplanes can then be handed over to jbg_enc_init(). The
  552. variables x and y specify the width and height of the image in pixels,
  553. and has_planes specifies how many bits per pixel are used. As each
  554. pixel is represented by an integral number of consecutive bytes, of
  555. which each contains up to eight bits, the total length of the input
  556. image array src[] will therefore be x * y * ((has_planes + 7) / 8)
  557. bytes. The pixels are stored as usually in English reading order, and
  558. for each pixel the integer value is stored with the most significant
  559. byte coming first (Bigendian). This is exactly the format used in raw
  560. PGM files. In encode_planes, the number of bitplanes that shall be
  561. extracted can be specified. This allows for instance to extract only
  562. the most significant 8 bits of a 12-bit image, where each pixel is
  563. represented by two bytes, by specifying has_planes = 12 and
  564. encode_planes = 8. If use_graycode is zero, then the binary code of
  565. the pixel integer values will be used instead of the Gray code. Plane
  566. 0 contains always the most significant bit.
  567.  
  568.  
  569. 3  Decompressing an image
  570.  
  571. Like with the compression functions, if you want to use the JBIG-KIT
  572. library, you have to put the line
  573.  
  574.   #include "jbig.h"
  575.  
  576. into your source code and link your executable with libjbig.a.
  577.  
  578. The state of a JBIG decoder is stored completely in a struct and you
  579. will have to define a variable like
  580.  
  581.   struct jbg_dec_state sd;
  582.  
  583. which is initialized by a call to
  584.  
  585.   void jbg_dec_init(struct jbg_dec_state *s);
  586.  
  587. After this, you can directly start to pass data from the BIE to the decoder
  588. by calling the function
  589.  
  590.   int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
  591.                  size_t *cnt);
  592.  
  593. The pointer data points to the first byte of a data block with length
  594. len, which contains bytes from a BIE. It is not necessary to pass a
  595. whole BIE at once to jbg_dec_in(), it can arrive fragmented in any way
  596. by calling jbg_dec_in() several times. It is also possible to send
  597. several BIEs concatenated to jbg_dec_in(), however these then have to
  598. fit together. If you send several BIEs to the decoder, the lowest
  599. resolution layer in each following BIE has to be the highest
  600. resolution layer in the previous BIE plus one and the image sizes and
  601. number of planes also have to fit together, otherwise jbg_dec_in()
  602. will return the error JBG_ENOCONT after the header of the new BIE has
  603. been received completely.
  604.  
  605. If pointer cnt is not NULL, then the number of bytes actually read
  606. from the data block is stored there. In case the data block did not
  607. contain the end of the BIE, then the value JBG_EAGAIN will be returned
  608. and *cnt equals len.
  609.  
  610. Once the end of a BIE has been reached, the return value of
  611. jbg_dec_in() will be JBG_EOK. After this has happened, the functions
  612. and macros
  613.  
  614.   long jbg_dec_getwidth(struct jbg_dec_state *s);
  615.   long jbg_dec_getheight(struct jbg_dec_state *s);
  616.   int jbg_dec_getplanes(struct jbg_dec_state *s);
  617.   unsigned char *jbg_dec_getimage(struct jbg_dec_state *s, int plane);
  618.   long jbg_dec_getsize(struct jbg_dec_state *s);
  619.  
  620. can be used to query the dimensions of the now completely decoded
  621. image and to get a pointer to all bitmap planes. The bitmaps are
  622. stored as described in section 2.1. The function jbg_dec_getsize()
  623. calculates the number of bytes which one bitmap requires.
  624.  
  625. The function
  626.  
  627.   void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
  628.                             void (*data_out)(unsigned char *start, size_t len,
  629.                                              void *file), void *file);
  630.  
  631. allows you to merge the bitplanes that can be accessed individually
  632. with jbg_dec_getimage() into an array with one or more bytes per pixel
  633. (i.e., the format provided to jbg_split_planes()). If use_graycode is
  634. zero, then a binary encoding will be used. The output array will be
  635. delivered via the callback function data_out, exactly in the same way
  636. in which the encoder provides the BIE. The function
  637.  
  638.   long jbg_dec_getsize_merged(const struct jbg_dec_state *s);
  639.  
  640. determines how long the data array delivered by jbg_dec_merge_planes()
  641. is going to be.
  642.  
  643. Before calling jbg_dec_in() the first time, it is possible to specify with
  644. a call to
  645.  
  646.   void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax,
  647.                        unsigned long ymax);
  648.  
  649. an abort criterion for progressively encoded images. For instance if an
  650. application will display a whole document on a screen which is 1024 x
  651. 768 pixels large, then this application should call
  652.  
  653.   jbg_dec_maxsize(&sd, 1024, 768);
  654.  
  655. before the decoding process starts. If the image has been encoded in
  656. progressive mode (i.e. with several resolution layers), then the
  657. decoder will stop with a return value JBG_EOK_INTR after the largest
  658. resolution layer that is still smaller than 1024 x 768. However this
  659. is no guarantee that the image which can then be read out using
  660. jbg_dec_getimage(), etc. is really not larger than the specified
  661. maximal size. The application will have to check the size of the
  662. image, because the decoder does not automatically apply a resolution
  663. reduction if no suitable resolution layer is available in the BIE.
  664.  
  665. If jbg_dec_in() returned JBG_EOK_INTR or JBG_EOK, then it is possible
  666. to continue calling jbg_dec_in() with the remaining data in order to
  667. either decode the remaining resolution layers of the current BIE or in
  668. order to add another BIE with additional resolution layers. In both
  669. cases, after jbg_dec_in() returned JBG_EOK_INTR or JBG_EOK, *cnt is
  670. probably not equal to len and the remainder of the data block which
  671. has not yet been processed by the decoder has to be delivered to
  672. jbg_dec_in() again.
  673.  
  674. If any other return value than JBG_EOK, JBG_EOK_INTR or JBG_EAGAIN
  675. has been returned by jbg_dec_in(), then an error has occurred and
  676.  
  677.   void jbg_dec_free(struct jbg_dec_state *s);
  678.  
  679. should be called in order to release any allocated memory. The
  680. destructor jbg_dec_free() should of course also be called, once the
  681. decoded bitmap returned by jbg_dec_getimage() is no longer required
  682. and the memory can be released.
  683.  
  684. The function
  685.  
  686.   const char *jbg_strerror(int errnum, int language);
  687.  
  688. returns a pointer to a short single line test message which explains
  689. the return value of jbg_dec_in(). This message can be used in order to
  690. provide the user a brief informative message about what when wrong
  691. while decompressing the JBIG image. The error messages are available
  692. in several languages and in several character sets. Currently
  693. supported are the following values for the language parameter:
  694.  
  695.   JBG_EN              English messages in ASCII
  696.   JBG_DE_8859_1       German messages in ISO 8859-1 Latin 1 character set
  697.   JBG_DE_UTF_8        German messages in ISO 10646/Unicode UTF-8 encoding
  698.  
  699.  
  700. The current implementation of the JBIG-KIT decoder has the following
  701. limitations:
  702.  
  703.   - The maximal horizontal offset mx of the adaptive template pixel
  704.     must not be larger than 32 and the maximal vertical offset must
  705.     be zero.
  706.  
  707.   - HITOLO and SEQ bits must not be set in the order value.
  708.  
  709.   - The image size initially indicated in the BIE header is used to
  710.     allocate memory for a bitmap of this size. This means that BIEs
  711.     that set initially Y_D = 0xffffffff (as suggested in ITU-T T.85
  712.     for fax machines that do not know the height of the page at the
  713.     start of the transmission) cannot be decoded by this version.
  714.  
  715. A more detailed description of the JBIG-KIT implementation is
  716.  
  717.   Markus Kuhn: Effiziente Kompression von bi-level Bilddaten durch
  718.   kontextsensitive arithmetische Codierung. Studienarbeit, Lehrstuhl
  719.   fⁿr Betriebssysteme, IMMD IV, UniversitΣt Erlangen-Nⁿrnberg,
  720.   Erlangen, July 1995. (German, 62 pages)
  721.   <http://www.cl.cam.ac.uk/~mgk25/kuhn-sta.pdf>
  722.  
  723. Please quote the above if you use JBIG-KIT in your research project.
  724.  
  725. *** Happy compressing ***
  726.  
  727. [end]
  728.