+ // 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];
+
+ 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;
+ }
+}
+
+} // anonymous namespace
+
+wxImage wxImage::ResampleBilinear(int width, int height) const
+{
+ // This function implements a Bilinear algorithm for resampling.
+ wxImage ret_image(width, height, false);
+ const unsigned char* src_data = M_IMGDATA->m_data;
+ const 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();
+ }
+
+ wxVector<BilinearPrecalc> vPrecalcs(height);
+ wxVector<BilinearPrecalc> hPrecalcs(width);
+ ResampleBilinearPrecalc(vPrecalcs, M_IMGDATA->m_height);
+ ResampleBilinearPrecalc(hPrecalcs, M_IMGDATA->m_width);
+
+ // initialize alpha values to avoid g++ warnings about possibly
+ // uninitialized variables
+ double r1, g1, b1, a1 = 0;
+ double r2, g2, b2, a2 = 0;
+
+ for ( int dsty = 0; dsty < height; dsty++ )
+ {
+ // We need to calculate the source pixel to interpolate from - Y-axis
+ const BilinearPrecalc& vPrecalc = vPrecalcs[dsty];
+ const int y_offset1 = vPrecalc.offset1;
+ const int y_offset2 = vPrecalc.offset2;
+ const double dy = vPrecalc.dd;
+ const double dy1 = vPrecalc.dd1;
+
+
+ for ( int dstx = 0; dstx < width; dstx++ )
+ {
+ // X-axis of pixel to interpolate from
+ const BilinearPrecalc& hPrecalc = hPrecalcs[dstx];
+
+ const int x_offset1 = hPrecalc.offset1;
+ const int x_offset2 = hPrecalc.offset2;
+ const double dx = hPrecalc.dd;
+ const double dx1 = hPrecalc.dd1;
+
+ 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] = static_cast<unsigned char>(r1 * dy1 + r2 * dy);
+ dst_data[1] = static_cast<unsigned char>(g1 * dy1 + g2 * dy);
+ dst_data[2] = static_cast<unsigned char>(b1 * dy1 + b2 * dy);
+ dst_data += 3;
+
+ if ( src_alpha )
+ *dst_alpha++ = static_cast<unsigned char>(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)
+{
+ return value <= 0.0 ? 0.0 : value * value * value;
+}
+
+static inline double spline_weight(double value)
+{
+ return (spline_cube(value + 2) -
+ 4 * spline_cube(value + 1) +
+ 6 * spline_cube(value) -
+ 4 * spline_cube(value - 1)) / 6;
+}
+
+
+namespace
+{
+
+struct BicubicPrecalc
+{
+ double weight[4];
+ int offset[4];
+};
+
+void ResampleBicubicPrecalc(wxVector<BicubicPrecalc> &aWeight, 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
+ ? 0
+ : srcpixd + k >= oldDim
+ ? oldDim - 1
+ : static_cast<int>(srcpixd + k);
+
+ precalc.weight[k + 1] = spline_weight(k - dd);
+ }
+ }
+}
+
+} // anonymous namespace
+
+// This is the bicubic resampling algorithm
+wxImage wxImage::ResampleBicubic(int width, int height) const
+{
+ // This function implements a Bicubic B-Spline algorithm for resampling.
+ // This method is certainly a little slower than wxImage's default pixel
+ // replication method, however for most reasonably sized images not being
+ // upsampled too much on a fairly average CPU this difference is hardly
+ // noticeable and the results are far more pleasing to look at.
+ //
+ // This particular bicubic algorithm does pixel weighting according to a
+ // B-Spline that basically implements a Gaussian bell-like weighting
+ // kernel. Because of this method the results may appear a bit blurry when
+ // upsampling by large factors. This is basically because a slight
+ // gaussian blur is being performed to get the smooth look of the upsampled
+ // image.
+
+ // Edge pixels: 3-4 possible solutions
+ // - (Wrap/tile) Wrap the image, take the color value from the opposite
+ // side of the image.
+ // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
+ // where n is nonpositive, will have the value of (2, 1).
+ // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
+ // pixels which do have all neighbours.
+ // - (Clamp) Choose the nearest pixel along the border. This takes the
+ // border pixels and extends them out to infinity.
+ //
+ // NOTE: below the y_offset and x_offset variables are being set for edge
+ // pixels using the "Mirror" method mentioned above
+
+ wxImage ret_image;
+
+ ret_image.Create(width, height, false);
+
+ const unsigned char* src_data = M_IMGDATA->m_data;
+ const 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();
+ }
+
+ // Precalculate weights
+ wxVector<BicubicPrecalc> vPrecalcs(height);
+ wxVector<BicubicPrecalc> hPrecalcs(width);
+
+ ResampleBicubicPrecalc(vPrecalcs, M_IMGDATA->m_height);
+ ResampleBicubicPrecalc(hPrecalcs, M_IMGDATA->m_width);
+
+ for ( int dsty = 0; dsty < height; dsty++ )
+ {
+ // We need to calculate the source pixel to interpolate from - Y-axis
+ const BicubicPrecalc& vPrecalc = vPrecalcs[dsty];
+
+ for ( int dstx = 0; dstx < width; dstx++ )