source: trunk/libdjvu/JB2Image.h @ 206

Last change on this file since 206 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

File size: 31.8 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: JB2Image.h,v 1.11 2007/03/25 20:48:32 leonb Exp $
57// $Name: release_3_5_19 $
58
59#ifndef _JB2IMAGE_H
60#define _JB2IMAGE_H
61#ifdef HAVE_CONFIG_H
62#include "config.h"
63#endif
64#if NEED_GNUG_PRAGMAS
65# pragma interface
66#endif
67
68/** @name JB2Image.h
69
70    Files #"JB2Image.h"# and #"JB2Image.cpp"# address the compression of
71    bilevel images using the JB2 soft pattern matching scheme.  These files
72    provide the complete decoder and the decoder back-end.  The JB2 scheme is
73    optimized for images containing a large number of self-similar small
74    components such as characters.  Typical text images can be compressed into
75    files 3 to 5 times smaller than with G4/MMR and 2 to 4 times smaller than
76    with JBIG1.
77
78    {\bf JB2 and JBIG2} --- JB2 has strong similarities with the forthcoming
79    JBIG2 standard developed by the "ISO/IEC JTC1 SC29 Working Group 1" which
80    is responsible for both the JPEG and JBIG standards.  This is hardly
81    surprising since JB2 was our own proposal for the JBIG2 standard
82    and remained the only proposal for years.  The full JBIG2 standard however
83    is significantly more complex and slighlty less efficient than JB2 because
84    it addresses a broader range of applications.  Full JBIG2 compliance may
85    be implemented in the future.
86
87    {\bf JB2 Images} --- Class \Ref{JB2Image} is the central data structure
88    implemented here.  A #JB2Image# is composed of an array of shapes
89    and an array of blits.  Each shape contains a small bitmap representing an
90    elementary blob of ink, such as a character or a segment of line art.
91    Each blit instructs the decoder to render a particular shape at a
92    specified position in the image.  Some compression is already achieved
93    because several blits can refer to the same shape.  A shape can also
94    contain a pointer to a parent shape.  Additional compression is achieved
95    when both shapes are similar because each shape is encoded using the
96    parent shape as a model.  A #"O"# shape for instance could be a parent for
97    both a #"C"# shape and a #"Q"# shape.
98
99    {\bf JB2 Dictionary} --- Class \Ref{JB2Dict} is a peculiar kind of
100    JB2Image which only contains an array of shapes.  These shapes can be
101    referenced from another JB2Dict/JB2Image.  This is arranged by setting the
102    ``inherited dictionary'' of a JB2Dict/JB2Image using function
103    \Ref{JB2Dict::set_inherited_dict}. Several JB2Images can use shapes from a
104    same JB2Dict encoded separately.  This is how several pages of a same
105    document can share information.
106   
107    {\bf Decoding JB2 data} --- The first step for decoding JB2 data consists of
108    creating an empty #JB2Image# object.  Function \Ref{JB2Image::decode} then
109    reads the data and populates the #JB2Image# with the shapes and the blits.
110    Function \Ref{JB2Image::get_bitmap} finally produces an anti-aliased image.
111
112    {\bf Encoding JB2 data} --- The first step for decoding JB2 data also
113    consists of creating an empty #JB2Image# object.  You must then use
114    functions \Ref{JB2Image::add_shape} and \Ref{JB2Image::add_blit} to
115    populate the #JB2Image# object.  Function \Ref{JB2Image::encode} finally
116    produces the JB2 data.  Function #encode# sequentially encodes the blits
117    and the necessary shapes.  The compression ratio depends on several
118    factors:
119    \begin{itemize}
120    \item Blits should reuse shapes as often as possible.
121    \item Blits should be sorted in reading order because this facilitates
122          the prediction of the blit coordinates.
123    \item Shapes should be sorted according to the order of first appearance
124          in the sequence of blits because this facilitates the prediction of the
125          shape indices.
126    \item Shapes should be compared to all previous shapes in the shape array.
127          The shape parent pointer should be set to a suitable parent shape if
128          such a parent shape exists.  The parent shape should have almost the
129          same size and the same pixels.
130    \end{itemize}
131    All this is quite easy to achieve in the case of an electronically
132    produced document such as a DVI file or a PS file: we know what the
133    characters are and where they are located.  If you only have a scanned
134    image however you must first locate the characters (connected component
135    analysis) and cut the remaining pieces of ink into smaller blobs.
136    Ordering the blits and matching the shapes is then an essentially
137    heuristic process.  Although the quality of the heuristics substantially
138    effects the file size, misordering blits or mismatching shapes never
139    effects the quality of the image.  The last refinement consists in
140    smoothing the shapes in order to reduce the noise and maximize the
141    similarities between shapes.
142   
143    {\bf JB2 extensions} --- Two extensions of the JB2
144    encoding format have been introduced with DjVu files version 21. The first
145    extension addresses the shared shape dictionaries. The second extension
146    bounds the number of probability contexts used for coding numbers.
147    Both extensions maintain backward compatibility with JB2 as
148    described in the ICFDD proposal. A more complete discussion
149    can be found in section \Ref{JB2 extensions for version 21.}.
150
151    {\bf References}
152    \begin{itemize}
153    \item Paul G. Howard : {\em Text image compression using soft
154          pattern matching}, Computer Journal, volume 40:2/3, 1997.
155    \item JBIG1 : \URL{http://www.jpeg.org/public/jbighomepage.htm}.
156    \item JBIG2 draft : \URL{http://www.jpeg.org/public/jbigpt2.htm}.
157    \item ICFDD Draft Proposed American National Standard, 1999-08-26.
158    \end{itemize}
159
160    @version
161    #$Id: JB2Image.h,v 1.11 2007/03/25 20:48:32 leonb Exp $#
162    @memo
163    Coding bilevel images with JB2.
164    @author
165    Paul Howard <pgh@research.att.com> -- JB2 design\\
166    L\'eon Bottou <leonb@research.att.com> -- this implementation
167
168// From: Leon Bottou, 1/31/2002
169// Lizardtech has split the corresponding cpp file into a decoder and an encoder.
170// Only superficial changes.  The meat is mine.
171
172*/
173//@{
174
175
176#include "GString.h"
177#include "ZPCodec.h"
178
179
180#ifdef HAVE_NAMESPACES
181namespace DJVU {
182# ifdef NOT_DEFINED // Just to fool emacs c++ mode
183}
184#endif
185#endif
186
187class JB2Dict;
188class JB2Image;
189class GRect;
190class GBitmap;
191class ByteStream;
192
193/** Blit data structure.  A #JB2Image# contains an array of #JB2Blit# data
194    structures.  Each array entry instructs the decoder to render a particular
195    shape at a particular location.  Members #left# and #bottom# specify the
196    coordinates of the bottom left corner of the shape bitmap.  All
197    coordinates are relative to the bottom left corner of the image.  Member
198    #shapeno# is the subscript of the shape to be rendered.  */
199
200class JB2Blit {
201public:
202  /** Horizontal coordinate of the blit. */
203  unsigned short left;
204  /** Vertical coordinate of the blit. */
205  unsigned short bottom;
206  /** Index of the shape to blit. */
207  unsigned int shapeno;
208};
209
210
211/** Shape data structure.  A #JB2Image# contains an array of #JB2Shape# data
212    structures.  Each array entry represents an elementary blob of ink such as
213    a character or a segment of line art.  Member #bits# points to a bilevel
214    image representing the shape pixels.  Member #parent# is the subscript of
215    the parent shape.  */
216
217class JB2Shape
218{ 
219public: 
220  /** Subscript of the parent shape.  The parent shape must always be located
221      before the current shape in the shape array.  A negative value indicates
222      that this shape has no parent.  Any negative values smaller than #-1#
223      further indicates that this shape does not look like a character.  This
224      is used to enable a few internal optimizations.  This information is
225      saved into the JB2 file, but the actual value of the #parent# variable
226      is not. */
227  int parent; 
228  /** Bilevel image of the shape pixels.  This must be a pointer to a bilevel
229      #GBitmap# image.  This pointer can also be null. The encoder will just
230      silently discard all blits referring to a shape containing a null
231      bitmap. */
232  GP<GBitmap> bits;
233  /** Private user data. This long word is provided as a convenience for users
234      of the JB2Image data structures.  Neither the rendering functions nor
235      the coding functions ever access this value. */
236  long userdata;
237};
238
239
240
241/** JB2 Dictionary callback.
242    The decoding function call this callback function when they discover that
243    the current JB2Image or JB2Dict needs a pre-existing shape dictionary.
244    The callback function must return a pointer to the dictionary or NULL
245    if none is found. */
246
247typedef GP<JB2Dict> JB2DecoderCallback ( void* );
248
249
250/** Dictionary of JB2 shapes. */
251
252class JB2Dict : public GPEnabled
253{
254protected:
255  JB2Dict(void);
256public:
257  class JB2Codec;
258
259  // CONSTRUCTION
260  /** Default creator.  Constructs an empty #JB2Dict# object.  You can then
261      call the decoding function #decode#.  You can also manually set the
262      image size using #add_shape#. */
263  static GP<JB2Dict> create(void);
264
265  // INITIALIZATION
266  /** Resets the #JB2Image# object.  This function reinitializes both the shape
267     and the blit arrays.  All allocated memory is freed. */
268  void init(void);
269
270  // INHERITED
271  /** Returns the inherited dictionary. */
272  GP<JB2Dict> get_inherited_dict(void) const;
273  /** Returns the number of inherited shapes. */
274  int get_inherited_shape_count(void) const;
275  /** Sets the inherited dictionary. */
276  void set_inherited_dict(const GP<JB2Dict> &dict);
277
278  // ACCESSING THE SHAPE LIBRARY
279  /** Returns the total number of shapes.
280      Shape indices range from #0# to #get_shape_count()-1#. */
281  int get_shape_count(void) const;
282  /** Returns a pointer to shape #shapeno#.
283      The returned pointer directly points into the shape array.
284      This pointer can be used for reading or writing the shape data. */
285  JB2Shape &get_shape(const int shapeno);
286  /** Returns a constant pointer to shape #shapeno#.
287      The returned pointer directly points into the shape array.
288      This pointer can only be used for reading the shape data. */
289  const JB2Shape &get_shape(const int shapeno) const;
290  /** Appends a shape to the shape array.  This function appends a copy of
291      shape #shape# to the shape array and returns the subscript of the new
292      shape.  The subscript of the parent shape #shape.parent# must
293      actually designate an already existing shape. */
294  int  add_shape(const JB2Shape &shape);
295
296  // MEMORY OPTIMIZATION
297  /** Compresses all shape bitmaps.  This function reduces the memory required
298      by the #JB2Image# by calling \Ref{GBitmap::compress} on all shapes
299      bitmaps.  This function is best called after decoding a #JB2Image#,
300      because function \Ref{get_bitmap} can directly use the compressed
301      bitmaps.  */
302  void compress(void);
303  /** Returns the total memory used by the JB2Image.
304      The returned value is expressed in bytes. */
305  unsigned int get_memory_usage(void) const;
306
307  // CODING
308  /** Encodes the JB2Dict into ByteStream #bs#. 
309      This function generates the JB2 data stream without any header.   */
310  void encode(const GP<ByteStream> &gbs) const;
311  /** Decodes JB2 data from ByteStream #bs#. This function decodes the image
312      size and populates the shape and blit arrays.  The callback function
313      #cb# is called when the decoder determines that the ByteStream data
314      requires a shape dictionary which has not been set with
315      \Ref{JB2Dict::set_inherited_dict}. The callback receives argument #arg#
316      and must return a suitable dictionary which will be installed as the
317      inherited dictionary.  The callback should return null if no such
318      dictionary is found. */
319  void decode(const GP<ByteStream> &gbs, JB2DecoderCallback *cb=0, void *arg=0);
320
321 
322public:
323  /** Comment string coded by JB2 file. */
324  GUTF8String comment;
325
326
327private:
328  int inherited_shapes;
329  GP<JB2Dict> inherited_dict;
330  GArray<JB2Shape> shapes;
331
332  struct LibRect {
333    int top,left,right,bottom;
334    void compute_bounding_box(const GBitmap &cbm);
335  };
336  GTArray<LibRect> boxes;
337  void get_bounding_box(int shapeno, LibRect &dest);
338};
339
340/** Main JB2 data structure.  Each #JB2Image# consists of an array of shapes
341    and an array of blits.  These arrays can be populated by hand using
342    functions \Ref{add_shape} and \Ref{add_blit}, or by decoding JB2 data
343    using function \Ref{decode}.  You can then use function \Ref{get_bitmap}
344    to render anti-aliased images, or use function \Ref{encode} to generate
345    JB2 data. */
346
347class JB2Image : public JB2Dict
348{
349protected:
350  JB2Image(void);
351public:
352
353  /** Creates an empty #JB2Image# object.  You can then
354      call the decoding function #decode#.  You can also manually set the
355      image size using #set_dimension# and populate the shape and blit arrays
356      using #add_shape# and #add_blit#. */
357  static GP<JB2Image> create(void) { return new JB2Image(); }
358
359  // INITIALIZATION
360  /** Resets the #JB2Image# object.  This function reinitializes both the shape
361     and the blit arrays.  All allocated memory is freed. */
362  void init(void);
363
364  // DIMENSION
365  /** Returns the width of the image. 
366      This is the width value previously set with #set_dimension#. */
367  int get_width(void) const;
368  /** Returns the height of the image. 
369      This is the height value previously set with #set_dimension#. */
370  int get_height(void) const;
371  /** Sets the size of the JB2Image.
372      This function can be called at any time.
373      The corresponding #width# and the #height# are stored
374      in the JB2 file. */
375  void set_dimension(int width, int height);
376
377  // RENDERING
378  /** Renders an anti-aliased gray level image.  This function renders the
379      JB2Image as a bilevel or gray level image.  Argument #subsample#
380      specifies the desired subsampling ratio in range #1# to #15#.  The
381      returned image uses #1+subsample^2# gray levels for representing
382      anti-aliased edges.  Argument #align# specified the alignment of the
383      rows of the returned images.  Setting #align# to #4#, for instance, will
384      adjust the bitmap border in order to make sure that each row of the
385      returned image starts on a word (four byte) boundary. */
386  GP<GBitmap> get_bitmap(int subsample = 1, int align = 1) const;
387  /** Renders an anti-aliased gray level sub-image.  This function renders a
388      segment of the JB2Image as a bilevel or gray level image.  Conceptually,
389      this function first renders the full JB2Image with subsampling ratio
390      #subsample# and then extracts rectangle #rect# in the subsampled image.
391      Both operations of course are efficiently performed simultaneously.
392      Argument #align# specified the alignment of the rows of the returned
393      images, as explained above.  Argument #dispy# should remain null. */
394  GP<GBitmap> get_bitmap(const GRect &rect, int subsample=1, int align=1, int dispy=0) const;
395
396  // ACCESSING THE BLIT LIBRARY
397  /** Returns the total number of blits.
398      Blit indices range from #0# to #get_blit_count(void)-1#. */
399  int get_blit_count(void) const;
400  /** Returns a pointer to blit #blitno#.
401      The returned pointer directly points into the blit array.
402      This pointer can be used for reading or writing the blit data. */
403  JB2Blit *get_blit(int blitno);
404  /** Returns a constant pointer to blit #blitno#.
405      The returned pointer directly points into the shape array.
406      This pointer can only be used for reading the shape data. */
407  const JB2Blit *get_blit(int blitno) const;
408  /** Appends a blit to the blit array.  This function appends a copy of blit
409      #blit# to the blit array and returns the subscript of the new blit.  The
410      shape subscript #blit.shapeno# must actually designate an already
411      existing shape. */
412  int  add_blit(const JB2Blit &blit);
413
414  // MEMORY OPTIMIZATION
415  /** Returns the total memory used by the JB2Image.
416      The returned value is expressed in bytes. */
417  unsigned int get_memory_usage(void) const;
418
419  // CODING
420  /** Encodes the JB2Image into ByteStream #bs#. 
421      This function generates the JB2 data stream without any header. */
422  void encode(const GP<ByteStream> &gbs) const;
423  /** Decodes JB2 data from ByteStream #bs#. This function decodes the image
424      size and populates the shape and blit arrays.  The callback function
425      #cb# is called when the decoder determines that the ByteStream data
426      requires a shape dictionary which has not been set with
427      \Ref{JB2Dict::set_inherited_dict}. The callback receives argument #arg#
428      and must return a suitable dictionary which will be installed as the
429      inherited dictionary.  The callback should return null if no such
430      dictionary is found. */
431  void decode(const GP<ByteStream> &gbs, JB2DecoderCallback *cb=0, void *arg=0);
432 
433private:
434  // Implementation
435  int width;
436  int height;
437  GTArray<JB2Blit> blits;
438public:
439  /** Reproduces a old bug.  Setting this flag may be necessary for accurately
440      decoding DjVu files with version smaller than #18#.  The default value
441      is of couse #false#. */
442  bool reproduce_old_bug;
443};
444
445
446
447// JB2DICT INLINE FUNCTIONS
448
449inline int
450JB2Dict::get_shape_count(void) const
451{
452  return inherited_shapes + shapes.size();
453}
454
455inline int
456JB2Dict::get_inherited_shape_count(void) const
457{
458  return inherited_shapes;
459}
460
461inline GP<JB2Dict>
462JB2Dict::get_inherited_dict(void) const
463{
464  return inherited_dict;
465}
466
467// JB2IMAGE INLINE FUNCTIONS
468
469inline int
470JB2Image::get_width(void) const
471{
472  return width;
473}
474
475inline int
476JB2Image::get_height(void) const
477{
478  return height;
479}
480
481
482inline int
483JB2Image::get_blit_count(void) const
484{
485  return blits.size();
486}
487
488
489inline JB2Blit *
490JB2Image::get_blit(int blitno)
491{
492  return & blits[blitno];
493}
494
495inline const JB2Blit *
496JB2Image::get_blit(int blitno) const
497{
498  return & blits[blitno];
499}
500
501
502
503
504
505/** @name JB2 extensions for version 21.
506
507    Two extensions of the JB2 encoding format have been introduced
508    with DjVu files version 21.  Both extensions maintain significant
509    backward compatibility with previous version of the JB2 format.
510    These extensions are described below by reference to the ICFDD
511    proposal dated August 1999.  Both extension make use of the unused
512    record type value #9# (cf. ICFDD page 24) which has been renamed
513    #REQUIRED_DICT_OR_RESET#.
514
515    {\bf Shared Shape Dictionaries} --- This extension provides
516    support for sharing symbol definitions between the pages of a
517    document.  To achieve this objective, the JB2 image data chunk
518    must be able to address symbols defined elsewhere by a JB2
519    dictionary data chunk shared by all the pages of a document.
520
521    The arithmetically encoded JB2 image data logically consist of a
522    sequence of records. The decoder processes these records in
523    sequence and maintains a library of symbols which can be addressed
524    by the following records.  The first record usually is a ``Start
525    Of Image'' record describing the size of the image.
526
527    Starting with version 21, a #REQUIRED_DICT_OR_RESET# (9) record
528    type can appear {\em before} the #START_OF_DATA# (0) record.  The
529    record type field is followed by a single number arithmetically
530    encoded (cf. ICFDD page 26) using a sixteenth context (cf. ICFDD
531    page 25).  This record appears when the JB2 data chunk requires
532    symbols encoded in a separate JB2 dictionary data chunk.  The
533    number (the {\bf dictionary size}) indicates how many symbols
534    should have been defined by the JB2 dictionary data chunk.  The
535    decoder should simply load these symbols in the symbol library and
536    proceed as usual.  New symbols potentially defined by the
537    subsequent JB2 image data records will therefore be numbered with
538    integers greater or equal than the dictionary size.
539
540    The JB2 dictionary data format is a pure subset of the JB2 image
541    data format.  The #START_OF_DATA# (0) record always specifies an
542    image width of zero and an image height of zero.  The only allowed
543    record types are those defining library symbols only
544    (#NEW_SYMBOL_LIBRARY_ONLY# (2) and #MATCHED_REFINE_LIBRARY_ONLY#
545    (5) cf. ICFDD page 24) followed by a final #END_OF_DATA# (11)
546    record.
547
548    The JB2 dictionary data is usually located in an {\bf Djbz} chunk.
549    Each page {\bf FORM:DJVU} may directly contain a {\bf Djbz} chunk,
550    or may indirectly point to such a chunk using an {\bf INCL} chunk
551    (cf. \Ref{Multipage DjVu documents.}).
552   
553
554    {\bf Numcoder Reset} --- This extension addresses a problem for
555    hardware implementations.  The encoding of numbers (cf. ICFDD page
556    26) potentially uses an unbounded number of binary coding
557    contexts. These contexts are normally allocated when they are used
558    for the first time (cf. ICFDD informative note, page 27).
559
560    Starting with version 21, a #REQUIRED_DICT_OR_RESET# (9) record
561    type can appear {\em after} the #START_OF_DATA# (0) record.  The
562    decoder should proceed with the next record after {\em clearing
563    all binary contexts used for coding numbers}.  This operation
564    implies that all binary contexts previously allocated for coding
565    numbers can be deallocated.
566 
567    Starting with version 21, the JB2 encoder should insert a
568    #REQUIRED_DICT_OR_RESET# record type whenever the number of these
569    allocated binary contexts exceeds #20000#.  Only very large
570    documents ever reach such a large number of allocated binary
571    contexts (e.g large maps).  Hardware implementation however can
572    benefit greatly from a hard bound on the total number of binary
573    coding contexts.  Old JB2 decoders will treat this record type as
574    an #END_OF_DATA# record and cleanly stop decoding (cf. ICFDD page
575    30, Image refinement data).
576
577
578    {\bf References} ---
579    \begin{itemize}
580    \item ICFDD Draft Proposed American National Standard, 1999-08-26.
581    \item DjVu Specification, \URL{http://www.lizardtech.com/djvu/sci/djvuspec}.
582    \end{itemize}
583
584    @memo Extensions to the JB2 format introduced in version 21.  */
585
586//@}
587
588////////////////////////////////////////
589//// CLASS JB2CODEC:  DECLARATION
590////////////////////////////////////////
591
592// This class is accessed via the encode and decode
593// functions of class JB2Image
594
595
596//**** Class JB2Codec
597// This class implements the base class for both the JB2 coder and decoder.
598// The JB2Codec's Contains all contextual information for encoding/decoding
599// a JB2Image.
600
601class JB2Dict::JB2Codec
602{
603public:
604  class Decode;
605  class Encode;
606  typedef unsigned int NumContext;
607  virtual ~JB2Codec();
608protected:
609  // Constructors
610  JB2Codec(const bool xencoding=false);
611  // Forbidden assignment
612  JB2Codec(const JB2Codec &ref);
613  JB2Codec& operator=(const JB2Codec &ref);
614
615  int CodeNum(int lo, int hi, NumContext *pctx,int v);
616  void reset_numcoder(void);
617  inline void code_eventual_lossless_refinement(void);
618  void init_library(JB2Dict &jim);
619  int add_library(const int shapeno, JB2Shape &jshp);
620  void code_relative_location(JB2Blit *jblt, int rows, int columns);
621  void code_bitmap_directly (GBitmap &bm);
622  void code_bitmap_by_cross_coding (GBitmap &bm, GP<GBitmap> &cbm, const int libno);
623  void code_record(int &rectype, const GP<JB2Dict> &jim, JB2Shape *jshp);
624  void code_record(int &rectype, const GP<JB2Image> &jim, JB2Shape *jshp, JB2Blit *jblt);
625  static void compute_bounding_box(GBitmap &cbm, LibRect &lrect);
626  static int get_direct_context( unsigned char const * const up2,
627    unsigned char const * const up1, unsigned char const * const up0,
628    const int column);
629  static int shift_direct_context ( const int context,
630    const int next, unsigned char const * const up2,
631    unsigned char const * const up1, unsigned char const * const up0,
632    const int column);
633  static int get_cross_context( unsigned char const * const up1,
634    unsigned char const * const up0, unsigned char const * const xup1,
635    unsigned char const * const xup0, unsigned char const * const xdn1,
636    const int column );
637  static int shift_cross_context( const int context,
638    const int n, unsigned char const * const up1,
639    unsigned char const * const up0, unsigned char const * const xup1,
640    unsigned char const * const xup0, unsigned char const * const xdn1,
641    const int column );
642
643  virtual bool CodeBit(const bool bit, BitContext &ctx) = 0;
644  virtual void code_comment(GUTF8String &comment) = 0;
645  virtual void code_record_type(int &rectype) = 0;
646  virtual int code_match_index(int &index, JB2Dict &jim)=0;
647  virtual void code_inherited_shape_count(JB2Dict &jim)=0;
648  virtual void code_image_size(JB2Dict &jim);
649  virtual void code_image_size(JB2Image &jim);
650  virtual void code_absolute_location(JB2Blit *jblt,  int rows, int columns)=0;
651  virtual void code_absolute_mark_size(GBitmap &bm, int border=0) = 0;
652  virtual void code_relative_mark_size(GBitmap &bm, int cw, int ch, int border=0) = 0;
653  virtual void code_bitmap_directly(GBitmap &bm,const int dw, int dy,
654    unsigned char *up2, unsigned char *up1, unsigned char *up0 )=0;
655  virtual void code_bitmap_by_cross_coding (GBitmap &bm, GBitmap &cbm,
656    const int xd2c, const int dw, int dy, int cy,
657    unsigned char *up1, unsigned char *up0, unsigned char *xup1, 
658    unsigned char *xup0, unsigned char *xdn1 )=0;
659  // Code records
660  virtual int get_diff(const int x_diff,NumContext &rel_loc) = 0;
661
662private:
663  bool encoding;
664
665protected:
666  // NumCoder
667  int cur_ncell;
668  BitContext *bitcells;
669  GPBuffer<BitContext> gbitcells;
670  NumContext *leftcell;
671  GPBuffer<NumContext> gleftcell;
672  NumContext *rightcell;
673  GPBuffer<NumContext> grightcell;
674  // Info
675  bool refinementp;
676  char gotstartrecordp;
677  // Code comment
678  NumContext dist_comment_byte;
679  NumContext dist_comment_length;
680  // Code values
681  NumContext dist_record_type;
682  NumContext dist_match_index;
683  BitContext dist_refinement_flag;
684  // Library
685  GTArray<int> shape2lib;
686  GTArray<int> lib2shape;
687  GTArray<LibRect> libinfo;
688  // Code pairs
689  NumContext abs_loc_x;
690  NumContext abs_loc_y;
691  NumContext abs_size_x;
692  NumContext abs_size_y;
693  NumContext image_size_dist;
694  NumContext inherited_shape_count_dist;
695  BitContext offset_type_dist;
696  NumContext rel_loc_x_current;
697  NumContext rel_loc_x_last;
698  NumContext rel_loc_y_current;
699  NumContext rel_loc_y_last;
700  NumContext rel_size_x;
701  NumContext rel_size_y;
702  int last_bottom;
703  int last_left;
704  int last_right;
705  int last_row_bottom;
706  int last_row_left;
707  int image_columns;
708  int image_rows;
709  int short_list[3];
710  int short_list_pos;
711  inline void fill_short_list(const int v);
712  int update_short_list(const int v);
713  // Code bitmaps
714  BitContext bitdist[1024];
715  BitContext cbitdist[2048];
716};
717
718inline void
719JB2Dict::JB2Codec::code_eventual_lossless_refinement(void)
720{
721  refinementp=CodeBit(refinementp, dist_refinement_flag);
722}
723
724inline void
725JB2Dict::JB2Codec::fill_short_list(const int v)
726{
727  short_list[0] = short_list[1] = short_list[2] = v;
728  short_list_pos = 0;
729}
730
731inline int
732JB2Dict::JB2Codec::get_direct_context( unsigned char const * const up2,
733                    unsigned char const * const up1,
734                    unsigned char const * const up0,
735                    const int column)
736{
737  return ( (up2[column - 1] << 9) |
738              (up2[column    ] << 8) |
739              (up2[column + 1] << 7) |
740              (up1[column - 2] << 6) |
741              (up1[column - 1] << 5) |
742              (up1[column    ] << 4) |
743              (up1[column + 1] << 3) |
744              (up1[column + 2] << 2) |
745              (up0[column - 2] << 1) |
746              (up0[column - 1] << 0) );
747}
748
749inline int
750JB2Dict::JB2Codec::shift_direct_context(const int context, const int next,
751                     unsigned char const * const up2,
752                     unsigned char const * const up1,
753                     unsigned char const * const up0,
754                     const int column)
755{
756  return ( ((context << 1) & 0x37a) |
757              (up1[column + 2] << 2)   |
758              (up2[column + 1] << 7)   |
759              (next << 0)              );
760}
761
762inline int
763JB2Dict::JB2Codec::get_cross_context( unsigned char const * const up1,
764                   unsigned char const * const up0,
765                   unsigned char const * const xup1,
766                   unsigned char const * const xup0,
767                   unsigned char const * const xdn1,
768                   const int column )
769{
770  return ( ( up1[column - 1] << 10) |
771              ( up1[column    ] <<  9) |
772              ( up1[column + 1] <<  8) |
773              ( up0[column - 1] <<  7) |
774              (xup1[column    ] <<  6) |
775              (xup0[column - 1] <<  5) |
776              (xup0[column    ] <<  4) |
777              (xup0[column + 1] <<  3) |
778              (xdn1[column - 1] <<  2) |
779              (xdn1[column    ] <<  1) |
780              (xdn1[column + 1] <<  0) );
781}
782
783inline int
784JB2Dict::JB2Codec::shift_cross_context( const int context, const int n,
785                     unsigned char const * const up1,
786                     unsigned char const * const up0,
787                     unsigned char const * const xup1,
788                     unsigned char const * const xup0,
789                     unsigned char const * const xdn1,
790                     const int column )
791{
792  return ( ((context<<1) & 0x636)  |
793              ( up1[column + 1] << 8) |
794              (xup1[column    ] << 6) |
795              (xup0[column + 1] << 3) |
796              (xdn1[column + 1] << 0) |
797              (n << 7)             );
798}
799
800// ---------- THE END
801
802#ifdef HAVE_NAMESPACES
803}
804# ifndef NOT_USING_DJVU_NAMESPACE
805using namespace DJVU;
806# endif
807#endif
808#endif
809
Note: See TracBrowser for help on using the repository browser.