Changeset View
Changeset View
Standalone View
Standalone View
imagelib/effects/kpEffectBalance.cpp
Show All 32 Lines | |||||
33 | #include <cmath> | 33 | #include <cmath> | ||
34 | #include <QImage> | 34 | #include <QImage> | ||
35 | #include "kpLogCategories.h" | 35 | #include "kpLogCategories.h" | ||
36 | #include "pixmapfx/kpPixmapFX.h" | 36 | #include "pixmapfx/kpPixmapFX.h" | ||
37 | #include <qdatetime.h> | 37 | #include <qdatetime.h> | ||
38 | 38 | | |||
39 | static inline int between0And255 (int val) | 39 | static inline int between0And255 (int val) | ||
40 | { | 40 | { | ||
41 | if (val < 0) | 41 | if (val < 0) { | ||
42 | return 0; | 42 | return 0; | ||
43 | else if (val > 255) | 43 | } | ||
44 | | ||||
45 | if (val > 255) { | ||||
44 | return 255; | 46 | return 255; | ||
45 | else | 47 | } | ||
48 | | ||||
46 | return val; | 49 | return val; | ||
47 | } | 50 | } | ||
48 | 51 | | |||
49 | 52 | | |||
50 | static inline int brightness (int base, int strength) | 53 | static inline int brightness (int base, int strength) | ||
51 | { | 54 | { | ||
52 | return between0And255 (base + strength * 255 / 50); | 55 | return between0And255 (base + strength * 255 / 50); | ||
53 | } | 56 | } | ||
Show All 22 Lines | |||||
76 | static inline QRgb brightnessContrastGammaForRGB (QRgb rgb, | 79 | static inline QRgb brightnessContrastGammaForRGB (QRgb rgb, | ||
77 | int channels, | 80 | int channels, | ||
78 | int brightness, int contrast, int gamma) | 81 | int brightness, int contrast, int gamma) | ||
79 | { | 82 | { | ||
80 | int red = qRed (rgb); | 83 | int red = qRed (rgb); | ||
81 | int green = qGreen (rgb); | 84 | int green = qGreen (rgb); | ||
82 | int blue = qBlue (rgb); | 85 | int blue = qBlue (rgb); | ||
83 | 86 | | |||
84 | if (channels & kpEffectBalance::Red) | 87 | if (channels & kpEffectBalance::Red) { | ||
85 | red = brightnessContrastGamma (red, brightness, contrast, gamma); | 88 | red = brightnessContrastGamma (red, brightness, contrast, gamma); | ||
86 | if (channels & kpEffectBalance::Green) | 89 | } | ||
90 | if (channels & kpEffectBalance::Green) { | ||||
87 | green = brightnessContrastGamma (green, brightness, contrast, gamma); | 91 | green = brightnessContrastGamma (green, brightness, contrast, gamma); | ||
88 | if (channels & kpEffectBalance::Blue) | 92 | } | ||
93 | if (channels & kpEffectBalance::Blue) { | ||||
89 | blue = brightnessContrastGamma (blue, brightness, contrast, gamma); | 94 | blue = brightnessContrastGamma (blue, brightness, contrast, gamma); | ||
95 | } | ||||
90 | 96 | | |||
91 | return qRgba (red, green, blue, qAlpha (rgb)); | 97 | return qRgba (red, green, blue, qAlpha (rgb)); | ||
92 | } | 98 | } | ||
93 | 99 | | |||
94 | 100 | | |||
95 | // public static | 101 | // public static | ||
96 | kpImage kpEffectBalance::applyEffect (const kpImage &image, | 102 | kpImage kpEffectBalance::applyEffect (const kpImage &image, | ||
97 | int channels, | 103 | int channels, | ||
98 | int brightness, int contrast, int gamma) | 104 | int brightness, int contrast, int gamma) | ||
99 | { | 105 | { | ||
100 | qCDebug(kpLogImagelib) << "kpEffectBalance::applyEffect(" | 106 | qCDebug(kpLogImagelib) << "kpEffectBalance::applyEffect(" | ||
101 | << "channels=" << channels | 107 | << "channels=" << channels | ||
102 | << ",brightness=" << brightness | 108 | << ",brightness=" << brightness | ||
103 | << ",contrast=" << contrast | 109 | << ",contrast=" << contrast | ||
104 | << ",gamma=" << gamma | 110 | << ",gamma=" << gamma | ||
105 | << ")" << endl; | 111 | << ")"; | ||
106 | QTime timer; timer.start (); | 112 | QTime timer; timer.start (); | ||
107 | 113 | | |||
108 | QImage qimage = image; | 114 | QImage qimage = image; | ||
109 | qCDebug(kpLogImagelib) << "\tconvertToImage=" << timer.restart (); | 115 | qCDebug(kpLogImagelib) << "\tconvertToImage=" << timer.restart (); | ||
110 | 116 | | |||
111 | quint8 transformRed [256], | 117 | quint8 transformRed [256], | ||
112 | transformGreen [256], | 118 | transformGreen [256], | ||
113 | transformBlue [256]; | 119 | transformBlue [256]; | ||
114 | 120 | | |||
115 | for (int i = 0; i < 256; i++) | 121 | for (int i = 0; i < 256; i++) | ||
116 | { | 122 | { | ||
117 | quint8 applied = static_cast<quint8> (brightnessContrastGamma (i, brightness, contrast, gamma)); | 123 | auto applied = static_cast<quint8> (brightnessContrastGamma (i, brightness, contrast, gamma)); | ||
118 | 124 | | |||
119 | if (channels & kpEffectBalance::Red) | 125 | if (channels & kpEffectBalance::Red) { | ||
120 | transformRed [i] = applied; | 126 | transformRed [i] = applied; | ||
121 | else | 127 | } | ||
128 | else { | ||||
122 | transformRed [i] = static_cast<quint8> (i); | 129 | transformRed [i] = static_cast<quint8> (i); | ||
130 | } | ||||
123 | 131 | | |||
124 | if (channels & kpEffectBalance::Green) | 132 | if (channels & kpEffectBalance::Green) { | ||
125 | transformGreen [i] = applied; | 133 | transformGreen [i] = applied; | ||
126 | else | 134 | } | ||
135 | else { | ||||
127 | transformGreen [i] = static_cast<quint8> (i); | 136 | transformGreen [i] = static_cast<quint8> (i); | ||
137 | } | ||||
128 | 138 | | |||
129 | if (channels & kpEffectBalance::Blue) | 139 | if (channels & kpEffectBalance::Blue) { | ||
130 | transformBlue [i] = applied; | 140 | transformBlue [i] = applied; | ||
131 | else | 141 | } | ||
142 | else { | ||||
132 | transformBlue [i] = static_cast<quint8> (i); | 143 | transformBlue [i] = static_cast<quint8> (i); | ||
133 | } | 144 | } | ||
145 | } | ||||
134 | 146 | | |||
135 | qCDebug(kpLogImagelib) << "\tbuild lookup=" << timer.restart (); | 147 | qCDebug(kpLogImagelib) << "\tbuild lookup=" << timer.restart (); | ||
136 | 148 | | |||
137 | if (qimage.depth () > 8) | 149 | if (qimage.depth () > 8) | ||
138 | { | 150 | { | ||
139 | for (int y = 0; y < qimage.height (); y++) | 151 | for (int y = 0; y < qimage.height (); y++) | ||
140 | { | 152 | { | ||
141 | for (int x = 0; x < qimage.width (); x++) | 153 | for (int x = 0; x < qimage.width (); x++) | ||
142 | { | 154 | { | ||
143 | const QRgb rgb = qimage.pixel (x, y); | 155 | const QRgb rgb = qimage.pixel (x, y); | ||
144 | 156 | | |||
145 | const quint8 red = static_cast<quint8> (qRed (rgb)); | 157 | const auto red = static_cast<quint8> (qRed (rgb)); | ||
146 | const quint8 green = static_cast<quint8> (qGreen (rgb)); | 158 | const auto green = static_cast<quint8> (qGreen (rgb)); | ||
147 | const quint8 blue = static_cast<quint8> (qBlue (rgb)); | 159 | const auto blue = static_cast<quint8> (qBlue (rgb)); | ||
148 | const quint8 alpha = static_cast<quint8> (qAlpha (rgb)); | 160 | const auto alpha = static_cast<quint8> (qAlpha (rgb)); | ||
149 | 161 | | |||
150 | qimage.setPixel (x, y, | 162 | qimage.setPixel (x, y, | ||
151 | qRgba (transformRed [red], | 163 | qRgba (transformRed [red], | ||
152 | transformGreen [green], | 164 | transformGreen [green], | ||
153 | transformBlue [blue], | 165 | transformBlue [blue], | ||
154 | alpha)); | 166 | alpha)); | ||
155 | } | 167 | } | ||
156 | } | 168 | } | ||
157 | } | 169 | } | ||
158 | else | 170 | else | ||
159 | { | 171 | { | ||
160 | for (int i = 0; i < qimage.colorCount (); i++) | 172 | for (int i = 0; i < qimage.colorCount (); i++) | ||
161 | { | 173 | { | ||
162 | const QRgb rgb = qimage.color (i); | 174 | const QRgb rgb = qimage.color (i); | ||
163 | 175 | | |||
164 | const quint8 red = static_cast<quint8> (qRed (rgb)); | 176 | const auto red = static_cast<quint8> (qRed (rgb)); | ||
165 | const quint8 green = static_cast<quint8> (qGreen (rgb)); | 177 | const auto green = static_cast<quint8> (qGreen (rgb)); | ||
166 | const quint8 blue = static_cast<quint8> (qBlue (rgb)); | 178 | const auto blue = static_cast<quint8> (qBlue (rgb)); | ||
167 | const quint8 alpha = static_cast<quint8> (qAlpha (rgb)); | 179 | const auto alpha = static_cast<quint8> (qAlpha (rgb)); | ||
168 | 180 | | |||
169 | qimage.setColor (i, | 181 | qimage.setColor (i, | ||
170 | qRgba (transformRed [red], | 182 | qRgba (transformRed [red], | ||
171 | transformGreen [green], | 183 | transformGreen [green], | ||
172 | transformBlue [blue], | 184 | transformBlue [blue], | ||
173 | alpha)); | 185 | alpha)); | ||
174 | } | 186 | } | ||
175 | 187 | | |||
176 | } | 188 | } | ||
177 | 189 | | |||
178 | return qimage; | 190 | return qimage; | ||
179 | } | 191 | } | ||
180 | 192 | |