+namespace
+{
+
+struct BilinearPrecalc
+{
+ int offset1;
+ int offset2;
+ double dd;
+ double dd1;
+};
+
+void ResampleBilinearPrecalc(wxVector<BilinearPrecalc>& precalcs, int oldDim)
+{
+ 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];
+
+ 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;
+}
+