Image Interpolation with Contour Stencils
Defines | Functions

cwinterp.c File Reference

Contour stencil windowed interpolation. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "basic.h"
#include "cwinterp.h"
#include "fitsten.h"
#include "invmat.h"
#include "nninterp.h"
#include "drawline.h"
#include "imageio.h"
Include dependency graph for cwinterp.c:

Go to the source code of this file.

Defines

#define NUMSTENCILS   8
 The number of contour stencils, cardinality of $\Sigma$.
#define NEIGHRADIUS   1
 Cardinality of the neighborhood $\mathcal{N}$.
#define NEIGHDIAMETER   (2*NEIGHRADIUS+1)
#define NUMNEIGH   (NEIGHDIAMETER*NEIGHDIAMETER)
#define CORRECTION_IGNOREBITS   3
 CWRefinementPass residual tolerance.
#define INPUT_FRACBITS   8
 Number of fractional bits in the input array.
#define PSI_FRACBITS   12
 Number of fractional bits in the psi sample arrays.
#define OUTPUT_FRACBITS   (INPUT_FRACBITS + PSI_FRACBITS)
 Number of fractional bits in the output array.
#define FIXED_ONE(N)   (1 << (N))
 The number 1.0 in fixed-point with N fractional bits.
#define FIXED_HALF(N)   (1 << ((N) - 1))
 The number 0.5 in fixed-point with N fractional bits.
#define PIXEL_STRIDE   3
 Number of elements between successive RGB fixed-point pixels.
#define CLAMP(X, A, B)   (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : (X)))
 Clamp X to [A, B].
#define ROUNDCLAMP(X, A, B)   (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X)))
 Round and clamp double X to integer.
#define ROUNDCLAMP(X, A, B)   (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X)))
 Round and clamp double X to integer.
#define XY_FRACBITS   15
#define TRIG_FRACBITS   10
#define PHITN_FRACBITS   9
#define PHI_FRACBITS   10
#define COEFF_FRACBITS   10
#define WINDOW_FRACBITS   10
#define UK_FRACBITS   10
#define ROUND_FIXED(X, N)   (((X) + FIXED_HALF(N)) >> (N))
#define FLOAT_TO_FIXED(X, N)   ((int32_t)ROUND((X) * FIXED_ONE(N)))

Functions

int32_t * PreCWInterp (cwparams Param)
 Precomputations before windowed interpolation CWInterp.
int CWInterp (uint32_t *Output, const uint32_t *Input, int InputWidth, int InputHeight, const int32_t *Psi, cwparams Param)
 Contour stencil windowed interpolation.
int CWArbitraryInterp (uint32_t *Output, int OutputWidth, int OutputHeight, const int32_t *Input, int InputWidth, int InputHeight, const int *Stencil, const double *InverseA, cwparams Param)
 Arbitrary scale factor interpolation.
int CWInterpEx (uint32_t *Output, int OutputWidth, int OutputHeight, const uint32_t *Input, int InputWidth, int InputHeight, const int32_t *Psi, cwparams Param)
 Contour stencil windowed interpolation for arbitrary scale factors.
int DisplayContours (uint32_t *Output, int OutputWidth, int OutputHeight, uint32_t *Input, int InputWidth, int InputHeight, cwparams Param)
 Display the estimated contour orientations.

Detailed Description

Contour stencil windowed interpolation.

Author:
Pascal Getreuer <getreuer@gmail.com>

This file implements contour stencil windowed interpolation for integer scale factors. The interpolation model supposes that the input image $v$ was created by convolution followed by downsampling

\[ v = {\downarrow_r} (h * u) \]

where $u$ is the underlying high resolution image, $h$ is the point- spread function (PSF), and $\downarrow_r$ denotes downsampling by factor $r$. For simplicity, this code requires that $r$ is integer and $h$ is a Gaussian. The standard deviation $h$ is controlled by PsfSigma.

The image is interpolated by the following steps. First, contour stencils are applied to estimate the local contour orientations (in routine FitStencils). The image is then interpolated by the formula

