Fixed rescaling of wxImage

When wxImage is rescaled with wxIMAGE_QUALITY_BILINEAR, wxIMAGE_QUALITY_BICUBIC or wxIMAGE_QUALITY_BOX_AVERAGE algorithm then for proper interpolation there is necessary to uniformly (linearly) map entire pixel range in the new image to the entire pixel range in the current image, i.e. pixels positions (integer values) in range [0..newDim-1] has to be mapped to "virtual" pixel positions (not necessary integer values) exactly in the range [0..oldDim-1].
[0..oldDim-1] range of target mapping is required because for proper interpolation every "virtual" pixel has to be located between two physical pixels in the current image.
Thus scaling ratio used to find corresponding pixels in the current image should be (oldDim-1)/(newDim-1) and not oldDim/newDim (which is used now).
When image is e.g. upsampled oldDim/newDim ratio then some new rightmost/botommost pixels are mapped to old pixels > (oldDim-1) and their values are improperly interpolated (in the current implementation their positions are just truncated to (oldDim-1) to bypass the problem and upsampled image looks like it was shifted left/up). The larger scaling ratio the effect is more visible.

Note:
Because reference images used currently in the unit tests were created with improper scaling there is necessary to upload new reference images created with fixed scaling.

Closes #17594
This commit is contained in:
Artur Wieczorek 2016-07-22 22:37:13 +02:00
parent c3fa684a27
commit 097625de52
14 changed files with 96 additions and 45 deletions

View File

@ -93,6 +93,7 @@ All (GUI):
- Fix wxTextEntry::SetHint() with wxTE_PASSWORD in generic implementation.
- Many fixes and improvements in Direct2D, Cairo, and GDI+ graphics renderers.
- Fix and unify clipping region support for MSW and GTK+.
- Fix rescaling of wxImage
wxGTK:

View File

@ -576,7 +576,12 @@ inline int BoxBetween(int value, int low, int high)
void ResampleBoxPrecalc(wxVector<BoxPrecalc>& boxes, int oldDim)
{
const int newDim = boxes.size();
const double scale_factor_1 = double(oldDim) / newDim;
wxASSERT( oldDim > 0 && newDim > 0 );
if ( newDim > 1 )
{
// We want to map pixels in the range [0..newDim-1]
// to the range [0..oldDim-1]
const double scale_factor_1 = double(oldDim-1) / (newDim-1);
const int scale_factor_2 = (int)(scale_factor_1 / 2);
for ( int dst = 0; dst < newDim; ++dst )
@ -592,6 +597,14 @@ void ResampleBoxPrecalc(wxVector<BoxPrecalc>& boxes, int oldDim)
0, oldDim - 1);
}
}
else
{
// Let's take entire source image.
BoxPrecalc& precalc = boxes[0];
precalc.boxStart = 0;
precalc.boxEnd = oldDim - 1;
}
}
} // anonymous namespace
@ -680,20 +693,10 @@ struct BilinearPrecalc
double dd1;
};
void ResampleBilinearPrecalc(wxVector<BilinearPrecalc>& precalcs, int oldDim)
inline void DoCalc(BilinearPrecalc& precalc, double srcpix, int srcpixmax)
{
const int newDim = precalcs.size();
const double scale_factor = double(oldDim) / newDim;
const int srcpixmax = oldDim - 1;
for ( int dsty = 0; dsty < newDim; dsty++ )
{
// We need to calculate the source pixel to interpolate from - Y-axis
double srcpix = double(dsty) * scale_factor;
double srcpix1 = int(srcpix);
double srcpix2 = srcpix1 == srcpixmax ? srcpix1 : srcpix1 + 1.0;
BilinearPrecalc& precalc = precalcs[dsty];
int srcpix1 = int(srcpix);
int srcpix2 = srcpix1 == srcpixmax ? srcpix1 : srcpix1 + 1;
precalc.dd = srcpix - (int)srcpix;
precalc.dd1 = 1.0 - precalc.dd;
@ -708,6 +711,33 @@ void ResampleBilinearPrecalc(wxVector<BilinearPrecalc>& precalcs, int oldDim)
? srcpixmax
: (int)srcpix2;
}
void ResampleBilinearPrecalc(wxVector<BilinearPrecalc>& precalcs, int oldDim)
{
const int newDim = precalcs.size();
wxASSERT( oldDim > 0 && newDim > 0 );
const int srcpixmax = oldDim - 1;
if ( newDim > 1 )
{
// We want to map pixels in the range [0..newDim-1]
// to the range [0..oldDim-1]
const double scale_factor = double(oldDim-1) / (newDim-1);
for ( int dsty = 0; dsty < newDim; dsty++ )
{
// We need to calculate the source pixel to interpolate from - Y-axis
double srcpix = (double)dsty * scale_factor;
DoCalc(precalcs[dsty], srcpix, srcpixmax);
}
}
else
{
// Let's take the pixel from the center of the source image.
double srcpix = (double)srcpixmax / 2.0;
DoCalc(precalcs[0], srcpix, srcpixmax);
}
}
} // anonymous namespace
@ -816,17 +846,10 @@ struct BicubicPrecalc
int offset[4];
};
void ResampleBicubicPrecalc(wxVector<BicubicPrecalc> &aWeight, int oldDim)
inline void DoCalc(BicubicPrecalc& precalc, double srcpixd, int oldDim)
{
const int newDim = aWeight.size();
for ( int dstd = 0; dstd < newDim; dstd++ )
{
// We need to calculate the source pixel to interpolate from - Y-axis
const double srcpixd = static_cast<double>(dstd * oldDim) / newDim;
const double dd = srcpixd - static_cast<int>(srcpixd);
BicubicPrecalc &precalc = aWeight[dstd];
for ( int k = -1; k <= 2; k++ )
{
precalc.offset[k + 1] = srcpixd + k < 0.0
@ -838,6 +861,33 @@ void ResampleBicubicPrecalc(wxVector<BicubicPrecalc> &aWeight, int oldDim)
precalc.weight[k + 1] = spline_weight(k - dd);
}
}
void ResampleBicubicPrecalc(wxVector<BicubicPrecalc> &aWeight, int oldDim)
{
const int newDim = aWeight.size();
wxASSERT( oldDim > 0 && newDim > 0 );
if ( newDim > 1 )
{
// We want to map pixels in the range [0..newDim-1]
// to the range [0..oldDim-1]
const double scale_factor = static_cast<double>(oldDim-1) / (newDim-1);
for ( int dstd = 0; dstd < newDim; dstd++ )
{
// We need to calculate the source pixel to interpolate from - Y-axis
const double srcpixd = static_cast<double>(dstd) * scale_factor;
DoCalc(aWeight[dstd], srcpixd, oldDim);
}
}
else
{
// Let's take the pixel from the center of the source image.
const double srcpixd = static_cast<double>(oldDim - 1) / 2.0;
DoCalc(aWeight[0], srcpixd, oldDim);
}
}
} // anonymous namespace

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 102 KiB

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 69 KiB

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB