source: trunk/libdjvu/IW44Image.h @ 199

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

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

File size: 32.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, 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: IW44Image.h,v 1.11 2003/11/07 22:08:22 leonb Exp $
55// $Name:  $
56
57#ifndef IW44IMAGE_H_
58#define IW44IMAGE_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/** @name IW44Image.h
68
69    Files #"IW44Image.h"# and #"IW44Image.cpp"# implement the DjVu IW44 wavelet
70    scheme for the compression of gray-level images (see class \Ref{IWBitmap})
71    and color images (see class \Ref{IWPixmap}).  Programs \Ref{c44} and
72    \Ref{d44} demonstrate how to encode and decode IW44 files.
73
74    {\bf IW44 File Structure} --- The IW44 files are structured according to
75    the EA IFF85 specifications (see \Ref{IFFByteStream.h}).  Gray level IW44
76    Images consist of a single #"FORM:BM44"# chunk composed of an arbitrary
77    number of #"BM44"# data chunks.  Color IW44 Images consist of a single
78    #"FORM:PM44"# chunk composed of an arbitrary number of #"PM44"# data
79    chunks.  The successive #"PM44"# or #"BM44"# data chunks contain
80    successive refinements of the encoded image.  Each chunk contains a
81    certain number of ``data slices''.  The first chunk also contains a small
82    image header.  You can use program \Ref{djvuinfo} to display all this
83    structural information:
84    \begin{verbatim}
85    % djvuinfo lag.iw4
86    lag.iw4:
87      FORM:PM44 [62598]
88        PM44 [10807]              #1 - 74 slices - v1.2 (color) - 684x510
89        PM44 [23583]              #2 - 13 slices
90        PM44 [28178]              #3 - 10 slices
91    \end{verbatim}
92
93    {\bf Embedded IW44 Images} --- These IW44 data chunks can also appear within
94    other contexts.  Files representing a DjVu page, for instance, consist of
95    a single #"FORM:DJVU"# composite chunk.  This composite chunk may contain
96    #"BG44"# chunks encoding the background layer and #"FG44"# chunks encoding
97    the foreground color layer.  These #"BG44"# and #"FG44"# chunks are
98    actually regular IW44 data chunks with a different chunk identifier.  This
99    information too can be displayed using program \Ref{djvuinfo}.
100    \begin{verbatim}
101    % djvuinfo graham1.djvu
102    graham1.djvu:
103      FORM:DJVU [32553]
104        INFO [5]            3156x2325, version 17
105        Sjbz [17692]
106        BG44 [2570]         #1 - 74 slices - v1.2 (color) - 1052x775
107        FG44 [1035]         #1 - 100 slices - v1.2 (color) - 263x194
108        BG44 [3048]         #2 - 10 slices
109        BG44 [894]          #3 - 4 slices
110        BG44 [7247]         #4 - 9 slices
111    \end{verbatim}
112
113    {\bf Performance} --- The main design objective for the DjVu wavelets
114    consisted of allowing progressive rendering and smooth scrolling of large
115    images with limited memory requirements.  Decoding functions process the
116    compressed data and update a memory efficient representation of the
117    wavelet coefficients.  Imaging function then can quickly render an
118    arbitrary segment of the image using the available data.  Both process can
119    be carried out in two threads of execution.  This design plays an
120    important role in the DjVu system.  We have investigated various
121    state-of-the-art wavelet compression schemes: although these schemes may
122    achieve slightly smaller file sizes, the decoding functions did not even
123    approach our requirements. 
124
125    The IW44 wavelets satisfy these requirements today. It performs very well
126    for quality settings resulting in high compression ratios.  It should not
127    be used for quasi-lossless compression because certain design choices
128    deliberately sacrifice the IW44 quasi-lossless performance in order to
129    improve the image quality at high compression ratios.
130
131    Little care however has been taken to make the IW44 encoder memory
132    efficient.  This code uses two copies of the wavelet coefficient data
133    structure (one for the raw coefficients, one for the quantized
134    coefficients).  A more sophisticated implementation should considerably
135    reduce the memory requirements.
136
137    {\bf Masking} --- When we create a DjVu image, we often know that certain
138    pixels of the background image are going to be covered by foreground
139    objects like text or drawings.  The DjVu IW44 wavelet decomposition
140    routine can use an optional bilevel image named the mask.  Every non zero
141    pixel in the mask means the value of the corresponding pixel in the
142    background image is irrelevant.  The wavelet decomposition code will
143    replace these masked pixels by a color value whose coding cost is minimal
144    (see \URL{http://www.research.att.com/~leonb/DJVU/mask}).
145
146    {\bf ToDo} --- There are many improvements to be made.  Besides better
147    quantization algorithms (such as trellis quantization and bitrate
148    allocation), we should allow for more wavelet transforms.  These
149    improvements may be implemented in future version, if (and only if) they
150    can meet our decoding constraints.  Future versions will probably split
151    file #"IW44Image.cpp"# which currently contains everything.
152 
153    @memo
154    Wavelet encoded images.
155    @author
156    L\'eon Bottou <leonb@research.att.com>
157
158// From: Leon Bottou, 1/31/2002
159// Lizardtech has split the corresponding cpp file into a decoder and an encoder.
160// Only superficial changes.  The meat is mine.
161
162    @version
163    #$Id: IW44Image.h,v 1.11 2003/11/07 22:08:22 leonb Exp $# */
164//@{
165
166
167#include "GSmartPointer.h"
168#include "ZPCodec.h"
169
170
171#ifdef HAVE_NAMESPACES
172namespace DJVU {
173# ifdef NOT_DEFINED // Just to fool emacs c++ mode
174}
175#endif
176#endif
177
178class GRect;
179class IFFByteStream;
180class ByteStream;
181class GBitmap;
182class GPixmap;
183
184
185
186/** IW44 encoding parameters. 
187    This data structure gathers the quality specification parameters needed
188    for encoding each chunk of an IW44 file.  Chunk data is generated until
189    meeting either the slice target, the size target or the decibel target.  */
190
191struct IWEncoderParms
192{
193  /** Slice target.  Data generation for the current chunk stops if the total
194      number of slices (in this chunk and all the previous chunks) reaches
195      value #slice#.  The default value #0# has a special meaning: data will
196      be generated regardless of the number of slices in the file. */
197  int    slices;
198  /** Size target.  Data generation for the current chunk stops if the total
199      data size (in this chunk and all the previous chunks), expressed in
200      bytes, reaches value #size#.  The default value #0# has a special
201      meaning: data will be generated regardless of the file size. */
202  int    bytes;
203  /** Decibel target.  Data generation for the current chunk stops if the
204      estimated luminance error, expressed in decibels, reaches value
205      #decibel#.  The default value #0# has a special meaning: data will be
206      generated regardless of the estimated luminance error.  Specifying value
207      #0# in fact shortcuts the computation of the estimated luminance error
208      and sensibly speeds up the encoding process.  */
209  float  decibels;
210  /** Constructor. Initializes the structure with the default values. */
211  IWEncoderParms(void);
212};
213
214
215
216/** IW44 encoded gray-level and color images.  This class acts as a base for
217    images represented as a collection of IW44 wavelet coefficients.  The
218    coefficients are stored in a memory efficient data structure.  Member
219    function \Ref{get_bitmap} renders an arbitrary segment of the image into
220    a \Ref{GBitmap}.  Member functions \Ref{decode_iff} and \Ref{encode_iff}
221    read and write DjVu IW44 files (see \Ref{IW44Image.h}).  Both the copy
222    constructor and the copy operator are declared as private members. It is
223    therefore not possible to make multiple copies of instances of this
224    class. */
225
226class IW44Image : public GPEnabled
227{
228public:
229  /** Chrominance processing selector.  The following constants may be used as
230      argument to the following \Ref{IWPixmap} constructor to indicate how the
231      chrominance information should be processed. There are four possible values:
232      \begin{description}
233      \item[CRCBnone:] The wavelet transform will discard the chrominance
234           information and only keep the luminance. The image will show in shades of gray.
235      \item[CRCBhalf:] The wavelet transform will process the chrominance at only
236           half the image resolution. This option creates smaller files but may create
237           artifacts in highly colored images.
238      \item[CRCBnormal:] The wavelet transform will process the chrominance at full
239           resolution. This is the default.
240      \item[CRCBfull:] The wavelet transform will process the chrominance at full
241           resolution. This option also disables the chrominance encoding delay
242           (see \Ref{parm_crcbdelay}) which usually reduces the bitrate associated with the
243           chrominance information.
244      \end{description} */
245  enum CRCBMode { 
246    CRCBnone, 
247    CRCBhalf, 
248    CRCBnormal, 
249    CRCBfull };
250  class Transform;
251  class Map;
252  class Block;
253  class Codec;
254  struct Alloc;
255  struct PrimaryHeader;
256  struct SecondaryHeader;
257  struct TertiaryHeader;
258  enum ImageType {
259    GRAY=false,
260    COLOR=true };
261protected:
262  IW44Image(void);
263public:
264  /** Null constructor.  Constructs an empty IW44Image object. This object does
265      not contain anything meaningful. You must call function \Ref{init},
266      \Ref{decode_iff} or \Ref{decode_chunk} to populate the wavelet
267      coefficient data structure. You may not use \Ref{encode_iff} or
268      \Ref{encode_chunk}. */
269  static GP<IW44Image> create_decode(const ImageType itype=COLOR);
270  /** Null constructor.  Constructs an empty IW44Image object. This object does
271      not contain anything meaningful. You must call function \Ref{init},
272      \Ref{decode_iff} or \Ref{decode_chunk} to populate the wavelet
273      coefficient data structure.  You may then use \Ref{encode_iff}
274      and \Ref{encode_chunk}. */
275  static GP<IW44Image> create_encode(const ImageType itype=COLOR);
276  // virtual destructor
277  virtual ~IW44Image();
278  /** Initializes an IWBitmap with image #bm#.  This constructor
279      performs the wavelet decomposition of image #bm# and records the
280      corresponding wavelet coefficient.  Argument #mask# is an optional
281      bilevel image specifying the masked pixels (see \Ref{IW44Image.h}). */
282  static GP<IW44Image> create_encode(const GBitmap &bm, const GP<GBitmap> mask=0);
283  /** Initializes an IWPixmap with color image #bm#.  This constructor
284      performs the wavelet decomposition of image #bm# and records the
285      corresponding wavelet coefficient.  Argument #mask# is an optional
286      bilevel image specifying the masked pixels (see \Ref{IW44Image.h}).
287      Argument #crcbmode# specifies how the chrominance information should be
288      encoded (see \Ref{CRCBMode}). */
289  static GP<IW44Image> create_encode(const GPixmap &bm, const GP<GBitmap> mask=0, CRCBMode crcbmode=CRCBnormal);
290  // ACCESS
291  /** Returns the width of the IWBitmap image. */
292  int get_width(void) const;
293  /** Returns the height of the IWBitmap image. */
294  int get_height(void) const;
295  /** Reconstructs the complete image.  The reconstructed image
296      is then returned as a GBitmap object. */
297  virtual GP<GBitmap> get_bitmap(void) {return 0;}
298  /** Reconstructs a segment of the image at a given scale.  The subsampling
299      ratio #subsample# must be a power of two between #1# and #32#.  Argument
300      #rect# specifies which segment of the subsampled image should be
301      reconstructed.  The reconstructed image is returned as a GBitmap object
302      whose size is equal to the size of the rectangle #rect#. */
303  virtual GP<GBitmap> get_bitmap(int subsample, const GRect &rect) {return 0;}
304  /** Reconstructs the complete image.  The reconstructed image
305      is then returned as a GPixmap object. */
306  virtual GP<GPixmap> get_pixmap(void) {return 0;}
307  /** Reconstructs a segment of the image at a given scale.  The subsampling
308      ratio #subsample# must be a power of two between #1# and #32#.  Argument
309      #rect# specifies which segment of the subsampled image should be
310      reconstructed.  The reconstructed image is returned as a GPixmap object
311      whose size is equal to the size of the rectangle #rect#. */
312  virtual GP<GPixmap> get_pixmap(int subsample, const GRect &rect) {return 0;}
313  /** Returns the amount of memory used by the wavelet coefficients.  This
314      amount of memory is expressed in bytes. */
315  virtual unsigned int get_memory_usage(void) const = 0;
316  /** Returns the filling ratio of the internal data structure.  Wavelet
317      coefficients are stored in a sparse array.  This function tells what
318      percentage of bins have been effectively allocated. */
319  virtual int get_percent_memory(void) const = 0;
320  // CODER
321  /** Encodes one data chunk into ByteStream #bs#.  Parameter #parms# controls
322      how much data is generated.  The chunk data is written to ByteStream
323      #bs# with no IFF header.  Successive calls to #encode_chunk# encode
324      successive chunks.  You must call #close_codec# after encoding the last
325      chunk of a file. */
326  virtual int  encode_chunk(GP<ByteStream> gbs, const IWEncoderParms &parms);
327  /** Writes a gray level image into DjVu IW44 file.  This function creates a
328      composite chunk (identifier #FORM:BM44# or #FORM:PM44#) composed of
329      #nchunks# chunks (identifier #BM44# or #PM44#).  Data for each chunk is
330      generated with #encode_chunk# using the corresponding parameters in
331      array #parms#. */
332  virtual void encode_iff(IFFByteStream &iff, int nchunks, const IWEncoderParms *parms);
333  // DECODER
334  /** Decodes one data chunk from ByteStream #bs#.  Successive calls to
335      #decode_chunk# decode successive chunks.  You must call #close_codec#
336      after decoding the last chunk of a file.  Note that function
337      #get_bitmap# and #decode_chunk# may be called simultaneously from two
338      execution threads. */
339  virtual int  decode_chunk(GP<ByteStream> gbs) = 0;
340  /** This function enters a composite chunk (identifier #FORM:BM44#, or
341      #FORM:PM44#), and decodes a maximum of #maxchunks# data chunks
342      (identifier #BM44#).  Data for each chunk is processed using the
343      function #decode_chunk#. */
344  virtual void decode_iff(IFFByteStream &iff, int maxchunks=999) = 0;
345  // MISCELLANEOUS
346  /** Resets the encoder/decoder state.  The first call to #decode_chunk# or
347      #encode_chunk# initializes the coder for encoding or decoding.  Function
348      #close_codec# must be called after processing the last chunk in order to
349      reset the coder and release the associated memory. */
350  virtual void close_codec(void) = 0;
351  /** Returns the chunk serial number.  This function returns the serial
352      number of the last chunk encoded with #encode_chunk# or decoded with
353      #decode_chunk#. The first chunk always has serial number #1#. Successive
354      chunks have increasing serial numbers.  Value #0# is returned if this
355      function is called before calling #encode_chunk# or #decode_chunk# or
356      after calling #close_codec#. */
357  virtual int get_serial(void) = 0;
358  /** Sets the chrominance delay parameter.  This function can be called
359      before encoding the first color IW44 data chunk.  Parameter #parm# is an
360      encoding delay which reduces the bitrate associated with the
361      chrominance information. The default chrominance encoding delay is 10. */
362  virtual int  parm_crcbdelay(const int parm) {return parm;}
363  /** Sets the #dbfrac# parameter.  This function can be called before
364      encoding the first IW44 data chunk.  Parameter #frac# modifies the
365      decibel estimation algorithm in such a way that the decibel target only
366      pertains to the average error of the fraction #frac# of the most
367      misrepresented 32x32 pixel blocks.  Setting arguments #frac# to #1.0#
368      restores the normal behavior.  */
369  virtual void parm_dbfrac(float frac) = 0;
370protected:
371  // Parameter
372  float db_frac;
373  // Data
374  Map *ymap, *cbmap, *crmap;
375  int cslice;
376  int cserial;
377  int cbytes;
378private:
379  // Disable assignment semantic
380  IW44Image(const IW44Image &ref);
381  IW44Image& operator=(const IW44Image &ref);
382};
383
384#ifdef IW44IMAGE_IMPLIMENTATION
385
386/*x IW44 encoded gray-level image.  This class provided functions for managing
387    a gray level image represented as a collection of IW44 wavelet
388    coefficients.  The coefficients are stored in a memory efficient data
389    structure.  Member function \Ref{get_bitmap} renders an arbitrary segment
390    of the image into a \Ref{GBitmap}.  Member functions \Ref{decode_iff} and
391    \Ref{encode_iff} read and write DjVu IW44 files (see \Ref{IW44Image.h}).
392    Both the copy constructor and the copy operator are declared as private
393    members. It is therefore not possible to make multiple copies of instances
394    of this class. */
395
396class IWBitmap : public IW44Image
397{
398public:
399  friend class IW44Image;
400  class Encode;
401protected:
402  /*x Null constructor.  Constructs an empty IWBitmap object. This object does
403      not contain anything meaningful. You must call function \Ref{init},
404      \Ref{decode_iff} or \Ref{decode_chunk} to populate the wavelet
405      coefficient data structure. */
406  IWBitmap(void);
407public:
408  //x virtual destructor
409  virtual ~IWBitmap();
410  //x ACCESS
411  /*x Reconstructs the complete image.  The reconstructed image
412      is then returned as a GBitmap object. */
413  virtual GP<GBitmap> get_bitmap(void);
414  /*x Reconstructs a segment of the image at a given scale.  The subsampling
415      ratio #subsample# must be a power of two between #1# and #32#.  Argument
416      #rect# specifies which segment of the subsampled image should be
417      reconstructed.  The reconstructed image is returned as a GBitmap object
418      whose size is equal to the size of the rectangle #rect#. */
419  virtual GP<GBitmap> get_bitmap(int subsample, const GRect &rect);
420  /*x Returns the amount of memory used by the wavelet coefficients.  This
421      amount of memory is expressed in bytes. */
422  virtual unsigned int get_memory_usage(void) const;
423  /*x Returns the filling ratio of the internal data structure.  Wavelet
424      coefficients are stored in a sparse array.  This function tells what
425      percentage of bins have been effectively allocated. */
426  virtual int get_percent_memory(void) const;
427  // DECODER
428  /*x Decodes one data chunk from ByteStream #bs#.  Successive calls to
429      #decode_chunk# decode successive chunks.  You must call #close_codec#
430      after decoding the last chunk of a file.  Note that function
431      #get_bitmap# and #decode_chunk# may be called simultaneously from two
432      execution threads. */
433  virtual int  decode_chunk(GP<ByteStream> gbs);
434  /*x Reads a DjVu IW44 file as a gray level image.  This function enters a
435      composite chunk (identifier #FORM:BM44#), and decodes a maximum of
436      #maxchunks# data chunks (identifier #BM44#).  Data for each chunk is
437      processed using the function #decode_chunk#. */
438  virtual void decode_iff(IFFByteStream &iff, int maxchunks=999);
439  // MISCELLANEOUS
440  /*x Resets the encoder/decoder state.  The first call to #decode_chunk# or
441      #encode_chunk# initializes the coder for encoding or decoding.  Function
442      #close_codec# must be called after processing the last chunk in order to
443      reset the coder and release the associated memory. */
444  virtual void close_codec(void);
445  /*x Returns the chunk serial number.  This function returns the serial
446      number of the last chunk encoded with #encode_chunk# or decoded with
447      #decode_chunk#. The first chunk always has serial number #1#. Successive
448      chunks have increasing serial numbers.  Value #0# is returned if this
449      function is called before calling #encode_chunk# or #decode_chunk# or
450      after calling #close_codec#. */
451  virtual int get_serial(void);
452  /*x Sets the #dbfrac# parameter.  This function can be called before
453      encoding the first IW44 data chunk.  Parameter #frac# modifies the
454      decibel estimation algorithm in such a way that the decibel target only
455      pertains to the average error of the fraction #frac# of the most
456      misrepresented 32x32 pixel blocks.  Setting arguments #frac# to #1.0#
457      restores the normal behavior.  */
458  virtual void parm_dbfrac(float frac);
459private:
460  Codec *ycodec;
461};
462
463
464/*x IW44 encoded color image. This class provided functions for managing a
465    color image represented as a collection of IW44 wavelet coefficients.  The
466    coefficients are stored in a memory efficient data structure.  Member
467    function \Ref{get_pixmap} renders an arbitrary segment of the image into a
468    \Ref{GPixmap}.  Member functions \Ref{decode_iff} and \Ref{encode_iff}
469    read and write DjVu IW44 files (see \Ref{IW44Image.h}).  Both the copy
470    constructor and the copy operator are declared as private members. It is
471    therefore not possible to make multiple copies of instances of this
472    class. */
473
474class IWPixmap : public IW44Image
475{
476public:
477  friend class IW44Image;
478protected:
479  class Encode;
480  /*x Null constructor.  Constructs an empty IWPixmap object. This object does
481      not contain anything meaningful. You must call function \Ref{init},
482      \Ref{decode_iff} or \Ref{decode_chunk} to populate the wavelet
483      coefficient data structure. */
484  IWPixmap(void);
485public:
486  // virtual destructor
487  virtual ~IWPixmap();
488  // ACCESS
489  /*x Reconstructs the complete image.  The reconstructed image
490      is then returned as a GPixmap object. */
491  virtual GP<GPixmap> get_pixmap(void);
492  /*x Reconstructs a segment of the image at a given scale.  The subsampling
493      ratio #subsample# must be a power of two between #1# and #32#.  Argument
494      #rect# specifies which segment of the subsampled image should be
495      reconstructed.  The reconstructed image is returned as a GPixmap object
496      whose size is equal to the size of the rectangle #rect#. */
497  virtual GP<GPixmap> get_pixmap(int subsample, const GRect &rect);
498  /*x Returns the amount of memory used by the wavelet coefficients.  This
499      amount of memory is expressed in bytes. */
500  virtual unsigned int get_memory_usage(void) const;
501  /*x Returns the filling ratio of the internal data structure.  Wavelet
502      coefficients are stored in a sparse array.  This function tells what
503      percentage of bins have been effectively allocated. */
504  virtual int get_percent_memory(void) const;
505  // DECODER
506  /*x Decodes one data chunk from ByteStream #bs#.  Successive calls to
507      #decode_chunk# decode successive chunks.  You must call #close_codec#
508      after decoding the last chunk of a file.  Note that function
509      #get_bitmap# and #decode_chunk# may be called simultaneously from two
510      execution threads. */
511  virtual int  decode_chunk(GP<ByteStream> gbs);
512  /*x Reads a DjVu IW44 file as a color image.  This function enters a
513      composite chunk (identifier #FORM:PM44# or #FORM:BM44#), and decodes a
514      maximum of #maxchunks# data chunks (identifier #PM44# or #BM44#).  Data
515      for each chunk is processed using the function #decode_chunk#. */
516  virtual void decode_iff(IFFByteStream &iff, int maxchunks=999);
517  // MISCELLANEOUS
518  /*x Resets the encoder/decoder state.  The first call to #decode_chunk# or
519      #encode_chunk# initializes the coder for encoding or decoding.  Function
520      #close_codec# must be called after processing the last chunk in order to
521      reset the coder and release the associated memory. */
522  virtual void close_codec(void);
523  /*x Returns the chunk serial number.  This function returns the serial
524      number of the last chunk encoded with #encode_chunk# or decoded with
525      #decode_chunk#. The first chunk always has serial number #1#. Successive
526      chunks have increasing serial numbers.  Value #0# is returned if this
527      function is called before calling #encode_chunk# or #decode_chunk# or
528      after calling #close_codec#. */
529  virtual int  get_serial(void);
530  /*x Sets the chrominance delay parameter.  This function can be called
531      before encoding the first IW44 data chunk.  Parameter #parm# is an
532      encoding delay which reduces the bitrate associated with the
533      chrominance information. The default chrominance encoding delay is 10. */
534  virtual int  parm_crcbdelay(const int parm);
535  /*x Sets the #dbfrac# parameter.  This function can be called before
536      encoding the first IW44 data chunk.  Parameter #frac# modifies the
537      decibel estimation algorithm in such a way that the decibel target only
538      pertains to the average error of the fraction #frac# of the most
539      misrepresented 32x32 pixel blocks.  Setting arguments #frac# to #1.0#
540      restores the normal behavior.  */
541  virtual void parm_dbfrac(float frac);
542protected:
543  // Parameter
544  int   crcb_delay;
545  int   crcb_half;
546  // Data
547private:
548  Codec *ycodec, *cbcodec, *crcodec;
549};
550
551/*x IW44Transform.
552*/
553class IW44Image::Transform
554{
555public:
556  class Decode;
557  class Encode;
558protected:
559  static void filter_begin(int w, int h);
560  static void filter_end(void);
561};
562
563struct GPixel;
564class IW44Image::Transform::Decode : public IW44Image::Transform
565{
566public:
567 // WAVELET TRANSFORM
568  /*x Forward transform. */
569  static void backward(short *p, int w, int h, int rowsize, int begin, int end);
570 
571  // COLOR TRANSFORM
572  /*x Converts YCbCr to RGB. */
573  static void YCbCr_to_RGB(GPixel *p, int w, int h, int rowsize);
574};
575
576//---------------------------------------------------------------
577// *** Class IW44Image::Block [declaration]
578// Represents a block of 32x32 coefficients after zigzagging and scaling
579
580
581class IW44Image::Block // DJVU_CLASS
582{
583public:
584  // creating
585  Block(void);
586  // accessing scaled coefficients
587  short get(int n) const;
588  void  set(int n, int val, IW44Image::Map *map);
589  // converting from liftblock
590  void  read_liftblock(const short *coeff, IW44Image::Map *map);
591  void  write_liftblock(short *coeff, int bmin=0, int bmax=64) const;
592  // sparse array access
593  const short* data(int n) const;
594  short* data(int n, IW44Image::Map *map);
595  void   zero(int n);
596  // sparse representation
597private:
598  short **(pdata[4]);
599};
600
601//---------------------------------------------------------------
602// *** Class IW44Image::Map [declaration]
603// Represents all the blocks of an image
604
605class IW44Image::Map // DJVU_CLASS
606{
607public:
608  class Encode;
609
610  // construction
611  Map(int w, int h);
612  ~Map();
613  // image access
614  void image(signed char *img8, int rowsize, 
615             int pixsep=1, int fast=0);
616  void image(int subsample, const GRect &rect, 
617             signed char *img8, int rowsize, 
618             int pixsep=1, int fast=0);
619  // array of blocks
620  IW44Image::Block *blocks;
621  // geometry
622  int iw, ih;
623  int bw, bh;
624  int nb;
625  // coefficient allocation stuff
626  short *alloc(int n);
627  short **allocp(int n);
628  IW44Image::Alloc *chain;
629  int top;
630  // statistics
631  int get_bucket_count(void) const;
632  unsigned int get_memory_usage(void) const;
633};
634
635//////////////////////////////////////////////////////
636// ENCODING/DECODING WAVELET COEFFICIENTS
637//    USING HIERARCHICAL SET DIFFERENCE
638//////////////////////////////////////////////////////
639
640
641//-----------------------------------------------
642// Class IW44Image::Codec [declaration+implementation]
643// Maintains information shared while encoding or decoding
644
645class IW44Image::Codec
646{
647public:
648  class Decode;
649  class Encode;
650
651protected:
652  // Construction
653  Codec(IW44Image::Map &map);
654public:
655  virtual ~Codec();
656  // Coding
657  int finish_code_slice(ZPCodec &zp);
658  virtual int code_slice(ZPCodec &zp) = 0;
659  // Data
660  IW44Image::Map &map;                  // working map
661  // status
662  int curband;                  // current band
663  int curbit;                   // current bitplane
664  // quantization tables
665  int quant_hi[10];             // quantization for bands 1 to 9
666  int quant_lo[16];             // quantization for band 0.
667  // bucket state
668  char coeffstate[256];
669  char bucketstate[16];
670  enum { ZERO   = 1,            // this coeff never hits this bit
671         ACTIVE = 2,            // this coeff is already active
672         NEW    = 4,            // this coeff is becoming active
673         UNK    = 8 };          // this coeff may become active
674  // coding context
675  BitContext ctxStart [32];
676  BitContext ctxBucket[10][8];
677  BitContext ctxMant;
678  BitContext ctxRoot;
679  // helper
680  int is_null_slice(int bit, int band);
681  int decode_prepare(int fbucket, int nbucket, IW44Image::Block &blk);
682  void decode_buckets(ZPCodec &zp, int bit, int band,
683    IW44Image::Block &blk, int fbucket, int nbucket);
684};
685
686//////////////////////////////////////////////////////
687// DEFINITION OF CHUNK HEADERS
688//////////////////////////////////////////////////////
689
690
691struct IW44Image::PrimaryHeader {
692  unsigned char serial;
693  unsigned char slices;
694  void encode(GP<ByteStream> gbs);
695  void decode(GP<ByteStream> gbs);
696}; 
697
698struct IW44Image::SecondaryHeader {
699  unsigned char major;
700  unsigned char minor;
701  void encode(GP<ByteStream> gbs);
702  void decode(GP<ByteStream> gbs);
703};
704
705struct IW44Image::TertiaryHeader {
706  unsigned char xhi, xlo;
707  unsigned char yhi, ylo;
708  unsigned char crcbdelay;
709  void encode(GP<ByteStream> gbs);
710  void decode(GP<ByteStream> gbs, int major=1, int minor=2);
711};
712
713inline const short* 
714IW44Image::Block::data(int n) const
715{
716  if (! pdata[n>>4])
717    return 0;
718  return pdata[n>>4][n&15];
719}
720
721inline short* 
722IW44Image::Block::data(int n, IW44Image::Map *map)
723{
724  if (! pdata[n>>4])
725    pdata[n>>4] = map->allocp(16);
726  if (! pdata[n>>4][n &15])
727    pdata[n>>4][n &15] = map->alloc(16);
728  return pdata[n>>4][n&15];
729}
730
731inline short 
732IW44Image::Block::get(int n) const
733{
734  int n1 = (n>>4);
735  const short *d = data(n1);
736  if (! d)
737    return 0;
738  return d[n&15];
739}
740
741inline void 
742IW44Image::Block::set(int n, int val, IW44Image::Map *map)
743{
744  int n1 = (n>>4);
745  short* d = data(n1, map);
746  d[n&15] = val;
747}
748
749#endif /* IW44IMAGE_IMPLIMENTATION */
750
751//@}
752
753
754#ifdef HAVE_NAMESPACES
755}
756# ifndef NOT_USING_DJVU_NAMESPACE
757using namespace DJVU;
758# endif
759#endif
760#endif
761
Note: See TracBrowser for help on using the repository browser.