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
@ -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:
|
||||
|
||||
|
@ -576,20 +576,33 @@ 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;
|
||||
const int scale_factor_2 = (int)(scale_factor_1 / 2);
|
||||
|
||||
for ( int dst = 0; dst < newDim; ++dst )
|
||||
wxASSERT( oldDim > 0 && newDim > 0 );
|
||||
if ( newDim > 1 )
|
||||
{
|
||||
// Source pixel in the Y direction
|
||||
const int src_p = int(dst * scale_factor_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);
|
||||
|
||||
BoxPrecalc& precalc = boxes[dst];
|
||||
precalc.boxStart = BoxBetween(int(src_p - scale_factor_1/2.0 + 1),
|
||||
0, oldDim - 1);
|
||||
precalc.boxEnd = BoxBetween(wxMax(precalc.boxStart + 1,
|
||||
int(src_p + scale_factor_2)),
|
||||
0, oldDim - 1);
|
||||
for ( int dst = 0; dst < newDim; ++dst )
|
||||
{
|
||||
// Source pixel in the Y direction
|
||||
const int src_p = int(dst * scale_factor_1);
|
||||
|
||||
BoxPrecalc& precalc = boxes[dst];
|
||||
precalc.boxStart = BoxBetween(int(src_p - scale_factor_1/2.0 + 1),
|
||||
0, oldDim - 1);
|
||||
precalc.boxEnd = BoxBetween(wxMax(precalc.boxStart + 1,
|
||||
int(src_p + scale_factor_2)),
|
||||
0, oldDim - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Let's take entire source image.
|
||||
BoxPrecalc& precalc = boxes[0];
|
||||
precalc.boxStart = 0;
|
||||
precalc.boxEnd = oldDim - 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -680,33 +693,50 @@ struct BilinearPrecalc
|
||||
double dd1;
|
||||
};
|
||||
|
||||
inline void DoCalc(BilinearPrecalc& precalc, double srcpix, int srcpixmax)
|
||||
{
|
||||
int srcpix1 = int(srcpix);
|
||||
int srcpix2 = srcpix1 == srcpixmax ? srcpix1 : srcpix1 + 1;
|
||||
|
||||
precalc.dd = srcpix - (int)srcpix;
|
||||
precalc.dd1 = 1.0 - precalc.dd;
|
||||
precalc.offset1 = srcpix1 < 0.0
|
||||
? 0
|
||||
: srcpix1 > srcpixmax
|
||||
? srcpixmax
|
||||
: (int)srcpix1;
|
||||
precalc.offset2 = srcpix2 < 0.0
|
||||
? 0
|
||||
: srcpix2 > srcpixmax
|
||||
? srcpixmax
|
||||
: (int)srcpix2;
|
||||
}
|
||||
|
||||
void ResampleBilinearPrecalc(wxVector<BilinearPrecalc>& precalcs, int oldDim)
|
||||
{
|
||||
const int newDim = precalcs.size();
|
||||
const double scale_factor = double(oldDim) / newDim;
|
||||
wxASSERT( oldDim > 0 && newDim > 0 );
|
||||
const int srcpixmax = oldDim - 1;
|
||||
|
||||
for ( int dsty = 0; dsty < newDim; dsty++ )
|
||||
if ( newDim > 1 )
|
||||
{
|
||||
// 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;
|
||||
// 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);
|
||||
|
||||
BilinearPrecalc& precalc = precalcs[dsty];
|
||||
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;
|
||||
|
||||
precalc.dd = srcpix - (int)srcpix;
|
||||
precalc.dd1 = 1.0 - precalc.dd;
|
||||
precalc.offset1 = srcpix1 < 0.0
|
||||
? 0
|
||||
: srcpix1 > srcpixmax
|
||||
? srcpixmax
|
||||
: (int)srcpix1;
|
||||
precalc.offset2 = srcpix2 < 0.0
|
||||
? 0
|
||||
: srcpix2 > srcpixmax
|
||||
? srcpixmax
|
||||
: (int)srcpix2;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -816,28 +846,48 @@ struct BicubicPrecalc
|
||||
int offset[4];
|
||||
};
|
||||
|
||||
inline void DoCalc(BicubicPrecalc& precalc, double srcpixd, int oldDim)
|
||||
{
|
||||
const double dd = srcpixd - static_cast<int>(srcpixd);
|
||||
|
||||
for ( int k = -1; k <= 2; k++ )
|
||||
{
|
||||
precalc.offset[k + 1] = srcpixd + k < 0.0
|
||||
? 0
|
||||
: srcpixd + k >= oldDim
|
||||
? oldDim - 1
|
||||
: static_cast<int>(srcpixd + k);
|
||||
|
||||
precalc.weight[k + 1] = spline_weight(k - dd);
|
||||
}
|
||||
}
|
||||
|
||||
void ResampleBicubicPrecalc(wxVector<BicubicPrecalc> &aWeight, int oldDim)
|
||||
{
|
||||
const int newDim = aWeight.size();
|
||||
for ( int dstd = 0; dstd < newDim; dstd++ )
|
||||
wxASSERT( oldDim > 0 && newDim > 0 );
|
||||
|
||||
if ( newDim > 1 )
|
||||
{
|
||||
// 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);
|
||||
// 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);
|
||||
|
||||
BicubicPrecalc &precalc = aWeight[dstd];
|
||||
|
||||
for ( int k = -1; k <= 2; k++ )
|
||||
for ( int dstd = 0; dstd < newDim; dstd++ )
|
||||
{
|
||||
precalc.offset[k + 1] = srcpixd + k < 0.0
|
||||
? 0
|
||||
: srcpixd + k >= oldDim
|
||||
? oldDim - 1
|
||||
: static_cast<int>(srcpixd + k);
|
||||
// We need to calculate the source pixel to interpolate from - Y-axis
|
||||
const double srcpixd = static_cast<double>(dstd) * scale_factor;
|
||||
|
||||
precalc.weight[k + 1] = spline_weight(k - dd);
|
||||
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
|
||||
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 102 KiB After Width: | Height: | Size: 99 KiB |
Before Width: | Height: | Size: 5.6 KiB After Width: | Height: | Size: 5.5 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 110 KiB After Width: | Height: | Size: 110 KiB |
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.6 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 65 KiB |
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 4.9 KiB |