selection-api-006.rst 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. .. -*- coding: utf-8; mode: rst -*-
  2. ********
  3. Examples
  4. ********
  5. (A video capture device is assumed; change
  6. ``V4L2_BUF_TYPE_VIDEO_CAPTURE`` for other devices; change target to
  7. ``V4L2_SEL_TGT_COMPOSE_*`` family to configure composing area)
  8. Example: Resetting the cropping parameters
  9. ==========================================
  10. .. code-block:: c
  11. struct v4l2_selection sel = {
  12. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  13. .target = V4L2_SEL_TGT_CROP_DEFAULT,
  14. };
  15. ret = ioctl(fd, VIDIOC_G_SELECTION, &sel);
  16. if (ret)
  17. exit(-1);
  18. sel.target = V4L2_SEL_TGT_CROP;
  19. ret = ioctl(fd, VIDIOC_S_SELECTION, &sel);
  20. if (ret)
  21. exit(-1);
  22. Setting a composing area on output of size of *at most* half of limit
  23. placed at a center of a display.
  24. Example: Simple downscaling
  25. ===========================
  26. .. code-block:: c
  27. struct v4l2_selection sel = {
  28. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  29. .target = V4L2_SEL_TGT_COMPOSE_BOUNDS,
  30. };
  31. struct v4l2_rect r;
  32. ret = ioctl(fd, VIDIOC_G_SELECTION, &sel);
  33. if (ret)
  34. exit(-1);
  35. /* setting smaller compose rectangle */
  36. r.width = sel.r.width / 2;
  37. r.height = sel.r.height / 2;
  38. r.left = sel.r.width / 4;
  39. r.top = sel.r.height / 4;
  40. sel.r = r;
  41. sel.target = V4L2_SEL_TGT_COMPOSE;
  42. sel.flags = V4L2_SEL_FLAG_LE;
  43. ret = ioctl(fd, VIDIOC_S_SELECTION, &sel);
  44. if (ret)
  45. exit(-1);
  46. A video output device is assumed; change ``V4L2_BUF_TYPE_VIDEO_OUTPUT``
  47. for other devices
  48. Example: Querying for scaling factors
  49. =====================================
  50. .. code-block:: c
  51. struct v4l2_selection compose = {
  52. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  53. .target = V4L2_SEL_TGT_COMPOSE,
  54. };
  55. struct v4l2_selection crop = {
  56. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  57. .target = V4L2_SEL_TGT_CROP,
  58. };
  59. double hscale, vscale;
  60. ret = ioctl(fd, VIDIOC_G_SELECTION, &compose);
  61. if (ret)
  62. exit(-1);
  63. ret = ioctl(fd, VIDIOC_G_SELECTION, &crop);
  64. if (ret)
  65. exit(-1);
  66. /* computing scaling factors */
  67. hscale = (double)compose.r.width / crop.r.width;
  68. vscale = (double)compose.r.height / crop.r.height;