Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImageToolkit/BSplineRotate.cpp view on Meta::CPAN
Line[x] = (double)*Image++;
}
}
/**
InitialAntiCausalCoefficient
@param c Coefficients
@param DataLength Number of samples or coefficients
@param z Actual pole
@return
*/
static double
InitialAntiCausalCoefficient(double *c, long DataLength, double z) {
// this initialization corresponds to mirror boundaries
return((z / (z * z - 1.0)) * (z * c[DataLength - 2L] + c[DataLength - 1L]));
}
/**
PutColumn
@param Image Output image array
@param Width Width of the image
@param x x coordinate of the selected line
@param Line Input linear array
@param Height Length of the line and height of the image
*/
static void
PutColumn(double *Image, long Width, long x, double *Line, long Height) {
long y;
Image = Image + x;
for(y = 0L; y < Height; y++) {
*Image = (double)Line[y];
Image += Width;
}
}
/**
PutRow
@param Image Output image array
@param y y coordinate of the selected line
@param Line Input linear array
@param Width length of the line and width of the image
*/
static void
PutRow(double *Image, long y, double *Line, long Width) {
long x;
Image = Image + (y * Width);
for(x = 0L; x < Width; x++) {
*Image++ = (double)Line[x];
}
}
/**
SamplesToCoefficients.<br>
Implement the algorithm that converts the image samples into B-spline coefficients.
This efficient procedure essentially relies on the three papers cited above;
data are processed in-place.
Even though this algorithm is robust with respect to quantization,
we advocate the use of a floating-point format for the data.
@param Image Input / Output image (in-place processing)
@param Width Width of the image
@param Height Height of the image
@param spline_degree Degree of the spline model
@return Returns true if success, false otherwise
*/
static bool
SamplesToCoefficients(double *Image, long Width, long Height, long spline_degree) {
double *Line;
double Pole[2];
long NbPoles;
long x, y;
// recover the poles from a lookup table
switch (spline_degree) {
case 2L:
NbPoles = 1L;
Pole[0] = sqrt(8.0) - 3.0;
break;
case 3L:
NbPoles = 1L;
Pole[0] = sqrt(3.0) - 2.0;
break;
case 4L:
NbPoles = 2L;
Pole[0] = sqrt(664.0 - sqrt(438976.0)) + sqrt(304.0) - 19.0;
Pole[1] = sqrt(664.0 + sqrt(438976.0)) - sqrt(304.0) - 19.0;
break;
case 5L:
NbPoles = 2L;
Pole[0] = sqrt(135.0 / 2.0 - sqrt(17745.0 / 4.0)) + sqrt(105.0 / 4.0)
- 13.0 / 2.0;
Pole[1] = sqrt(135.0 / 2.0 + sqrt(17745.0 / 4.0)) - sqrt(105.0 / 4.0)
- 13.0 / 2.0;
break;
default:
// Invalid spline degree
return false;
}
// convert the image samples into interpolation coefficients
// in-place separable process, along x
Line = (double *)malloc(Width * sizeof(double));
if (Line == NULL) {
// Row allocation failed
return false;
}
for (y = 0L; y < Height; y++) {
GetRow(Image, y, Line, Width);
ConvertToInterpolationCoefficients(Line, Width, Pole, NbPoles, DBL_EPSILON);
PutRow(Image, y, Line, Width);
}
free(Line);
// in-place separable process, along y
Line = (double *)malloc(Height * sizeof(double));
if (Line == NULL) {
// Column allocation failed
return false;
}
for (x = 0L; x < Width; x++) {
GetColumn(Image, Width, x, Line, Height);
ConvertToInterpolationCoefficients(Line, Height, Pole, NbPoles, DBL_EPSILON);
PutColumn(Image, Width, x, Line, Height);
}
free(Line);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Interpolation routines
/**
Perform the bidimensional interpolation of an image.
Given an array of spline coefficients, return the value of
the underlying continuous spline model, sampled at the location (x, y).
The model degree can be 2 (quadratic), 3 (cubic), 4 (quartic), or 5 (quintic).
@param Bcoeff Input B-spline array of coefficients
@param Width Width of the image
@param Height Height of the image
@param x x coordinate where to interpolate
@param y y coordinate where to interpolate
@param spline_degree Degree of the spline model
@return Returns the value of the underlying continuous spline model,
sampled at the location (x, y)
*/
static double
InterpolatedValue(double *Bcoeff, long Width, long Height, double x, double y, long spline_degree) {
double *p;
double xWeight[6], yWeight[6];
double interpolated;
double w, w2, w4, t, t0, t1;
long xIndex[6], yIndex[6];
long Width2 = 2L * Width - 2L, Height2 = 2L * Height - 2L;
long i, j, k;
// compute the interpolation indexes
if (spline_degree & 1L) {
i = (long)floor(x) - spline_degree / 2L;
j = (long)floor(y) - spline_degree / 2L;
for(k = 0; k <= spline_degree; k++) {
xIndex[k] = i++;
yIndex[k] = j++;
}
}
else {
i = (long)floor(x + 0.5) - spline_degree / 2L;
j = (long)floor(y + 0.5) - spline_degree / 2L;
for (k = 0; k <= spline_degree; k++) {
xIndex[k] = i++;
yIndex[k] = j++;
}
}
src/Source/FreeImageToolkit/BSplineRotate.cpp view on Meta::CPAN
// clamp and convert to BYTE
dst_bits[x] = (BYTE)MIN(MAX((int)0, (int)(p + 0.5)), (int)255);
}
}
// free working array and return
free(ImageRasterArray);
return dst;
}
/**
Image rotation using a 3rd order (cubic) B-Splines.
@param dib Input dib (8, 24 or 32-bit)
@param angle Output image rotation
@param x_shift Output image horizontal shift
@param y_shift Output image vertical shift
@param x_origin Output origin of the x-axis
@param y_origin Output origin of the y-axis
@param use_mask Whether or not to mask the image
@return Returns the translated & rotated dib if successful, returns NULL otherwise
*/
FIBITMAP * DLL_CALLCONV
FreeImage_RotateEx(FIBITMAP *dib, double angle, double x_shift, double y_shift, double x_origin, double y_origin, BOOL use_mask) {
int x, y, bpp;
int channel, nb_channels;
BYTE *src_bits, *dst_bits;
FIBITMAP *src8 = NULL, *dst8 = NULL, *dst = NULL;
if(!FreeImage_HasPixels(dib)) return NULL;
try {
bpp = FreeImage_GetBPP(dib);
if(bpp == 8) {
FIBITMAP *dst_8 = Rotate8Bit(dib, angle, x_shift, y_shift, x_origin, y_origin, ROTATE_CUBIC, use_mask);
if(dst_8) {
// copy metadata from src to dst
FreeImage_CloneMetadata(dst_8, dib);
}
return dst_8;
}
if((bpp == 24) || (bpp == 32)) {
// allocate dst image
int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);
if( bpp == 24 ) {
dst = FreeImage_Allocate(width, height, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dst = FreeImage_Allocate(width, height, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
}
if(!dst) throw(1);
// allocate a temporary 8-bit dib (no need to build a palette)
src8 = FreeImage_Allocate(width, height, 8);
if(!src8) throw(1);
// process each channel separately
// -------------------------------
nb_channels = (bpp / 8);
for(channel = 0; channel < nb_channels; channel++) {
// extract channel from source dib
for(y = 0; y < height; y++) {
src_bits = FreeImage_GetScanLine(dib, y);
dst_bits = FreeImage_GetScanLine(src8, y);
for(x = 0; x < width; x++) {
dst_bits[x] = src_bits[channel];
src_bits += nb_channels;
}
}
// process channel
dst8 = Rotate8Bit(src8, angle, x_shift, y_shift, x_origin, y_origin, ROTATE_CUBIC, use_mask);
if(!dst8) throw(1);
// insert channel to destination dib
for(y = 0; y < height; y++) {
src_bits = FreeImage_GetScanLine(dst8, y);
dst_bits = FreeImage_GetScanLine(dst, y);
for(x = 0; x < width; x++) {
dst_bits[channel] = src_bits[x];
dst_bits += nb_channels;
}
}
FreeImage_Unload(dst8);
}
FreeImage_Unload(src8);
// copy metadata from src to dst
FreeImage_CloneMetadata(dst, dib);
return dst;
}
} catch(int) {
if(src8) FreeImage_Unload(src8);
if(dst8) FreeImage_Unload(dst8);
if(dst) FreeImage_Unload(dst);
}
return NULL;
}
( run in 1.462 second using v1.01-cache-2.11-cpan-ceb78f64989 )