Image Interpolation with Contour Stencils
|
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"
Go to the source code of this file.
Macros | |
#define | NUMSTENCILS 8 |
The number of contour stencils, cardinality of ![]() | |
#define | NEIGHRADIUS 1 |
Cardinality of the neighborhood ![]() | |
#define | NEIGHDIAMETER (2*NEIGHRADIUS+1) |
#define | NUMNEIGH (NEIGHDIAMETER*NEIGHDIAMETER) |
#define | CORRECTION_IGNOREBITS 3 |
CWRefinementPass residual tolerance. More... | |
#define | INPUT_FRACBITS 8 |
Number of fractional bits in the input array. More... | |
#define | PSI_FRACBITS 12 |
Number of fractional bits in the psi sample arrays. More... | |
#define | OUTPUT_FRACBITS (INPUT_FRACBITS + PSI_FRACBITS) |
Number of fractional bits in the output array. More... | |
#define | FIXED_ONE(N) (1 << (N)) |
The number 1.0 in fixed-point with N fractional bits. More... | |
#define | FIXED_HALF(N) (1 << ((N) - 1)) |
The number 0.5 in fixed-point with N fractional bits. More... | |
#define | PIXEL_STRIDE 3 |
Number of elements between successive RGB fixed-point pixels. More... | |
#define | CLAMP(X, A, B) (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : (X))) |
Clamp X to [A, B]. More... | |
#define | ROUNDCLAMP(X, A, B) (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X))) |
Round and clamp double X to integer. More... | |
#define | ROUNDCLAMP(X, A, B) (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X))) |
Round and clamp double X to integer. More... | |
#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 . More... | |
int | CWInterp (uint32_t *Output, const uint32_t *Input, int InputWidth, int InputHeight, const int32_t *Psi, cwparams Param) |
Contour stencil windowed interpolation. More... | |
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. More... | |
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. More... | |
int | DisplayContours (uint32_t *Output, int OutputWidth, int OutputHeight, uint32_t *Input, int InputWidth, int InputHeight, cwparams Param) |
Display the estimated contour orientations. More... | |
Contour stencil windowed interpolation.
This file implements contour stencil windowed interpolation for integer scale factors. The interpolation model supposes that the input image was created by convolution followed by downsampling
where is the underlying high resolution image,
is the point- spread function (PSF), and
denotes downsampling by factor
. For simplicity, this code requires that
is integer and
is a Gaussian. The standard deviation
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
(routine CWFirstPass
). This initial interpolation approximately satisfies the degradation model, . 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 and the residual is controlled by
INPUT_FRACBITS
. The number of fractional bits in representing is
PSI_FRACBITS
. Since and
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 CLAMP | ( | X, | |
A, | |||
B | |||
) | (((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 958 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.
Definition at line 963 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 .
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 .
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 957 of file cwinterp.c.
#define PHITN_FRACBITS 9 |
Definition at line 956 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.
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, | |
N | |||
) | (((X) + FIXED_HALF(N)) >> (N)) |
Definition at line 962 of file cwinterp.c.
#define ROUNDCLAMP | ( | X, | |
A, | |||
B | |||
) | (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X))) |
Round and clamp double X to integer.
Definition at line 941 of file cwinterp.c.
#define ROUNDCLAMP | ( | X, | |
A, | |||
B | |||
) | (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : ROUND(X))) |
Round and clamp double X to integer.
Definition at line 941 of file cwinterp.c.
#define TRIG_FRACBITS 10 |
Definition at line 955 of file cwinterp.c.
#define UK_FRACBITS 10 |
Definition at line 960 of file cwinterp.c.
#define WINDOW_FRACBITS 10 |
Definition at line 959 of file cwinterp.c.
#define XY_FRACBITS 15 |
Definition at line 954 of file cwinterp.c.
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 966 of file cwinterp.c.
int CWInterp | ( | uint32_t * | Output, |
const uint32_t * | Input, | ||
int | InputWidth, | ||
int | InputHeight, | ||
const int32_t * | Psi, | ||
cwparams | Param | ||
) |
Contour stencil windowed interpolation.
Output | pointer to memory for holding the interpolated image |
Input | the input image |
InputWidth,InputHeight | input image dimensions |
Psi | ![]() PreCWInterp |
Param | cwparams struct of interpolation parameters |
Definition at line 847 of file cwinterp.c.
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.
Output | pointer to memory for holding the interpolated image |
OutputWidth,OutputHeight | output image dimensions |
Input | the input image |
InputWidth,InputHeight | input image dimensions |
Psi | ![]() PreCWInterp |
Param | cwparams struct of interpolation parameters |
Definition at line 1270 of file cwinterp.c.
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 1391 of file cwinterp.c.
int32_t* PreCWInterp | ( | cwparams | Param | ) |
Precomputations before windowed interpolation CWInterp
.
Param | cwparams struct of interpolation parameters |
PreCWInterp
precomputes samples of the functions,
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 319 of file cwinterp.c.