88#include < QGraphicsScene>
99#include < QImage>
1010#include < QPainter>
11- #include < random>
1211#include < array>
12+ #include < random>
1313
1414PixelateTool::PixelateTool (QObject* parent)
1515 : AbstractTwoPointTool(parent)
@@ -70,16 +70,14 @@ void PixelateTool::process(QPainter& painter, const QPixmap& pixmap)
7070 QRect selectionScaled = QRect (selection.topLeft () * pixelRatio,
7171 selection.bottomRight () * pixelRatio);
7272
73-
7473 // calculate the size of the pixelation effect using the tool size
75- int width = qMax (1 ,
76- static_cast <int >(selection.width () * (0.5 / qMax (1 , size () + 1 ))));
77- int height = qMax (1 ,
78- static_cast <int >(selection.height () * (0.5 / qMax (1 , size () + 1 ))));
74+ int width = qMax (
75+ 1 , static_cast <int >(selection.width () * (0.5 / qMax (1 , size () + 1 ))));
76+ int height = qMax (
77+ 1 , static_cast <int >(selection.height () * (0.5 / qMax (1 , size () + 1 ))));
7978
8079 QSize effect_size = QSize (width, height);
8180
82-
8381 // the PRNG is only used for visual effects and NOT part of the security
8482 // boundary
8583 std::mt19937 prng (42 );
@@ -92,42 +90,44 @@ void PixelateTool::process(QPainter& painter, const QPixmap& pixmap)
9290 // generating a monochromatic box when the fringe is monochromatic
9391 std::normal_distribution<float > noise (0 , 0 .1f );
9492
95-
96- QPoint offset_top
97- ( 0 , selectionScaled. topLeft (). y () == 0 ? 0 : - 1 );
98- QPoint offset_bottom
99- ( 0 , selectionScaled. bottomLeft (). y () == pixmap. rect (). bottomLeft (). y () ? 0 : 1 );
100- QPoint offset_left
101- (selectionScaled. topLeft (). x () == 0 ? 0 : - 1 , 0 );
102- QPoint offset_right
103- (selectionScaled. topRight (). x () == pixmap. rect (). topRight (). x () ? 0 : 1 , 0 );
93+ QPoint offset_top ( 0 , selectionScaled. topLeft (). y () == 0 ? 0 : - 1 );
94+ QPoint offset_bottom (
95+ 0 ,
96+ selectionScaled. bottomLeft (). y () == pixmap. rect (). bottomLeft (). y () ? 0
97+ : 1 );
98+ QPoint offset_left (selectionScaled. topLeft (). x () == 0 ? 0 : - 1 , 0 );
99+ QPoint offset_right (
100+ selectionScaled. topRight (). x () == pixmap. rect (). topRight (). x () ? 0 : 1 ,
101+ 0 );
104102
105103 // only values from the fringe will be used to compute the pseudo-pixelation
106104 std::array<QImage, 4 > fringe = {
107105 // top fringe
108- pixmap.copy (QRect (selectionScaled.topLeft () + offset_top,
109- selectionScaled.topRight () + offset_top))
110- .toImage (),
106+ pixmap
107+ .copy (QRect (selectionScaled.topLeft () + offset_top,
108+ selectionScaled.topRight () + offset_top))
109+ .toImage (),
111110 // bottom fringe
112- pixmap.copy (QRect (selectionScaled.bottomLeft () + offset_bottom,
113- selectionScaled.bottomRight () + offset_bottom))
114- .toImage (),
111+ pixmap
112+ .copy (QRect (selectionScaled.bottomLeft () + offset_bottom,
113+ selectionScaled.bottomRight () + offset_bottom))
114+ .toImage (),
115115 // left fringe
116- pixmap.copy (QRect (selectionScaled.topLeft () + offset_left,
117- selectionScaled.bottomLeft () + offset_left))
118- .toImage (),
116+ pixmap
117+ .copy (QRect (selectionScaled.topLeft () + offset_left,
118+ selectionScaled.bottomLeft () + offset_left))
119+ .toImage (),
119120 // right fringe
120- pixmap.copy (QRect (selectionScaled.topRight () + offset_right,
121- selectionScaled.bottomRight () + offset_right))
122- .toImage ()
121+ pixmap
122+ .copy (QRect (selectionScaled.topRight () + offset_right,
123+ selectionScaled.bottomRight () + offset_right))
124+ .toImage ()
123125 };
124126
125-
126127 // Image where the pseudo-pixelation is calculated.
127128 // This will later be scaled to cover the selected area.
128129 QImage pixelated = QImage (effect_size, QImage::Format_RGB32);
129130
130-
131131 // For every pixel of the effect, we consider four projections
132132 // to the fringe and sample a pixel from there.
133133 // Then a horizontal and vertical interpolation are calculated.
@@ -138,57 +138,58 @@ void PixelateTool::process(QPainter& painter, const QPixmap& pixmap)
138138 float n = noise (prng);
139139
140140 // relative horizontal resp. vertical position
141- float horizontal = x / (float ) width;
142- float vertical = y / (float ) height;
141+ float horizontal = x / (float )width;
142+ float vertical = y / (float )height;
143143
144144 for (int i = 0 ; i < 4 ; ++i) {
145145 QColor c = fringe[i].pixel (
146- std::clamp (
147- static_cast <int >(
148- horizontal * fringe[i].width ()
149- + sampling_noise (prng)),
150- 0 , fringe[i].width ()-1 ),
151- std::clamp (
152- static_cast <int >(
153- vertical * fringe[i].height ()
154- + sampling_noise (prng)),
155- 0 , fringe[i].height ()-1 ));
146+ std::clamp (static_cast <int >(horizontal * fringe[i].width () +
147+ sampling_noise (prng)),
148+ 0 ,
149+ fringe[i].width () - 1 ),
150+ std::clamp (static_cast <int >(vertical * fringe[i].height () +
151+ sampling_noise (prng)),
152+ 0 ,
153+ fringe[i].height () - 1 ));
156154 samples[i][0 ] = c.redF ();
157155 samples[i][1 ] = c.greenF ();
158156 samples[i][2 ] = c.blueF ();
159157 }
160158
161159 // weights of the horizontal resp. vertical interpolation
162- float weight_h = (qMin (x, width - x) / width)
163- - (qMin (y, height - y) / height)
164- + 0.5 ;
160+ float weight_h = (qMin (x, width - x) / width) -
161+ (qMin (y, height - y) / height) + 0.5 ;
165162
166163 float weight_v = 1 - weight_h;
167164
168165 // compute the weighted sum of the vertical and horizontal
169166 // interpolations
170- std::array<int , 3 > rgb = {0 , 0 , 0 };
167+ std::array<int , 3 > rgb = { 0 , 0 , 0 };
171168 for (int i = 0 ; i < 3 ; ++i) {
172169 float c =
173- // horizontal interpolation
174- weight_h * ((1 -horizontal) * samples[2 ][i] + horizontal * samples[3 ][i])
170+ // horizontal interpolation
171+ weight_h * ((1 - horizontal) * samples[2 ][i] +
172+ horizontal * samples[3 ][i])
175173
176- // vertical interpolation
177- + weight_v * ((1 -vertical) * samples[0 ][i] + vertical * samples[1 ][i])
174+ // vertical interpolation
175+ + weight_v * ((1 - vertical) * samples[0 ][i] +
176+ vertical * samples[1 ][i])
178177
179- // additional noise
178+ // additional noise
180179 + n;
181180
182181 rgb[i] = static_cast <int >(0xff * c);
183182 rgb[i] = std::clamp (rgb[i], 0 , 0xff );
184183 }
185184 QRgb value = qRgb (rgb[0 ], rgb[1 ], rgb[2 ]);
186- pixelated.setPixel (x,y, value);
185+ pixelated.setPixel (x, y, value);
187186 }
188187 }
189188
190- pixelated = pixelated.scaled (selection.width (), selection.height (),
191- Qt::IgnoreAspectRatio, Qt::FastTransformation);
189+ pixelated = pixelated.scaled (selection.width (),
190+ selection.height (),
191+ Qt::IgnoreAspectRatio,
192+ Qt::FastTransformation);
192193
193194 painter.drawImage (selection, pixelated);
194195}
0 commit comments