\[ u(x) = \sum_{k\in\mathbb{Z}^2} w(x - k) \Bigl[ v_k + \sum_{n\in\mathcal{N}\backslash\{0\}} (v_{k+n} - v_k) \psi^n_{\mathcal{S}^\star(k)}(x - k) \Bigr] \]

(routine CWFirstPass). This initial interpolation approximately satisfies the degradation model, $v \approx {\downarrow_r} (h * u)$. To improve the accuracy, several correction passes are applied. In each pass, the residual is computed (routine CWResidual) and then applied to refine the interpolation (routine CWRefinementPass). Under typical settings, the degradation model is accurately satisfied after two or three correction passes.

The main computations CWFirstPass and CWRefinementPass are done in fixed-point integer arithmetic. The downsides of using fixed-point arithmetic compared to floating-point arithmetic are more involved code for multiplication and division (it is harder to read) and the range and precision of fixed-point integers must be explicitly managed for accurate results (need to be careful). The upside is that when it does work, fixed- point arithmetic is significantly faster. Fortunately, in this application, the only needed operations are fixed-point additions and fixed-point multiplies where both factors are in a predictable range of values. These conditions are good for fixed-point arithmetic.

The number of fractional bits used to represent $v$ and the residual is controlled by INPUT_FRACBITS. The number of fractional bits in representing $\psi$ is PSI_FRACBITS. Since $v$ and $\psi$ are multiplied, the output has OUTPUT_FRACBITS = (INPUT_FRACBITS + PSI_FRACBITS) fractional bits. These constants must be balanced between precision and avoiding overflow. Fortunately this balance is easy to find for this application (neither extreme range nor extreme precision are needed).

Copyright (c) 2010-2011, Pascal Getreuer All rights reserved.

This program is free software: you can use, modify and/or redistribute it under the terms of the simplified BSD License. You should have received a copy of this license along this program. If not, see <http://www.opensource.org/licenses/bsd-license.html>.

Definition in file cwinterp.c.


Define Documentation

#define CLAMP (   X,
  A,
 
)    (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : (X)))

Clamp X to [A, B].

Definition at line 120 of file cwinterp.c.

#define COEFF_FRACBITS   10

Definition at line 962 of file cwinterp.c.

#define CORRECTION_IGNOREBITS   3

CWRefinementPass residual tolerance.

In the correction passes, pixels will be skipped if they have magnitude less than 2^(-INPUT_FRACBITS + CORRECTION_IGNOREBITS), which improves the speed. A larger value of CORRECTION_IGNOREBITS makes the correction passes faster, but less accurate.

Definition at line 89 of file cwinterp.c.

#define FIXED_HALF (   N)    (1 << ((N) - 1))

The number 0.5 in fixed-point with N fractional bits.

Definition at line 101 of file cwinterp.c.

#define FIXED_ONE (   N)    (1 << (N))

The number 1.0 in fixed-point with N fractional bits.

Definition at line 99 of file cwinterp.c.

#define FLOAT_TO_FIXED (   X,
 
)    ((int32_t)ROUND((X) * FIXED_ONE(N)))

Definition at line 967 of file cwinterp.c.

#define INPUT_FRACBITS   8

Number of fractional bits in the input array.

Definition at line 92 of file cwinterp.c.

#define NEIGHDIAMETER   (2*NEIGHRADIUS+1)

Definition at line 78 of file cwinterp.c.

#define NEIGHRADIUS   1

Cardinality of the neighborhood $\mathcal{N}$.

Definition at line 77 of file cwinterp.c.

#define NUMNEIGH   (NEIGHDIAMETER*NEIGHDIAMETER)

Definition at line 79 of file cwinterp.c.

#define NUMSTENCILS   8

The number of contour stencils, cardinality of $\Sigma$.

Definition at line 74 of file cwinterp.c.

#define OUTPUT_FRACBITS   (INPUT_FRACBITS + PSI_FRACBITS)

Number of fractional bits in the output array.

Definition at line 96 of file cwinterp.c.

#define PHI_FRACBITS   10

Definition at line 961 of file cwinterp.c.

