ThresholdMask.qml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (C) 2020 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import Qt5Compat.GraphicalEffects.private
  6. /*!
  7. \qmltype ThresholdMask
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since QtGraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-mask
  12. \brief Masks the source item with another item and applies a threshold
  13. value.
  14. The masking behavior can be controlled with the \l threshold value for the
  15. mask pixels.
  16. \table
  17. \header
  18. \li Source
  19. \li MaskSource
  20. \li Effect applied
  21. \row
  22. \li \image Original_bug.png
  23. \li \image ThresholdMask_mask.png
  24. \li \image ThresholdMask_bug.png
  25. \endtable
  26. \section1 Example
  27. The following example shows how to apply the effect.
  28. \snippet ThresholdMask-example.qml example
  29. */
  30. Item {
  31. id: rootItem
  32. /*!
  33. This property defines the source item that is going to be masked.
  34. \note It is not supported to let the effect include itself, for
  35. instance by setting source to the effect's parent.
  36. */
  37. property variant source
  38. /*!
  39. This property defines the item that is going to be used as the mask.
  40. Mask item gets rendered into an intermediate pixel buffer and the alpha
  41. values from the result are used to determine the source item's pixels
  42. visibility in the display.
  43. \table
  44. \header
  45. \li Original
  46. \li Mask
  47. \li Effect applied
  48. \row
  49. \li \image Original_bug.png
  50. \li \image ThresholdMask_mask.png
  51. \li \image ThresholdMask_bug.png
  52. \endtable
  53. \note It is not supported to let the effect include itself, for
  54. instance by setting maskSource to the effect's parent.
  55. */
  56. property variant maskSource
  57. /*!
  58. This property defines a threshold value for the mask pixels. The mask
  59. pixels that have an alpha value below this property are used to
  60. completely mask away the corresponding pixels from the source item. The
  61. mask pixels that have a higher alpha value are used to alphablend the
  62. source item to the display.
  63. The value ranges from 0.0 (alpha value 0) to 1.0 (alpha value 255). By
  64. default, the property is set to \c 0.0.
  65. \table
  66. \header
  67. \li Output examples with different threshold values
  68. \li
  69. \li
  70. \row
  71. \li \image ThresholdMask_threshold1.png
  72. \li \image ThresholdMask_threshold2.png
  73. \li \image ThresholdMask_threshold3.png
  74. \row
  75. \li \b { threshold: 0.0 }
  76. \li \b { threshold: 0.5 }
  77. \li \b { threshold: 0.7 }
  78. \row
  79. \li \l spread: 0.2
  80. \li \l spread: 0.2
  81. \li \l spread: 0.2
  82. \endtable
  83. */
  84. property real threshold: 0.0
  85. /*!
  86. This property defines the smoothness of the mask edges near the
  87. \l{ThresholdMask::threshold}{threshold} alpha value. Setting spread to
  88. 0.0 uses mask normally with the specified threshold. Setting higher
  89. spread values softens the transition from the transparent mask pixels
  90. towards opaque mask pixels by adding interpolated values between them.
  91. The value ranges from 0.0 (sharp mask edge) to 1.0 (smooth mask edge).
  92. By default, the property is set to \c 0.0.
  93. \table
  94. \header
  95. \li Output examples with different spread values
  96. \li
  97. \li
  98. \row
  99. \li \image ThresholdMask_spread1.png
  100. \li \image ThresholdMask_spread2.png
  101. \li \image ThresholdMask_spread3.png
  102. \row
  103. \li \b { spread: 0.0 }
  104. \li \b { spread: 0.2 }
  105. \li \b { spread: 0.8 }
  106. \row
  107. \li \l threshold: 0.4
  108. \li \l threshold: 0.4
  109. \li \l threshold: 0.4
  110. \endtable
  111. */
  112. property real spread: 0.0
  113. /*!
  114. This property allows the effect output pixels to be cached in order to
  115. improve the rendering performance.
  116. Every time the source or effect properties are changed, the pixels in
  117. the cache must be updated. Memory consumption is increased, because an
  118. extra buffer of memory is required for storing the effect output.
  119. It is recommended to disable the cache when the source or the effect
  120. properties are animated.
  121. By default, the property is set to \c false.
  122. */
  123. property bool cached: false
  124. SourceProxy {
  125. id: sourceProxy
  126. input: rootItem.source
  127. }
  128. SourceProxy {
  129. id: maskSourceProxy
  130. input: rootItem.maskSource
  131. }
  132. ShaderEffectSource {
  133. id: cacheItem
  134. anchors.fill: parent
  135. visible: rootItem.cached
  136. smooth: true
  137. sourceItem: shaderItem
  138. live: true
  139. hideSource: visible
  140. }
  141. ShaderEffect {
  142. id: shaderItem
  143. property variant source: sourceProxy.output
  144. property variant maskSource: maskSourceProxy.output
  145. property real threshold: rootItem.threshold
  146. property real spread: rootItem.spread
  147. anchors.fill: parent
  148. fragmentShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/thresholdmask.frag.qsb"
  149. }
  150. }