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