#define PHITN_FRACBITS   9

Definition at line 960 of file cwinterp.c.

#define PIXEL_STRIDE   3

Number of elements between successive RGB fixed-point pixels.

PIXEL_STRIDE is the number of elements between successive pixels in the RGB fixed-point representation used internally by the interpolation.

Note:
Unlike the other defines here, this one cannot be changed without also rewriting much of the code. Its purpose is more for clarity rather than working as a parameter.

Definition at line 114 of file cwinterp.c.

#define PSI_FRACBITS   12

Number of fractional bits in the psi sample arrays.

Definition at line 94 of file cwinterp.c.

#define ROUND_FIXED (   X,
 
)    (((X) + FIXED_HALF(N)) >> (N))

Definition at line 966 of file cwinterp.c.

#define ROUNDCLAMP (   X,
  A,
 
)    (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X)))

Round and clamp double X to integer.

Definition at line 945 of file cwinterp.c.

#define ROUNDCLAMP (   X,
  A,
 
)    (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X)))

Round and clamp double X to integer.

Definition at line 945 of file cwinterp.c.

#define TRIG_FRACBITS   10

Definition at line 959 of file cwinterp.c.

#define UK_FRACBITS   10

Definition at line 964 of file cwinterp.c.

#define WINDOW_FRACBITS   10

Definition at line 963 of file cwinterp.c.

#define XY_FRACBITS   15

Definition at line 958 of file cwinterp.c.


Function Documentation

int CWArbitraryInterp ( uint32_t *  Output,
int  OutputWidth,
int  OutputHeight,
const int32_t *  Input,
int  InputWidth,
int  InputHeight,
const int *  Stencil,
const double *  InverseA,
cwparams  Param 
)

Arbitrary scale factor interpolation.

Definition at line 970 of file cwinterp.c.

Here is the caller graph for this function:

int CWInterp ( uint32_t *  Output,
const uint32_t *  Input,
int  InputWidth,
int  InputHeight,
const int32_t *  Psi,
cwparams  Param 
)

Contour stencil windowed interpolation.

Parameters:
Outputpointer to memory for holding the interpolated image
Inputthe input image
InputWidth,InputHeightinput image dimensions
Psi$\psi$ samples computed by PreCWInterp
Paramcwparams struct of interpolation parameters

Definition at line 851 of file cwinterp.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int CWInterpEx ( uint32_t *  Output,
int  OutputWidth,
int  OutputHeight,
const uint32_t *  Input,
int  InputWidth,
int  InputHeight,
const int32_t *  Psi,
cwparams  Param 
)

Contour stencil windowed interpolation for arbitrary scale factors.

Parameters:
Outputpointer to memory for holding the interpolated image
OutputWidth,OutputHeightoutput image dimensions
Inputthe input image
InputWidth,InputHeightinput image dimensions
Psi$\psi$ samples computed by PreCWInterp
Paramcwparams struct of interpolation parameters

Definition at line 1274 of file cwinterp.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int DisplayContours ( uint32_t *  Output,
int  OutputWidth,
int  OutputHeight,
uint32_t *  Input,
int  InputWidth,
int  InputHeight,
cwparams  Param 
)

Display the estimated contour orientations.

Definition at line 1395 of file cwinterp.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t* PreCWInterp ( cwparams  Param)

Precomputations before windowed interpolation CWInterp.

Parameters:
Paramcwparams struct of interpolation parameters
Returns:
Pointer to $\psi$ samples array, or null on failure

PreCWInterp precomputes samples of the $\psi$ functions,

\[ \psi^n_\mathcal{S}(x) = \sum_{m\in\mathcal{N}} (A_\mathcal{S})^{-1}_{m,n} \varphi^m_\mathcal{S}(x - m). \]

The routine allocates memory to store the samples and returns a pointer to this memory. It is the responsibility of the caller to call free on this pointer when done to release the memory.

A non-null pointer indicates success. On failure, the returned pointer is null.

Definition at line 323 of file cwinterp.c.

Here is the caller graph for this function:

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines