GammaAdjust.qml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. /*!
  42. \qmltype GammaAdjust
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-color
  47. \brief Alters the luminance of the source item.
  48. GammaAdjust is applied to each pixel according to the curve which is
  49. pre-defined as a power-law expression, where the property gamma is used as the
  50. reciprocal scaling exponent. Refer to the property documentation of \l{GammaAdjust::gamma}{gamma}
  51. for more details.
  52. \table
  53. \header
  54. \li Source
  55. \li Effect applied
  56. \row
  57. \li \image Original_bug.png
  58. \li \image GammaAdjust_bug.png
  59. \endtable
  60. \note This effect is available when running with OpenGL.
  61. \section1 Example
  62. The following example shows how to apply the effect.
  63. \snippet GammaAdjust-example.qml example
  64. */
  65. Item {
  66. id: rootItem
  67. /*!
  68. This property defines the source item for which the luminance is going to be
  69. adjusted.
  70. \note It is not supported to let the effect include itself, for
  71. instance by setting source to the effect's parent.
  72. */
  73. property variant source
  74. /*!
  75. This property defines the change factor for how the luminance of each pixel
  76. is altered according to the equation:
  77. \code
  78. luminance = pow(original_luminance, 1.0 / gamma); // The luminance is assumed to be between 0.0 and 1.0
  79. \endcode
  80. Setting the gamma values under 1.0 makes the image darker, the values
  81. above 1.0 lighten it.
  82. The value ranges from 0.0 (darkest) to inf (lightest). By default, the
  83. property is set to \c 1.0 (no change).
  84. \table
  85. \header
  86. \li Output examples with different gamma values
  87. \li
  88. \li
  89. \row
  90. \li \image GammaAdjust_gamma1.png
  91. \li \image GammaAdjust_gamma2.png
  92. \li \image GammaAdjust_gamma3.png
  93. \row
  94. \li \b { gamma: 0.5 }
  95. \li \b { gamma: 1.0 }
  96. \li \b { gamma: 2.0 }
  97. \endtable
  98. \table
  99. \header
  100. \li Pixel luminance curves of the above images.
  101. \li
  102. \li
  103. \row
  104. \li \image GammaAdjust_gamma1_graph.png
  105. \li \image GammaAdjust_gamma2_graph.png
  106. \li \image GammaAdjust_gamma3_graph.png
  107. \row
  108. \li Red curve: default gamma (1.0)
  109. \li
  110. \li
  111. \row
  112. \li Yellow curve: effect applied
  113. \li
  114. \li
  115. \row
  116. \li X-axis: pixel original luminance
  117. \li
  118. \li
  119. \row
  120. \li Y-axis: pixel luminance with effect applied
  121. \li
  122. \li
  123. \endtable
  124. */
  125. property real gamma: 1.0
  126. /*!
  127. This property allows the effect output pixels to be cached in order to
  128. improve the rendering performance.
  129. Every time the source or effect properties are changed, the pixels in
  130. the cache must be updated. Memory consumption is increased, because an
  131. extra buffer of memory is required for storing the effect output.
  132. It is recommended to disable the cache when the source or the effect
  133. properties are animated.
  134. By default, the property is set to \c false.
  135. */
  136. property bool cached: false
  137. SourceProxy {
  138. id: sourceProxy
  139. input: rootItem.source
  140. interpolation: input && input.smooth ? SourceProxy.LinearInterpolation : SourceProxy.NearestInterpolation
  141. }
  142. ShaderEffectSource {
  143. id: cacheItem
  144. anchors.fill: parent
  145. visible: rootItem.cached
  146. smooth: true
  147. sourceItem: shaderItem
  148. live: true
  149. hideSource: visible
  150. }
  151. ShaderEffect {
  152. id: shaderItem
  153. property variant source: sourceProxy.output
  154. property real gamma: 1.0 / Math.max(rootItem.gamma, 0.0001)
  155. anchors.fill: parent
  156. fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/gammaadjust.frag"
  157. }
  158. }