Blame libdjvu/GScaler.h

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifndef _GSCALER_H_
Packit df99a1
#define _GSCALER_H_
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
#include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma interface
Packit df99a1
#endif
Packit df99a1
Packit df99a1
// From: Leon Bottou, 1/31/2002
Packit df99a1
// Almost equal to my initial code.
Packit df99a1
Packit df99a1
#include "GException.h"
Packit df99a1
#include "GRect.h"
Packit df99a1
#include "GBitmap.h"
Packit df99a1
#include "GPixmap.h"
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
/** @name GScaler.h 
Packit df99a1
Packit df99a1
    Files #"GScaler.h"# and #"GScaler.cpp"# implement a fast bilinear
Packit df99a1
    interpolation scheme to rescale a \Ref{GBitmap} or a \Ref{GPixmap}.
Packit df99a1
    Common setup functions are implemented by the base class \Ref{GScaler}.
Packit df99a1
    The actual function for rescaling a gray level image is implemented by
Packit df99a1
    class \Ref{GBitmapScaler}.  The actual function for rescaling a color
Packit df99a1
    image is implemented by class \Ref{GPixmapScaler}.
Packit df99a1
Packit df99a1
    {\bf Remark} --- The bilinear interpolation code relies on fixed precision
Packit df99a1
    tables.  It becomes suboptimal when upsampling (i.e. zooming into) an
Packit df99a1
    image by a factor greater than eight.  High contrast images displayed at
Packit df99a1
    high magnification may contain visible jaggies.
Packit df99a1
Packit df99a1
    @memo
Packit df99a1
    Rescaling images with bilinear interpolation.
Packit df99a1
    @author
Packit df99a1
    L\'eon Bottou <leonb@research.att.com>
Packit df99a1
*/
Packit df99a1
//@{
Packit df99a1
Packit df99a1
Packit df99a1
/** Base class for GBitmapScaler and GPixmapScaler.  This base class
Packit df99a1
    implements the common elements of class \Ref{GBitmapScaler} and
Packit df99a1
    \Ref{GPixmapScaler}.  Functions \Ref{set_input_size} and
Packit df99a1
    \Ref{set_output_size} are used to specify the size of the input image and
Packit df99a1
    the size of the output image.  Functions \Ref{set_horz_ratio} and
Packit df99a1
    \Ref{set_vert_ratio} may be used to override the scaling ratios computed
Packit df99a1
    from the image sizes.  You can then call function \Ref{get_input_rect} to
Packit df99a1
    know which pixels of the input image are necessary to compute a specified
Packit df99a1
    rectangular zone of the output image.  The actual computation is then
Packit df99a1
    performed by calling function #scale# in class \Ref{GBitmapScaler} and
Packit df99a1
    \Ref{GPixmapScaler}.  
Packit df99a1
*/
Packit df99a1
class DJVUAPI GScaler  : public GPEnabled
Packit df99a1
{
Packit df99a1
protected:  
Packit df99a1
  GScaler();
Packit df99a1
public:
Packit df99a1
  virtual ~GScaler();
Packit df99a1
  /** Sets the size of the input image. Argument #w# (resp. #h#) contains the
Packit df99a1
      horizontal (resp. vertical) size of the input image.  This size is used
Packit df99a1
      to initialize the internal data structures of the scaler object. */
Packit df99a1
  void set_input_size(int w, int h);
Packit df99a1
  /** Sets the size of the output image. Argument #w# (resp. #h#) contains the
Packit df99a1
      horizontal (resp. vertical) size of the output image. This size is used
Packit df99a1
      to initialize the internal data structures of the scaler object. */
Packit df99a1
  void set_output_size(int w, int h);
Packit df99a1
  /** Sets the horizontal scaling ratio #numer/denom#.  This function may be
Packit df99a1
      used to force an exact scaling ratio.  The scaling ratios are otherwise
Packit df99a1
      derived from the sizes of the input and output images. */
Packit df99a1
  void set_horz_ratio(int numer, int denom);
Packit df99a1
  /** Sets the vertical scaling ratio to #numer/denom#.  This function may be
Packit df99a1
      used to force an exact scaling ratio.  The scaling ratios are otherwise
Packit df99a1
      derived from the sizes of the input and output images. */
Packit df99a1
  void set_vert_ratio(int numer, int denom);
Packit df99a1
  /** Computes which input pixels are required to compute specified output
Packit df99a1
      pixels.  Let us assume that we only need a part of the output
Packit df99a1
      image. This part is defined by rectangle #desired_output#.  Only a part
Packit df99a1
      of the input image is necessary to compute the output pixels.  Function
Packit df99a1
      #get_input_rect# computes the coordinates of that part of the input
Packit df99a1
      image, and stores them into rectangle #required_input#.  */
Packit df99a1
  void get_input_rect( const GRect &desired_output, GRect &required_input );
Packit df99a1
protected:
Packit df99a1
  // The sizes
Packit df99a1
  int inw, inh;
Packit df99a1
  int xshift, yshift;
Packit df99a1
  int redw, redh;
Packit df99a1
  int outw, outh;
Packit df99a1
  // Fixed point coordinates
Packit df99a1
  int *vcoord;
Packit df99a1
  GPBuffer<int> gvcoord;
Packit df99a1
  int *hcoord;
Packit df99a1
  GPBuffer<int> ghcoord;
Packit df99a1
  // Helper
Packit df99a1
  void make_rectangles(const GRect &desired, GRect &red, GRect &inp;;
Packit df99a1
};
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
/** Fast rescaling code for gray level images.  This class augments the base
Packit df99a1
    class \Ref{GScaler} with a function for rescaling gray level
Packit df99a1
    images.  Function \Ref{GBitmapScaler::scale} computes an arbitrary segment
Packit df99a1
    of the output image given the corresponding pixels in the input image.
Packit df99a1
Packit df99a1
    {\bf Example} --- The following functions returns an gray level image
Packit df99a1
    (sixteen gray levels, size #nw# by #nh#) containing a rescaled version of
Packit df99a1
    the input image #in#.
Packit df99a1
    \begin{verbatim}
Packit df99a1
    GBitmap *rescale_bitmap(const GBitmap &in, int nw, int nh)
Packit df99a1
    {
Packit df99a1
      int w = in.columns();       // Get input width
Packit df99a1
      int h = in.raws();          // Get output width
Packit df99a1
      GBitmapScaler scaler(w,h,nw,nh);  // Creates bitmap scaler
Packit df99a1
      GRect desired(0,0,nw,nh);   // Desired output = complete bitmap
Packit df99a1
      GRect provided(0,0,w,h);    // Provided input = complete bitmap
Packit df99a1
      GBitmap *out = new GBitmap;
Packit df99a1
      scaler.scale(provided, in, desired, *out);  // Rescale
Packit df99a1
      out->change_grays(16);      // Reduce to 16 gray levels
Packit df99a1
      return out;
Packit df99a1
    }
Packit df99a1
    \end{verbatim} */
Packit df99a1
class DJVUAPI GBitmapScaler : public GScaler
Packit df99a1
{
Packit df99a1
protected:
Packit df99a1
  GBitmapScaler(void);
Packit df99a1
  GBitmapScaler(int inw, int inh, int outw, int outh);
Packit df99a1
public:
Packit df99a1
  /// Virtual destructor.
Packit df99a1
  virtual ~GBitmapScaler();
Packit df99a1
Packit df99a1
  /** Creates an empty GBitmapScaler. You must call functions
Packit df99a1
      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size} before
Packit df99a1
      calling any of the scaling functions. */
Packit df99a1
  static GP<GBitmapScaler> create(void) {return new GBitmapScaler(); }
Packit df99a1
Packit df99a1
  /** Creates a GBitmapScaler. The size of the input image is given by
Packit df99a1
      #inw# and #inh#.  This function internally calls
Packit df99a1
      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size}. The
Packit df99a1
      size of the output image is given by #outw# and #outh#.  . */
Packit df99a1
  static GP<GBitmapScaler> create(
Packit df99a1
    const int inw, const int inh, const int outw, const int outh)
Packit df99a1
  { return new GBitmapScaler(inw,inh,outw,outh); }
Packit df99a1
Packit df99a1
  /** Computes a segment of the rescaled output image.  The GBitmap object
Packit df99a1
      #output# is overwritten with the segment of the output image specified
Packit df99a1
      by the rectangle #desired_output#.  The rectangle #provided_input#
Packit df99a1
      specifies which segment of the input image is provided by the GBitmap
Packit df99a1
      object #input#.  An exception \Ref{GException} is thrown if the
Packit df99a1
      rectangle #provided_input# is smaller then the rectangle
Packit df99a1
      #required_input# returned by function \Ref{GScaler::get_input_rect}.
Packit df99a1
      Note that the output image always contain 256 gray levels. You may want
Packit df99a1
      to use function \Ref{GBitmap::change_grays} to reduce the number of gray
Packit df99a1
      levels. */
Packit df99a1
  void scale( const GRect &provided_input, const GBitmap &input,
Packit df99a1
              const GRect &desired_output, GBitmap &output );
Packit df99a1
protected:
Packit df99a1
  // Helpers
Packit df99a1
  unsigned char *get_line(int, const GRect &, const GRect &, const GBitmap &);
Packit df99a1
  // Temporaries
Packit df99a1
  unsigned char *lbuffer;
Packit df99a1
  GPBuffer<unsigned char> glbuffer;
Packit df99a1
  unsigned char *conv;
Packit df99a1
  GPBuffer<unsigned char> gconv;
Packit df99a1
  unsigned char *p1;
Packit df99a1
  GPBuffer<unsigned char> gp1;
Packit df99a1
  unsigned char *p2;
Packit df99a1
  GPBuffer<unsigned char> gp2;
Packit df99a1
  int l1;
Packit df99a1
  int l2;
Packit df99a1
};
Packit df99a1
Packit df99a1
Packit df99a1
/** Fast rescaling code for color images.  This class augments the base class
Packit df99a1
    \Ref{GScaler} with a function for rescaling color images.  Function
Packit df99a1
    \Ref{GPixmapScaler::scale} computes an arbitrary segment of the output
Packit df99a1
    image given the corresponding pixels in the input image.
Packit df99a1
Packit df99a1
    {\bf Example} --- The following functions returns a color image
Packit df99a1
    of size #nw# by #nh# containing a rescaled version of
Packit df99a1
    the input image #in#.
Packit df99a1
    \begin{verbatim}
Packit df99a1
    GPixmap *rescale_pixmap(const GPixmap &in, int nw, int nh)
Packit df99a1
    {
Packit df99a1
      int w = in.columns();       // Get input width
Packit df99a1
      int h = in.raws();          // Get output width
Packit df99a1
      GPixmapScaler scaler(w,h,nw,nh);  // Creates bitmap scaler
Packit df99a1
      GRect desired(0,0,nw,nh);   // Desired output = complete image
Packit df99a1
      GRect provided(0,0,w,h);    // Provided input = complete image
Packit df99a1
      GPixmap *out = new GPixmap;
Packit df99a1
      scaler.scale(provided, in, desired, *out);  // Rescale
Packit df99a1
      return out;
Packit df99a1
    }
Packit df99a1
    \end{verbatim}
Packit df99a1
Packit df99a1
 */
Packit df99a1
class DJVUAPI GPixmapScaler : public GScaler
Packit df99a1
{
Packit df99a1
protected:
Packit df99a1
  GPixmapScaler(void);
Packit df99a1
  GPixmapScaler(int inw, int inh, int outw, int outh);
Packit df99a1
public:
Packit df99a1
  /// Virtual destructor.
Packit df99a1
  virtual ~GPixmapScaler();
Packit df99a1
Packit df99a1
  /** Creates an empty GPixmapScaler. You must call functions
Packit df99a1
      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size} before
Packit df99a1
      calling any of the scaling functions. */
Packit df99a1
  static GP<GPixmapScaler> create(void) {return new GPixmapScaler(); }
Packit df99a1
Packit df99a1
  /** Creates a GPixmapScaler. The size of the input image is given by
Packit df99a1
      #inw# and #inh#.  This function internally calls
Packit df99a1
      \Ref{GScaler::set_input_size} and \Ref{GScaler::set_output_size}. The
Packit df99a1
      size of the output image is given by #outw# and #outh#.  . */
Packit df99a1
  static GP<GPixmapScaler> create(
Packit df99a1
    const int inw, const int inh, const int outw, const int outh)
Packit df99a1
  { return new GPixmapScaler(inw,inh,outw,outh); }
Packit df99a1
Packit df99a1
  /** Computes a segment of the rescaled output image.  The pixmap #output# is
Packit df99a1
      overwritten with the segment of the output image specified by the
Packit df99a1
      rectangle #desired_output#.  The rectangle #provided_input# specifies
Packit df99a1
      which segment of the input image is provided in the pixmap #input#.  An
Packit df99a1
      exception \Ref{GException} is thrown if the rectangle #provided_input#
Packit df99a1
      is smaller then the rectangle #required_input# returned by function
Packit df99a1
      \Ref{GScaler::get_input_rect}. */
Packit df99a1
  void scale( const GRect &provided_input, const GPixmap &input,
Packit df99a1
              const GRect &desired_output, GPixmap &output );
Packit df99a1
protected:
Packit df99a1
  // Helpers
Packit df99a1
  GPixel *get_line(int, const GRect &, const GRect &, const GPixmap &);
Packit df99a1
  // Temporaries
Packit df99a1
  GPixel *lbuffer;
Packit df99a1
  GPBuffer<GPixel> glbuffer;
Packit df99a1
  GPixel *p1;
Packit df99a1
  GPBuffer<GPixel> gp1;
Packit df99a1
  GPixel *p2;
Packit df99a1
  GPBuffer<GPixel> gp2;
Packit df99a1
  int    l1;
Packit df99a1
  int    l2;
Packit df99a1
};
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
//@}
Packit df99a1
    
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
// -------- END
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
#endif