source: trunk/libdjvu/MMRDecoder.h @ 426

Last change on this file since 426 was 280, checked in by rbri, 12 years ago

DJVU plugin: djvulibre updated to version 3.5.22

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