////////////////////////////////////////////////////////////////////////// \brief host texture fetch read from arbitrary position within image using bilinear interpolation out of range coords are mirrored \param[in] t texture raw data \param[in] w texture width \param[in] h texture height \param[in] s texture stride \param[in] x x coord of the point to fetch value at \param[in] y
| 43 | /// \return fetched value |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | inline float Tex2D(const float *t, int w, int h, int s, float x, float y) |
| 46 | { |
| 47 | // integer parts in floating point format |
| 48 | float intPartX, intPartY; |
| 49 | |
| 50 | // get fractional parts of coordinates |
| 51 | float dx = fabsf(modff(x, &intPartX)); |
| 52 | float dy = fabsf(modff(y, &intPartY)); |
| 53 | |
| 54 | // assume pixels are squares |
| 55 | // one of the corners |
| 56 | int ix0 = (int)intPartX; |
| 57 | int iy0 = (int)intPartY; |
| 58 | |
| 59 | // mirror out-of-range position |
| 60 | if (ix0 < 0) |
| 61 | ix0 = abs(ix0 + 1); |
| 62 | |
| 63 | if (iy0 < 0) |
| 64 | iy0 = abs(iy0 + 1); |
| 65 | |
| 66 | if (ix0 >= w) |
| 67 | ix0 = w * 2 - ix0 - 1; |
| 68 | |
| 69 | if (iy0 >= h) |
| 70 | iy0 = h * 2 - iy0 - 1; |
| 71 | |
| 72 | // corner which is opposite to (ix0, iy0) |
| 73 | int ix1 = ix0 + 1; |
| 74 | int iy1 = iy0 + 1; |
| 75 | |
| 76 | if (ix1 >= w) |
| 77 | ix1 = w * 2 - ix1 - 1; |
| 78 | |
| 79 | if (iy1 >= h) |
| 80 | iy1 = h * 2 - iy1 - 1; |
| 81 | |
| 82 | float res = t[ix0 + iy0 * s] * (1.0f - dx) * (1.0f - dy); |
| 83 | res += t[ix1 + iy0 * s] * dx * (1.0f - dy); |
| 84 | res += t[ix0 + iy1 * s] * (1.0f - dx) * dy; |
| 85 | res += t[ix1 + iy1 * s] * dx * dy; |
| 86 | |
| 87 | return res; |
| 88 | } |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////////// |
| 91 | /// \brief host texture fetch |