GammaAdjust.qml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 GammaAdjust
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since QtGraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-color
  12. \brief Alters the luminance of the source item.
  13. GammaAdjust is applied to each pixel according to the curve which is
  14. pre-defined as a power-law expression, where the property gamma is used as the
  15. reciprocal scaling exponent. Refer to the property documentation of \l{GammaAdjust::gamma}{gamma}
  16. for more details.
  17. \table
  18. \header
  19. \li Source
  20. \li Effect applied
  21. \row
  22. \li \image Original_bug.png
  23. \li \image GammaAdjust_bug.png
  24. \endtable
  25. \section1 Example
  26. The following example shows how to apply the effect.
  27. \snippet GammaAdjust-example.qml example
  28. */
  29. Item {
  30. id: rootItem
  31. /*!
  32. This property defines the source item for which the luminance is going to be
  33. adjusted.
  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 change factor for how the luminance of each pixel
  40. is altered according to the equation:
  41. \code
  42. luminance = pow(original_luminance, 1.0 / gamma); // The luminance is assumed to be between 0.0 and 1.0
  43. \endcode
  44. Setting the gamma values under 1.0 makes the image darker, the values
  45. above 1.0 lighten it.
  46. The value ranges from 0.0 (darkest) to inf (lightest). By default, the
  47. property is set to \c 1.0 (no change).
  48. \table
  49. \header
  50. \li Output examples with different gamma values
  51. \li
  52. \li
  53. \row
  54. \li \image GammaAdjust_gamma1.png
  55. \li \image GammaAdjust_gamma2.png
  56. \li \image GammaAdjust_gamma3.png
  57. \row
  58. \li \b { gamma: 0.5 }
  59. \li \b { gamma: 1.0 }
  60. \li \b { gamma: 2.0 }
  61. \endtable
  62. \table
  63. \header
  64. \li Pixel luminance curves of the above images.
  65. \li
  66. \li
  67. \row
  68. \li \image GammaAdjust_gamma1_graph.png
  69. \li \image GammaAdjust_gamma2_graph.png
  70. \li \image GammaAdjust_gamma3_graph.png
  71. \row
  72. \li Red curve: default gamma (1.0)
  73. \li
  74. \li
  75. \row
  76. \li Yellow curve: effect applied
  77. \li
  78. \li
  79. \row
  80. \li X-axis: pixel original luminance
  81. \li
  82. \li
  83. \row
  84. \li Y-axis: pixel luminance with effect applied
  85. \li
  86. \li
  87. \endtable
  88. */
  89. property real gamma: 1.0
  90. /*!
  91. This property allows the effect output pixels to be cached in order to
  92. improve the rendering performance.
  93. Every time the source or effect properties are changed, the pixels in
  94. the cache must be updated. Memory consumption is increased, because an
  95. extra buffer of memory is required for storing the effect output.
  96. It is recommended to disable the cache when the source or the effect
  97. properties are animated.
  98. By default, the property is set to \c false.
  99. */
  100. property bool cached: false
  101. SourceProxy {
  102. id: sourceProxy
  103. input: rootItem.source
  104. interpolation: input && input.smooth ? SourceProxy.LinearInterpolation : SourceProxy.NearestInterpolation
  105. }
  106. ShaderEffectSource {
  107. id: cacheItem
  108. anchors.fill: parent
  109. visible: rootItem.cached
  110. smooth: true
  111. sourceItem: shaderItem
  112. live: true
  113. hideSource: visible
  114. }
  115. ShaderEffect {
  116. id: shaderItem
  117. property variant source: sourceProxy.output
  118. property real gamma: 1.0 / Math.max(rootItem.gamma, 0.0001)
  119. anchors.fill: parent
  120. fragmentShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/gammaadjust.frag.qsb"
  121. }
  122. }