Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static float FormulaeLinearInterpolation(float f11, float f12, float x1, float x2, float x)
- {
- float x2x1, x2x, xx1;
- x2x1 = x2 - x1;
- x2x = x2 - x;
- xx1 = x - x1;
- return (1.0f / x2x1) * (
- f11 * x2x +
- f12 * xx1
- );
- }
- static float FormulaeBilinearInterpolation(float f11, float f21, float f12, float f22, float x1, float x2, float y1, float y2, float x, float y)
- {
- float x2x1, y2y1, x2x, y2y, yy1, xx1;
- x2x1 = x2 - x1;
- y2y1 = y2 - y1;
- x2x = x2 - x;
- y2y = y2 - y;
- yy1 = y - y1;
- xx1 = x - x1;
- return (1.0f / (x2x1 * y2y1)) * (
- f11 * x2x * y2y +
- f21 * xx1 * y2y +
- f12 * x2x * yy1 +
- f22 * xx1 * yy1
- );
- }
- static ColorFloatImage BilinearInterpolation(ColorFloatImage colorimage, ref ColorFloatImage sxcolorimage, int s)
- {
- for (int i = 0; i < sxcolorimage.Width - 1; i++)
- {
- for (int j = 0; j < sxcolorimage.Height - 1; j++)
- {
- int x1, x2, y1, y2, newx1, newx2, newy1, newy2;
- x1 = Convert.ToInt32(Math.Truncate(Convert.ToDouble(((double)i) / s)));
- x2 = Convert.ToInt32(Math.Truncate((Convert.ToDouble((double)i / s)))) + 1;
- y1 = Convert.ToInt32(Math.Truncate(Convert.ToDouble(((double)j / s))));
- y2 = Convert.ToInt32(Math.Truncate(Convert.ToDouble(((double)j / s)))) + 1;
- float f11, f12, f21, f22;
- if ((y1 == 0) || (y2 == colorimage.Height - 1) || (y2 == colorimage.Height))
- {
- newx1 = i - s;
- newx2 = i + s;
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].r;
- f21 = colorimage.rawdata[y1 * colorimage.Width + x2].r;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].r = FormulaeLinearInterpolation(f11, f21, newx1, newx2, i);
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].g;
- f21 = colorimage.rawdata[y1 * colorimage.Width + x2].g;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].g = FormulaeLinearInterpolation(f11, f21, newx1, newx2, i);
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].b;
- f21 = colorimage.rawdata[y1 * colorimage.Width + x2].b;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].b = FormulaeLinearInterpolation(f11, f21, newx1, newx2, i);
- }
- else
- {
- if ((x1 == 0) || (x2 == colorimage.Width - 1) || (x2 == colorimage.Width))
- {
- newy1 = j - s;
- newy2 = j + s;
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].r;
- f12 = colorimage.rawdata[y1 * colorimage.Width + x2].r;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].r = FormulaeLinearInterpolation(f11, f12, newy1, newy2, i);
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].g;
- f12 = colorimage.rawdata[y1 * colorimage.Width + x2].g;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].g = FormulaeLinearInterpolation(f11, f12, newy1, newy2, i);
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].b;
- f12 = colorimage.rawdata[y1 * colorimage.Width + x2].b;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].b = FormulaeLinearInterpolation(f11, f12, newy1, newy2, i);
- }
- else
- {
- newx1 = i - s;
- newx2 = i + s;
- newy1 = j - s;
- newy2 = j + s;
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].r;
- f12 = colorimage.rawdata[y2 * colorimage.Width + x1].r;
- f21 = colorimage.rawdata[y1 * colorimage.Width + x2].r;
- f22 = colorimage.rawdata[y2 * colorimage.Width + x2].r;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].r = FormulaeBilinearInterpolation(f11, f21, f12, f22, newx1, newx2, newy1, newy2, i, j);
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].g;
- f12 = colorimage.rawdata[y2 * colorimage.Width + x1].g;
- f21 = colorimage.rawdata[y1 * colorimage.Width + x2].g;
- f22 = colorimage.rawdata[y2 * colorimage.Width + x2].g;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].g = FormulaeBilinearInterpolation(f11, f21, f12, f22, newx1, newx2, newy1, newy2, i, j);
- f11 = colorimage.rawdata[y1 * colorimage.Width + x1].b;
- f12 = colorimage.rawdata[y2 * colorimage.Width + x1].b;
- f21 = colorimage.rawdata[y1 * colorimage.Width + x2].b;
- f22 = colorimage.rawdata[y2 * colorimage.Width + x2].b;
- sxcolorimage.rawdata[j * sxcolorimage.Width + i].b = FormulaeBilinearInterpolation(f11, f21, f12, f22, newx1, newx2, newy1, newy2, i, j);
- }
- }
- }
- }
- return sxcolorimage;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement