Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. static float FormulaeLinearInterpolation(float f11, float f12, float x1, float x2, float x)
  2. {
  3. float x2x1, x2x, xx1;
  4. x2x1 = x2 - x1;
  5. x2x = x2 - x;
  6. xx1 = x - x1;
  7. return (1.0f / x2x1) * (
  8. f11 * x2x +
  9. f12 * xx1
  10. );
  11. }
  12. static float FormulaeBilinearInterpolation(float f11, float f21, float f12, float f22, float x1, float x2, float y1, float y2, float x, float y)
  13. {
  14. float x2x1, y2y1, x2x, y2y, yy1, xx1;
  15. x2x1 = x2 - x1;
  16. y2y1 = y2 - y1;
  17. x2x = x2 - x;
  18. y2y = y2 - y;
  19. yy1 = y - y1;
  20. xx1 = x - x1;
  21. return (1.0f / (x2x1 * y2y1)) * (
  22. f11 * x2x * y2y +
  23. f21 * xx1 * y2y +
  24. f12 * x2x * yy1 +
  25. f22 * xx1 * yy1
  26. );
  27. }
  28. static ColorFloatImage BilinearInterpolation(ColorFloatImage colorimage, ref ColorFloatImage sxcolorimage, int s)
  29. {
  30. for (int i = 0; i < sxcolorimage.Width - 1; i++)
  31. {
  32. for (int j = 0; j < sxcolorimage.Height - 1; j++)
  33. {
  34. int x1, x2, y1, y2, newx1, newx2, newy1, newy2;
  35. x1 = Convert.ToInt32(Math.Truncate(Convert.ToDouble(((double)i) / s)));
  36. x2 = Convert.ToInt32(Math.Truncate((Convert.ToDouble((double)i / s)))) + 1;
  37. y1 = Convert.ToInt32(Math.Truncate(Convert.ToDouble(((double)j / s))));
  38. y2 = Convert.ToInt32(Math.Truncate(Convert.ToDouble(((double)j / s)))) + 1;
  39. float f11, f12, f21, f22;
  40. if ((y1 == 0) || (y2 == colorimage.Height - 1) || (y2 == colorimage.Height))
  41. {
  42. newx1 = i - s;
  43. newx2 = i + s;
  44. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].r;
  45. f21 = colorimage.rawdata[y1 * colorimage.Width + x2].r;
  46. sxcolorimage.rawdata[j * sxcolorimage.Width + i].r = FormulaeLinearInterpolation(f11, f21, newx1, newx2, i);
  47. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].g;
  48. f21 = colorimage.rawdata[y1 * colorimage.Width + x2].g;
  49. sxcolorimage.rawdata[j * sxcolorimage.Width + i].g = FormulaeLinearInterpolation(f11, f21, newx1, newx2, i);
  50. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].b;
  51. f21 = colorimage.rawdata[y1 * colorimage.Width + x2].b;
  52. sxcolorimage.rawdata[j * sxcolorimage.Width + i].b = FormulaeLinearInterpolation(f11, f21, newx1, newx2, i);
  53. }
  54. else
  55. {
  56. if ((x1 == 0) || (x2 == colorimage.Width - 1) || (x2 == colorimage.Width))
  57. {
  58. newy1 = j - s;
  59. newy2 = j + s;
  60. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].r;
  61. f12 = colorimage.rawdata[y1 * colorimage.Width + x2].r;
  62. sxcolorimage.rawdata[j * sxcolorimage.Width + i].r = FormulaeLinearInterpolation(f11, f12, newy1, newy2, i);
  63. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].g;
  64. f12 = colorimage.rawdata[y1 * colorimage.Width + x2].g;
  65. sxcolorimage.rawdata[j * sxcolorimage.Width + i].g = FormulaeLinearInterpolation(f11, f12, newy1, newy2, i);
  66. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].b;
  67. f12 = colorimage.rawdata[y1 * colorimage.Width + x2].b;
  68. sxcolorimage.rawdata[j * sxcolorimage.Width + i].b = FormulaeLinearInterpolation(f11, f12, newy1, newy2, i);
  69. }
  70. else
  71. {
  72. newx1 = i - s;
  73. newx2 = i + s;
  74. newy1 = j - s;
  75. newy2 = j + s;
  76. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].r;
  77. f12 = colorimage.rawdata[y2 * colorimage.Width + x1].r;
  78. f21 = colorimage.rawdata[y1 * colorimage.Width + x2].r;
  79. f22 = colorimage.rawdata[y2 * colorimage.Width + x2].r;
  80. sxcolorimage.rawdata[j * sxcolorimage.Width + i].r = FormulaeBilinearInterpolation(f11, f21, f12, f22, newx1, newx2, newy1, newy2, i, j);
  81. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].g;
  82. f12 = colorimage.rawdata[y2 * colorimage.Width + x1].g;
  83. f21 = colorimage.rawdata[y1 * colorimage.Width + x2].g;
  84. f22 = colorimage.rawdata[y2 * colorimage.Width + x2].g;
  85. sxcolorimage.rawdata[j * sxcolorimage.Width + i].g = FormulaeBilinearInterpolation(f11, f21, f12, f22, newx1, newx2, newy1, newy2, i, j);
  86. f11 = colorimage.rawdata[y1 * colorimage.Width + x1].b;
  87. f12 = colorimage.rawdata[y2 * colorimage.Width + x1].b;
  88. f21 = colorimage.rawdata[y1 * colorimage.Width + x2].b;
  89. f22 = colorimage.rawdata[y2 * colorimage.Width + x2].b;
  90. sxcolorimage.rawdata[j * sxcolorimage.Width + i].b = FormulaeBilinearInterpolation(f11, f21, f12, f22, newx1, newx2, newy1, newy2, i, j);
  91. }
  92. }
  93. }
  94. }
  95. return sxcolorimage;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement