transform.cl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #define INTER_BITS 5
  2. #define INTER_TAB_SIZE (1 << INTER_BITS)
  3. #define INTER_SCALE 1.f / INTER_TAB_SIZE
  4. #define INTER_REMAP_COEF_BITS 15
  5. #define INTER_REMAP_COEF_SCALE (1 << INTER_REMAP_COEF_BITS)
  6. __kernel void warpPerspective(__global const uchar * src,
  7. int src_row_stride, int src_px_stride, int src_offset, int src_rows, int src_cols,
  8. __global uchar * dst,
  9. int dst_row_stride, int dst_offset, int dst_rows, int dst_cols,
  10. __constant float * M)
  11. {
  12. int dx = get_global_id(0);
  13. int dy = get_global_id(1);
  14. if (dx < dst_cols && dy < dst_rows)
  15. {
  16. float X0 = M[0] * dx + M[1] * dy + M[2];
  17. float Y0 = M[3] * dx + M[4] * dy + M[5];
  18. float W = M[6] * dx + M[7] * dy + M[8];
  19. W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f;
  20. int X = rint(X0 * W), Y = rint(Y0 * W);
  21. int sx = convert_short_sat(X >> INTER_BITS);
  22. int sy = convert_short_sat(Y >> INTER_BITS);
  23. short sx_clamp = clamp(sx, 0, src_cols - 1);
  24. short sx_p1_clamp = clamp(sx + 1, 0, src_cols - 1);
  25. short sy_clamp = clamp(sy, 0, src_rows - 1);
  26. short sy_p1_clamp = clamp(sy + 1, 0, src_rows - 1);
  27. int v0 = convert_int(src[mad24(sy_clamp, src_row_stride, src_offset + sx_clamp*src_px_stride)]);
  28. int v1 = convert_int(src[mad24(sy_clamp, src_row_stride, src_offset + sx_p1_clamp*src_px_stride)]);
  29. int v2 = convert_int(src[mad24(sy_p1_clamp, src_row_stride, src_offset + sx_clamp*src_px_stride)]);
  30. int v3 = convert_int(src[mad24(sy_p1_clamp, src_row_stride, src_offset + sx_p1_clamp*src_px_stride)]);
  31. short ay = (short)(Y & (INTER_TAB_SIZE - 1));
  32. short ax = (short)(X & (INTER_TAB_SIZE - 1));
  33. float taby = 1.f/INTER_TAB_SIZE*ay;
  34. float tabx = 1.f/INTER_TAB_SIZE*ax;
  35. int dst_index = mad24(dy, dst_row_stride, dst_offset + dx);
  36. int itab0 = convert_short_sat_rte( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE );
  37. int itab1 = convert_short_sat_rte( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE );
  38. int itab2 = convert_short_sat_rte( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE );
  39. int itab3 = convert_short_sat_rte( taby*tabx * INTER_REMAP_COEF_SCALE );
  40. int val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3;
  41. uchar pix = convert_uchar_sat((val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS);
  42. dst[dst_index] = pix;
  43. }
  44. }