Add support for bilinear resize algorithm to wxImage.

Add wxIMAGE_QUALITY_BILINEAR in addition to the existing wxIMAGE_QUALITY_BICUBIC,
it is supposed to be much faster yet yield almost the same results.

Closes #11034.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61791 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-08-30 21:11:37 +00:00
parent 16b0c55398
commit 180f3c7461
4 changed files with 209 additions and 81 deletions

View File

@@ -384,7 +384,8 @@ wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
return image;
}
wxImage wxImage::Scale( int width, int height, int quality ) const
wxImage
wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const
{
wxImage image;
@@ -404,78 +405,32 @@ wxImage wxImage::Scale( int width, int height, int quality ) const
if ( old_width == width && old_height == height )
return *this;
// Scale the image (...or more appropriately, resample the image) using
// either the high-quality or normal method as specified
if ( quality == wxIMAGE_QUALITY_HIGH )
// resample the image using either the nearest neighbourhood, bilinear or
// bicubic method as specified
switch ( quality )
{
// We need to check whether we are downsampling or upsampling the image
if ( width < old_width && height < old_height )
{
// Downsample the image using the box averaging method for best results
image = ResampleBox(width, height);
}
else
{
// For upsampling or other random/wierd image dimensions we'll use
// a bicubic b-spline scaling method
image = ResampleBicubic(width, height);
}
}
else // Default scaling method == simple pixel replication
{
if ( old_width % width == 0 && old_width >= width &&
old_height % height == 0 && old_height >= height )
{
return ShrinkBy( old_width / width , old_height / height ) ;
}
image.Create( width, height, false );
case wxIMAGE_QUALITY_BICUBIC:
case wxIMAGE_QUALITY_BILINEAR:
// both of these algorithms should be used for up-sampling the
// image only, when down-sampling always use box averaging for best
// results
if ( width < old_width && height < old_height )
image = ResampleBox(width, height);
else if ( quality == wxIMAGE_QUALITY_BILINEAR )
image = ResampleBilinear(width, height);
else if ( quality == wxIMAGE_QUALITY_BICUBIC )
image = ResampleBicubic(width, height);
break;
unsigned char *data = image.GetData();
wxCHECK_MSG( data, image, wxT("unable to create image") );
unsigned char *source_data = M_IMGDATA->m_data;
unsigned char *target_data = data;
unsigned char *source_alpha = 0 ;
unsigned char *target_alpha = 0 ;
if ( !M_IMGDATA->m_hasMask )
{
source_alpha = M_IMGDATA->m_alpha ;
if ( source_alpha )
case wxIMAGE_QUALITY_NEAREST:
if ( old_width % width == 0 && old_width >= width &&
old_height % height == 0 && old_height >= height )
{
image.SetAlpha() ;
target_alpha = image.GetAlpha() ;
}
}
long x_delta = (old_width<<16) / width;
long y_delta = (old_height<<16) / height;
unsigned char* dest_pixel = target_data;
long y = 0;
for ( long j = 0; j < height; j++ )
{
unsigned char* src_line = &source_data[(y>>16)*old_width*3];
unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
long x = 0;
for ( long i = 0; i < width; i++ )
{
unsigned char* src_pixel = &src_line[(x>>16)*3];
unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
dest_pixel[0] = src_pixel[0];
dest_pixel[1] = src_pixel[1];
dest_pixel[2] = src_pixel[2];
dest_pixel += 3;
if ( source_alpha )
*(target_alpha++) = *src_alpha_pixel ;
x += x_delta;
return ShrinkBy( old_width / width , old_height / height );
}
y += y_delta;
}
image = ResampleNearest(width, height);
break;
}
// If the original image has a mask, apply the mask to the new image
@@ -497,6 +452,63 @@ wxImage wxImage::Scale( int width, int height, int quality ) const
return image;
}
wxImage wxImage::ResampleNearest(int width, int height) const
{
wxImage image;
image.Create( width, height, false );
unsigned char *data = image.GetData();
wxCHECK_MSG( data, image, wxT("unable to create image") );
unsigned char *source_data = M_IMGDATA->m_data;
unsigned char *target_data = data;
unsigned char *source_alpha = 0 ;
unsigned char *target_alpha = 0 ;
if ( !M_IMGDATA->m_hasMask )
{
source_alpha = M_IMGDATA->m_alpha ;
if ( source_alpha )
{
image.SetAlpha() ;
target_alpha = image.GetAlpha() ;
}
}
long old_height = M_IMGDATA->m_height,
old_width = M_IMGDATA->m_width;
long x_delta = (old_width<<16) / width;
long y_delta = (old_height<<16) / height;
unsigned char* dest_pixel = target_data;
long y = 0;
for ( long j = 0; j < height; j++ )
{
unsigned char* src_line = &source_data[(y>>16)*old_width*3];
unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
long x = 0;
for ( long i = 0; i < width; i++ )
{
unsigned char* src_pixel = &src_line[(x>>16)*3];
unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
dest_pixel[0] = src_pixel[0];
dest_pixel[1] = src_pixel[1];
dest_pixel[2] = src_pixel[2];
dest_pixel += 3;
if ( source_alpha )
*(target_alpha++) = *src_alpha_pixel ;
x += x_delta;
}
y += y_delta;
}
return image;
}
wxImage wxImage::ResampleBox(int width, int height) const
{
// This function implements a simple pre-blur/box averaging method for
@@ -582,6 +594,89 @@ wxImage wxImage::ResampleBox(int width, int height) const
return ret_image;
}
wxImage wxImage::ResampleBilinear(int width, int height) const
{
// This function implements a Bilinear algorithm for resampling.
wxImage ret_image(width, height, false);
unsigned char* src_data = M_IMGDATA->m_data;
unsigned char* src_alpha = M_IMGDATA->m_alpha;
unsigned char* dst_data = ret_image.GetData();
unsigned char* dst_alpha = NULL;
if ( src_alpha )
{
ret_image.SetAlpha();
dst_alpha = ret_image.GetAlpha();
}
double HFactor = double(M_IMGDATA->m_height) / height;
double WFactor = double(M_IMGDATA->m_width) / width;
int srcpixymax = M_IMGDATA->m_height - 1;
int srcpixxmax = M_IMGDATA->m_width - 1;
double srcpixy, srcpixy1, srcpixy2, dy, dy1;
double srcpixx, srcpixx1, srcpixx2, dx, dx1;
double r1, g1, b1, a1;
double r2, g2, b2, a2;
for ( int dsty = 0; dsty < height; dsty++ )
{
// We need to calculate the source pixel to interpolate from - Y-axis
srcpixy = double(dsty) * HFactor;
srcpixy1 = int(srcpixy);
srcpixy2 = ( srcpixy1 == srcpixymax ) ? srcpixy1 : srcpixy1 + 1.0;
dy = srcpixy - (int)srcpixy;
dy1 = 1.0 - dy;
for ( int dstx = 0; dstx < width; dstx++ )
{
// X-axis of pixel to interpolate from
srcpixx = double(dstx) * WFactor;
srcpixx1 = int(srcpixx);
srcpixx2 = ( srcpixx1 == srcpixxmax ) ? srcpixx1 : srcpixx1 + 1.0;
dx = srcpixx - (int)srcpixx;
dx1 = 1.0 - dx;
int x_offset1 = srcpixx1 < 0.0 ? 0 : srcpixx1 > srcpixxmax ? srcpixxmax : (int)srcpixx1;
int x_offset2 = srcpixx2 < 0.0 ? 0 : srcpixx2 > srcpixxmax ? srcpixxmax : (int)srcpixx2;
int y_offset1 = srcpixy1 < 0.0 ? 0 : srcpixy1 > srcpixymax ? srcpixymax : (int)srcpixy1;
int y_offset2 = srcpixy2 < 0.0 ? 0 : srcpixy2 > srcpixymax ? srcpixymax : (int)srcpixy2;
int src_pixel_index00 = y_offset1 * M_IMGDATA->m_width + x_offset1;
int src_pixel_index01 = y_offset1 * M_IMGDATA->m_width + x_offset2;
int src_pixel_index10 = y_offset2 * M_IMGDATA->m_width + x_offset1;
int src_pixel_index11 = y_offset2 * M_IMGDATA->m_width + x_offset2;
//first line
r1 = src_data[src_pixel_index00 * 3 + 0] * dx1 + src_data[src_pixel_index01 * 3 + 0] * dx;
g1 = src_data[src_pixel_index00 * 3 + 1] * dx1 + src_data[src_pixel_index01 * 3 + 1] * dx;
b1 = src_data[src_pixel_index00 * 3 + 2] * dx1 + src_data[src_pixel_index01 * 3 + 2] * dx;
if ( src_alpha )
a1 = src_alpha[src_pixel_index00] * dx1 + src_alpha[src_pixel_index01] * dx;
//second line
r2 = src_data[src_pixel_index10 * 3 + 0] * dx1 + src_data[src_pixel_index11 * 3 + 0] * dx;
g2 = src_data[src_pixel_index10 * 3 + 1] * dx1 + src_data[src_pixel_index11 * 3 + 1] * dx;
b2 = src_data[src_pixel_index10 * 3 + 2] * dx1 + src_data[src_pixel_index11 * 3 + 2] * dx;
if ( src_alpha )
a2 = src_alpha[src_pixel_index10] * dx1 + src_alpha[src_pixel_index11] * dx;
//result lines
dst_data[0] = r1 * dy1 + r2 * dy;
dst_data[1] = g1 * dy1 + g2 * dy;
dst_data[2] = b1 * dy1 + b2 * dy;
dst_data += 3;
if ( src_alpha )
*dst_alpha++ = a1 * dy1 + a2 * dy;
}
}
return ret_image;
}
// The following two local functions are for the B-spline weighting of the
// bicubic sampling algorithm
static inline double spline_cube(double value)