source: trunk/libdjvu/JB2Image.h @ 17

Last change on this file since 17 was 17, checked in by Eugene Romanenko, 15 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

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