source: trunk/libdjvu/GScaler.h @ 15

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

needed libs update

File size: 12.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: GScaler.h,v 1.9 2003/11/07 22:08:21 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifndef _GSCALER_H_
58#define _GSCALER_H_
59#ifdef HAVE_CONFIG_H
60#include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma interface
64#endif
65
66// From: Leon Bottou, 1/31/2002
67// Almost equal to my initial code.
68
69#include "GException.h"
70#include "GRect.h"
71#include "GBitmap.h"
72#include "GPixmap.h"
73
74#ifdef HAVE_NAMESPACES
75namespace DJVU {
76# ifdef NOT_DEFINED // Just to fool emacs c++ mode
77}
78#endif
79#endif
80
81
82/** @name GScaler.h
83
84    Files #"GScaler.h"# and #"GScaler.cpp"# implement a fast bilinear
85    interpolation scheme to rescale a \Ref{GBitmap} or a \Ref{GPixmap}.
86    Common setup functions are implemented by the base class \Ref{GScaler}.
87    The actual function for rescaling a gray level image is implemented by
88    class \Ref{GBitmapScaler}.  The actual function for rescaling a color
89    image is implemented by class \Ref{GPixmapScaler}.
90
91    {\bf Remark} --- The bilinear interpolation code relies on fixed precision
92    tables.  It becomes suboptimal when upsampling (i.e. zooming into) an
93    image by a factor greater than eight.  High contrast images displayed at
94    high magnification may contain visible jaggies.
95
96    @memo
97    Rescaling images with bilinear interpolation.
98    @author
99    L\'eon Bottou <leonb@research.att.com>
100    @version
101    #$Id: GScaler.h,v 1.9 2003/11/07 22:08:21 leonb Exp $# */
102//@{
103
104
105/** Base class for GBitmapScaler and GPixmapScaler.  This base class
106    implements the common elements of class \Ref{GBitmapScaler} and
107    \Ref{GPixmapScaler}.  Functions \Ref{set_input_size} and
108    \Ref{set_output_size} are used to specify the size of the input image and
109    the size of the output image.  Functions \Ref{set_horz_ratio} and
110    \Ref{set_vert_ratio} may be used to override the scaling ratios computed
111    from the image sizes.  You can then call function \Ref{get_input_rect} to
112    know which pixels of the input image are necessary to compute a specified
113    rectangular zone of the output image.  The actual computation is then
114    performed by calling function #scale# in class \Ref{GBitmapScaler} and
115    \Ref{GPixmapScaler}. 
116*/
117class GScaler  : public GPEnabled
118{
119protected: 
120  GScaler();
121public:
122  virtual ~GScaler();
123  /** Sets the size of the input image. Argument #w# (resp. #h#) contains the
124      horizontal (resp. vertical) size of the input image.  This size is used
125      to initialize the internal data structures of the scaler object. */
126  void set_input_size(int w, int h);
127  /** Sets the size of the output image. Argument #w# (resp. #h#) contains the
128      horizontal (resp. vertical) size of the output image. This size is used
129      to initialize the internal data structures of the scaler object. */
130  void set_output_size(int w, int h);
131  /** Sets the horizontal scaling ratio #numer/denom#.  This function may be
132      used to force an exact scaling ratio.  The scaling ratios are otherwise
133      derived from the sizes of the input and output images. */
134  void set_horz_ratio(int numer, int denom);
135  /** Sets the vertical scaling ratio to #numer/denom#.  This function may be
136      used to force an exact scaling ratio.  The scaling ratios are otherwise
137      derived from the sizes of the input and output images. */
138  void set_vert_ratio(int numer, int denom);
139  /** Computes which input pixels are required to compute specified output
140      pixels.  Let us assume that we only need a part of the output
141      image. This part is defined by rectangle #desired_output#.  Only a part
142      of the input image is necessary to compute the output pixels.  Function
143      #get_input_rect# computes the coordinates of that part of the input
144      image, and stores them into rectangle #required_input#.  */
145  void get_input_rect( const GRect &desired_output, GRect &required_input );
146protected:
147  // The sizes
148  int inw, inh;
149  int xshift, yshift;
150  int redw, redh;
151  int outw, outh;
152  // Fixed point coordinates
153  int *vcoord;
154  GPBuffer<int> gvcoord;
155  int *hcoord;
156  GPBuffer<int> ghcoord;
157  // Helper
158  void make_rectangles(const GRect &desired, GRect &red, GRect &inp);
159};
160
161
162
163/** Fast rescaling code for gray level images.  This class augments the base
164    class \Ref{GScaler} with a function for rescaling gray level
165    images.  Function \Ref{GBitmapScaler::scale} computes an arbitrary segment
166    of the output image given the corresponding pixels in the input image.
167
168    {\bf Example} --- The following functions returns an gray level image
169    (sixteen gray levels, size #nw# by #nh#) containing a rescaled version of
170    the input image #in#.
171    \begin{verbatim}
172    GBitmap *rescale_bitmap(const GBitmap &in, int nw, int nh)
173    {
174      int w = in.columns();       // Get input width
175      int h = in.raws();          // Get output width
176      GBitmapScaler scaler(w,h,nw,nh);  // Creates bitmap scaler
177      GRect desired(0,0,nw,nh);   // Desired output = complete bitmap
178      GRect provided(0,0,w,h);    // Provided input = complete bitmap
179      GBitmap *out = new GBitmap;
180      scaler.scale(provided, in, desired, *out);  // Rescale
181      out->change_grays(16);      // Reduce to 16 gray levels
182      return out;
183    }
184    \end{verbatim} */
185class GBitmapScaler : public GScaler
186{
187protected:
188  GBitmapScaler(void);
189  GBitmapScaler(int inw, int inh, int outw, int outh);
190public:
191  /// Virtual destructor.
192  virtual ~GBitmapScaler();
193
194  /** Creates an empty GBitmapScaler. You must call functions
195      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size} before
196      calling any of the scaling functions. */
197  static GP<GBitmapScaler> create(void) {return new GBitmapScaler(); }
198
199  /** Creates a GBitmapScaler. The size of the input image is given by
200      #inw# and #inh#.  This function internally calls
201      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size}. The
202      size of the output image is given by #outw# and #outh#.  . */
203  static GP<GBitmapScaler> create(
204    const int inw, const int inh, const int outw, const int outh)
205  { return new GBitmapScaler(inw,inh,outw,outh); }
206
207  /** Computes a segment of the rescaled output image.  The GBitmap object
208      #output# is overwritten with the segment of the output image specified
209      by the rectangle #desired_output#.  The rectangle #provided_input#
210      specifies which segment of the input image is provided by the GBitmap
211      object #input#.  An exception \Ref{GException} is thrown if the
212      rectangle #provided_input# is smaller then the rectangle
213      #required_input# returned by function \Ref{GScaler::get_input_rect}.
214      Note that the output image always contain 256 gray levels. You may want
215      to use function \Ref{GBitmap::change_grays} to reduce the number of gray
216      levels. */
217  void scale( const GRect &provided_input, const GBitmap &input,
218              const GRect &desired_output, GBitmap &output );
219protected:
220  // Helpers
221  unsigned char *get_line(int, const GRect &, const GRect &, const GBitmap &);
222  // Temporaries
223  unsigned char *lbuffer;
224  GPBuffer<unsigned char> glbuffer;
225  unsigned char *conv;
226  GPBuffer<unsigned char> gconv;
227  unsigned char *p1;
228  GPBuffer<unsigned char> gp1;
229  unsigned char *p2;
230  GPBuffer<unsigned char> gp2;
231  int l1;
232  int l2;
233};
234
235
236/** Fast rescaling code for color images.  This class augments the base class
237    \Ref{GScaler} with a function for rescaling color images.  Function
238    \Ref{GPixmapScaler::scale} computes an arbitrary segment of the output
239    image given the corresponding pixels in the input image.
240
241    {\bf Example} --- The following functions returns a color image
242    of size #nw# by #nh# containing a rescaled version of
243    the input image #in#.
244    \begin{verbatim}
245    GPixmap *rescale_pixmap(const GPixmap &in, int nw, int nh)
246    {
247      int w = in.columns();       // Get input width
248      int h = in.raws();          // Get output width
249      GPixmapScaler scaler(w,h,nw,nh);  // Creates bitmap scaler
250      GRect desired(0,0,nw,nh);   // Desired output = complete image
251      GRect provided(0,0,w,h);    // Provided input = complete image
252      GPixmap *out = new GPixmap;
253      scaler.scale(provided, in, desired, *out);  // Rescale
254      return out;
255    }
256    \end{verbatim}
257
258 */
259class GPixmapScaler : public GScaler
260{
261protected:
262  GPixmapScaler(void);
263  GPixmapScaler(int inw, int inh, int outw, int outh);
264public:
265  /// Virtual destructor.
266  virtual ~GPixmapScaler();
267
268  /** Creates an empty GPixmapScaler. You must call functions
269      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size} before
270      calling any of the scaling functions. */
271  static GP<GPixmapScaler> create(void) {return new GPixmapScaler(); }
272
273  /** Creates a GPixmapScaler. The size of the input image is given by
274      #inw# and #inh#.  This function internally calls
275      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size}. The
276      size of the output image is given by #outw# and #outh#.  . */
277  static GP<GPixmapScaler> create(
278    const int inw, const int inh, const int outw, const int outh)
279  { return new GPixmapScaler(inw,inh,outw,outh); }
280
281  /** Computes a segment of the rescaled output image.  The pixmap #output# is
282      overwritten with the segment of the output image specified by the
283      rectangle #desired_output#.  The rectangle #provided_input# specifies
284      which segment of the input image is provided in the pixmap #input#.  An
285      exception \Ref{GException} is thrown if the rectangle #provided_input#
286      is smaller then the rectangle #required_input# returned by function
287      \Ref{GScaler::get_input_rect}. */
288  void scale( const GRect &provided_input, const GPixmap &input,
289              const GRect &desired_output, GPixmap &output );
290protected:
291  // Helpers
292  GPixel *get_line(int, const GRect &, const GRect &, const GPixmap &);
293  // Temporaries
294  GPixel *lbuffer;
295  GPBufferBase glbuffer;
296  GPixel *p1;
297  GPBufferBase gp1;
298  GPixel *p2;
299  GPBufferBase gp2;
300  int    l1;
301  int    l2;
302};
303
304
305
306
307
308//@}
309   
310
311
312
313// -------- END
314
315#ifdef HAVE_NAMESPACES
316}
317# ifndef NOT_USING_DJVU_NAMESPACE
318using namespace DJVU;
319# endif
320#endif
321#endif
Note: See TracBrowser for help on using the repository browser.