source: trunk/libdjvu/MMRDecoder.h @ 15

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

needed libs update

File size: 9.5 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: MMRDecoder.h,v 1.9 2003/11/07 22:08:22 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifndef _MMRDECODER_H_
58#define _MMRDECODER_H_
59#ifdef HAVE_CONFIG_H
60#include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma interface
64#endif
65
66
67#include "GSmartPointer.h"
68
69#ifdef HAVE_NAMESPACES
70namespace DJVU {
71# ifdef NOT_DEFINED // Just to fool emacs c++ mode
72}
73#endif
74#endif
75
76class ByteStream;
77class JB2Image;
78
79/** @name MMRDecoder.h
80    Files #"MMRDecoder.h"# and #"MMRDecoder.cpp"# implement a
81    CCITT-G4/MMR decoder suitable for use in DjVu.  The main
82    entry point is function \Ref{MMRDecoder::decode}.
83
84    The foreground mask layer of a DjVu file is usually encoded with a
85    #"Sjbz"# chunk containing JB2 encoded data (cf. \Ref{JB2Image.h}).
86    Alternatively, the qmask layer may be encoded with a #"Smmr"#
87    chunk containing a small header followed by MMR encoded data.
88    This encoding scheme produces significantly larger files. On the
89    other hand, many scanners a printers talk MMR using very efficient
90    hardware components.  This is the reason behind the introduction
91    of #"Smmr"# chunks.
92
93    The #Smmr# chunk starts by a header containing the following data:
94    \begin{verbatim}
95        BYTE*3    :  'M' 'M' 'R'
96        BYTE      :  0xb000000<s><i>
97        INT16     :  <width> (MSB first)
98        INT16     :  <height> (MSB first)
99    \end{verbatim}
100
101    The header is followed by the encoded data.  Bit 0 of the fourth header
102    byte (#<i>#) is similar to TIFF's ``min-is-black'' tag.  This bit is set
103    for a reverse video image.  The encoded data can be in either ``regular''
104    MMR form or ``striped'' MMR form.  This is indicated by bit 1 of the
105    fourth header byte (#<s>#).  This bit is set to indicate ``striped''
106    data.  The ``regular'' data format consists of ordinary MMR encoded data.
107    The ``striped'' data format consists of one sixteen bit integer (msb
108    first) containing the number of rows per stripe, followed by data for each
109    stripe as follows.
110    \begin{verbatim}
111        INT16     :  <rowsperstripe> (MSB first)
112        INT32          :  <nbytes1>
113        BYTE*<nbytes1> :  <mmrdata1>
114        INT32          :  <nbytes2>
115        BYTE*<nbytes2> :  <mmrdata2>
116          ...
117    \end{verbatim}
118    Static function \Ref{MMRDecoder::decode_header} decodes the header.  You
119    can then create a \Ref{MMRDecoder} object with the flags #inverted# and
120    #striped# as obtained when decoding the header.  One can also decode raw
121    MMR data by simply initialising a \Ref{MMRDecoder} object with flag
122    #striped# unset.  Each call to \Ref{MMRDecoder::scanruns},
123    \Ref{MMRDecoder::scanrle} or \Ref{MMRDecoder::scanline} will then decode a
124    row of the MMR encoded image.
125
126    Function \Ref{MMRDecoder::decode} is a convenience function for decoding
127    the contents of a #"Smmr"# chunk.  It returns a \Ref{JB2Image} divided
128    into manageable blocks in order to provide the zooming and panning
129    features implemented by class \Ref{JB2Image}.
130
131    @memo
132    CCITT-G4/MMR decoder.
133    @version
134    #$Id: MMRDecoder.h,v 1.9 2003/11/07 22:08:22 leonb Exp $#
135    @author
136    Parag Deshmukh <parag@sanskrit.lz.att.com> \\
137    Leon Bottou <leonb@research.att.com> */
138//@{
139
140
141
142#define MMRDECODER_HAS_SCANRUNS  1
143#define MMRDECODER_HAS_SCANRLE   1
144
145
146
147/** Class for G4/MMR decoding.  The simplest way to use this class is
148    the static member function \Ref{MMRDecoder::decode}.  This
149    function internally creates an instance of #MMRDecoder# which
150    processes the MMR data scanline by scanline.  */
151class MMRDecoder : public GPEnabled
152{
153protected:
154  MMRDecoder(const int width, const int height);
155  void init(GP<ByteStream> gbs, const bool striped=false);
156public:
157  /** Main decoding routine that (a) decodes the header using
158      #decode_header#, (b) decodes the MMR data using an instance of
159      #MMRDecoder#, and returns a new \Ref{JB2Image} composed of tiles
160      whose maximal width and height is derived from the size of the
161      image. */
162  static GP<JB2Image> decode(GP<ByteStream> gbs);
163
164  /// Only decode the header.
165  static bool decode_header(ByteStream &inp, 
166                            int &width, int &height, int &invert);
167
168public:
169  /// Non-virtual destructor.
170  ~MMRDecoder();
171  /** Create a MMRDecoder object for decoding an image
172      of size #width# by #height#. Flag $striped# must be set
173      if the image is composed of multiple stripes. */
174  static GP<MMRDecoder> create(GP<ByteStream> gbs, 
175                               const int width, const int height,
176                               const bool striped=false );
177
178  /** Decodes a scanline and returns a pointer to an array of run lengths.
179      The returned buffer contains the length of alternative white and black
180      runs.  These run lengths sum to the image width. They are followed by
181      two zeroes.  The position of these two zeroes is stored in the pointer
182      specified by the optional argument #endptr#.  The buffer data should be
183      processed before calling this function again. */
184  const unsigned short *scanruns(const unsigned short **endptr=0);
185  /** Decodes a scanline and returns a pointer to RLE encoded data.  The
186      buffer contains the length of the runs for the current line encoded as
187      described in \Ref{PNM and RLE file formats}.)  The flag #invert# can be
188      used to indicate that the MMR data is encoded in reverse video.  The RLE
189      data is followed by two zero bytes.  The position of these two zeroes is
190      stored in the pointer specified by the optional argument #endptr#.  The
191      buffer data should be processed before calling this function again. This
192      is implemented by calling \Ref{MMRDecoder::scanruns}. */
193  const unsigned char  *scanrle(const bool invert, 
194                                const unsigned char **endptr=0);
195#if 0
196  /** Decodes a scanline and returns a pointer to an array of #0# or #1# bytes.
197      Returns a pointer to the scanline buffer containing one byte per pixel.
198      The buffer data should be processed before calling this function again.
199      This is implemented by calling \Ref{MMRDecoder::scanruns}. */
200  const unsigned char *scanline();
201#endif
202 private:
203  int width;
204  int height;
205  int lineno;
206  int striplineno;
207  int rowsperstrip;
208  unsigned char  *line;
209  GPBuffer<unsigned char> gline;
210  unsigned short *lineruns;
211  GPBuffer<unsigned short> glineruns;
212  unsigned short *prevruns;
213  GPBuffer<unsigned short> gprevruns;
214public:
215  class VLSource;
216  class VLTable;
217private:
218  GP<VLSource> src;
219  GP<VLTable> mrtable;
220  GP<VLTable> wtable;
221  GP<VLTable> btable;
222  friend class VLSource;
223  friend class VLTable;
224};
225
226
227//@}
228
229
230// -----------
231
232#ifdef HAVE_NAMESPACES
233}
234# ifndef NOT_USING_DJVU_NAMESPACE
235using namespace DJVU;
236# endif
237#endif
238#endif
Note: See TracBrowser for help on using the repository browser.