Spinning Topp Logo BlackTopp Studios
inc
edgedetectionmodifier.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  -----------------------------------------------------------------------------
42  This source file is part of ogre-procedural
43 
44  For the latest info, see http://code.google.com/p/ogre-procedural/
45 
46  Copyright (c) 2010-2013 Michael Broutin
47 
48  Permission is hereby granted, free of charge, to any person obtaining a copy
49  of this software and associated documentation files (the "Software"), to deal
50  in the Software without restriction, including without limitation the rights
51  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52  copies of the Software, and to permit persons to whom the Software is
53  furnished to do so, subject to the following conditions:
54 
55  The above copyright notice and this permission notice shall be included in
56  all copies or substantial portions of the Software.
57 
58  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
64  THE SOFTWARE.
65  -----------------------------------------------------------------------------
66  */
67 #ifndef _graphicsproceduraledgedetectionmodifier_cpp
68 #define _graphicsproceduraledgedetectionmodifier_cpp
69 
70 #include "Graphics/Procedural/Texture/edgedetectionmodifier.h"
71 #include "Graphics/Procedural/Texture/solidgenerator.h"
72 #include "Graphics/Procedural/Texture/blurmodifier.h"
73 
74 #include "MathTools/mathtools.h"
75 
76 namespace Mezzanine
77 {
78  namespace Graphics
79  {
80  namespace Procedural
81  {
83  EdgeDetect(EdgeDetectionModifier::DM_Sobel),
84  LowerThreshold(20),
85  UpperThreshold(100),
86  EdgeDetectionSigma(92)
87  { }
88 
90  { }
91 
93  {
94  Integer TargetWidth = static_cast<Integer>( Buffer.GetWidth() );
95  Integer TargetHeight = static_cast<Integer>( Buffer.GetHeight() );
96 
97  TextureBuffer TempBuffer(Buffer.GetWidth(),Buffer.GetHeight());
99 
100  for( Integer Y = 0 ; Y < TargetHeight ; ++Y )
101  {
102  for( Integer X = 0 ; X < TargetWidth ; ++X )
103  {
104  Real PixelAlpha = Buffer.GetAlphaReal(static_cast<Whole>(X),static_cast<Whole>(Y));
105  VectorBlock Block = this->GetBlock(X,Y,Buffer);
106  Vector3 CenterColour = Block[4];
107  Vector3 Difference(0.0,0.0,0.0);
108  for( Integer Row = 0 ; Row < 3 ; ++Row )
109  {
110  for( Integer Column = 0 ; Column < 3 ; ++Column )
111  {
112  if( Row != 1 || Column != 1 ) {
113  Vector3 Temp = CenterColour - Block[Row * 3 + Column];
114  if( MathTools::Abs(Temp.X) > Difference.X ) Difference.X = MathTools::Abs(Temp.X);
115  if( MathTools::Abs(Temp.Y) > Difference.Y ) Difference.Y = MathTools::Abs(Temp.Y);
116  if( MathTools::Abs(Temp.Z) > Difference.Z ) Difference.Z = MathTools::Abs(Temp.Z);
117  }
118  }
119  }
120  TempBuffer.SetPixel(static_cast<Whole>(X),static_cast<Whole>(Y),ColourValue(Difference.X,Difference.Y,Difference.Z,PixelAlpha));
121  }
122  }
123 
124  Buffer.SetData(TempBuffer);
125  }
126 
128  {
129  Integer TargetWidth = static_cast<Integer>( Buffer.GetWidth() );
130  Integer TargetHeight = static_cast<Integer>( Buffer.GetHeight() );
131 
132  TextureBuffer TempBuffer(Buffer.GetWidth(),Buffer.GetHeight());
134 
135  for( Integer Y = 0 ; Y < TargetHeight ; ++Y )
136  {
137  for( Integer X = 0 ; X < TargetWidth ; ++X )
138  {
139  Real PixelAlpha = Buffer.GetAlphaReal(static_cast<Whole>(X),static_cast<Whole>(Y));
140  VectorBlock Block = this->GetBlock(X,Y,Buffer);
141  Vector3 Difference(0.0,0.0,0.0);
142  Vector3 Temp(0.0,0.0,0.0);
143 
144  for( Integer Index = 0 ; Index < 3 ; ++Index )
145  {
146  Temp = Block[Index] - Block[6 + (2 - Index)];
147  if( MathTools::Abs(Temp.X) > Difference.X ) Difference.X = MathTools::Abs(Temp.X);
148  if( MathTools::Abs(Temp.Y) > Difference.Y ) Difference.Y = MathTools::Abs(Temp.Y);
149  if( MathTools::Abs(Temp.Z) > Difference.Z ) Difference.Z = MathTools::Abs(Temp.Z);
150  }
151  Temp = Block[5] - Block[3];
152  if( MathTools::Abs(Temp.X) > Difference.X ) Difference.X = MathTools::Abs(Temp.X);
153  if( MathTools::Abs(Temp.Y) > Difference.Y ) Difference.Y = MathTools::Abs(Temp.Y);
154  if( MathTools::Abs(Temp.Z) > Difference.Z ) Difference.Z = MathTools::Abs(Temp.Z);
155 
156  TempBuffer.SetPixel(static_cast<Whole>(X),static_cast<Whole>(Y),ColourValue(Difference.X,Difference.Y,Difference.Z,PixelAlpha));
157  }
158  }
159 
160  Buffer.SetData(TempBuffer);
161  }
162 
164  {
165  Integer TargetWidth = static_cast<Integer>( Buffer.GetWidth() );
166  Integer TargetHeight = static_cast<Integer>( Buffer.GetHeight() );
167 
168  TextureBuffer TempBuffer(Buffer.GetWidth(),Buffer.GetHeight());
170 
171  for( Integer Y = 0 ; Y < TargetHeight ; ++Y )
172  {
173  for( Integer X = 0 ; X < TargetWidth ; ++X )
174  {
175  Real PixelAlpha = Buffer.GetAlphaReal(static_cast<Whole>(X),static_cast<Whole>(Y));
176  VectorBlock Block = this->GetBlock(X,Y,Buffer);
177 
178  Vector3 Temp1 = Block[0] + ( Block[1] * 2.0 ) + Block[2] - Block[6] - ( Block[7] * 2.0 ) - Block[8];
179  Temp1 = Vector3(MathTools::Abs(Temp1.X),MathTools::Abs(Temp1.Y),MathTools::Abs(Temp1.Z));
180  Vector3 Temp2 = Block[2] + ( Block[5] * 2.0 ) + Block[8] - Block[0] - ( Block[3] * 2.0 ) - Block[6];
181  Temp2 = Vector3(MathTools::Abs(Temp2.X),MathTools::Abs(Temp2.Y),MathTools::Abs(Temp2.Z));
182  Vector3 Difference = Temp1 + Temp2;
183 
184  TempBuffer.SetPixel(static_cast<Whole>(X),static_cast<Whole>(Y),ColourValue(Difference.X,Difference.Y,Difference.Z,PixelAlpha));
185  }
186  }
187 
188  Buffer.SetData(TempBuffer);
189  }
190 
192  {
193  Integer TargetWidth = static_cast<Integer>( Buffer.GetWidth() );
194  Integer TargetHeight = static_cast<Integer>( Buffer.GetHeight() );
195 
196  TextureBuffer TempBuffer(Buffer.GetWidth(),Buffer.GetHeight());
198 
199  // STEP 1 - blur image
200  BlurModifier().SetSigma(this->EdgeDetectionSigma).SetBlurType(BlurModifier::BT_Gaussian).Modify(Buffer);
201 
202  // STEP 2 - calculate magnitude and edge Orientations
203  Real Div = 0.0;
204  VectorContainer Orientations(TargetWidth * TargetHeight);
205  VectorContainer Gradients(TargetWidth * TargetHeight);
206  Vector3 Difference = Vector3(-std::numeric_limits<Real>::infinity(),-std::numeric_limits<Real>::infinity(),-std::numeric_limits<Real>::infinity());
207  for( Integer Y = 0 ; Y < TargetHeight ; ++Y )
208  {
209  for( Integer X = 0 ; X < TargetWidth ; ++X )
210  {
211  VectorContainer Block = this->GetBlock(X,Y,Buffer);
212  Vector3 TempVec1 = Block[2] + Block[8] - Block[0] - Block[6] + ( ( Block[5] - Block[3] ) * 2.0 );
213  Vector3 TempVec2 = Block[0] + Block[2] - Block[6] - Block[8] + ( ( Block[1] - Block[7] ) * 2.0 );
214  Gradients[Y * TargetWidth + X] = Vector3(MathTools::Sqrt(TempVec1.X * TempVec1.X + TempVec2.X * TempVec2.X), MathTools::Sqrt(TempVec1.Y * TempVec1.Y + TempVec2.Y * TempVec2.Y), MathTools::Sqrt(TempVec1.Z * TempVec1.Z + TempVec2.Z * TempVec2.Z));
215  if( Gradients[Y * TargetWidth + X].X > Difference.X ) Difference.X = Gradients[Y * TargetWidth + X].X;
216  if( Gradients[Y * TargetWidth + X].Y > Difference.Y ) Difference.Y = Gradients[Y * TargetWidth + X].Y;
217  if( Gradients[Y * TargetWidth + X].Z > Difference.Z ) Difference.Z = Gradients[Y * TargetWidth + X].Z;
218  Orientations[Y * TargetWidth + X].Zero();
219  if( TempVec1.X == 0.0 ) {
220  Orientations[Y * TargetWidth + X].X = ( TempVec2.X == 0.0 ) ? 0.0 : 90.0;
221  }else{
222  Div = TempVec2.X / TempVec1.X;
223  if( Div < 0.0 ) {
224  Orientations[Y * TargetWidth + X].X = 180.0 - ( MathTools::ATan(-Div) * MathTools::GetRadToDegMultiplier() );
225  }else{
226  Orientations[Y * TargetWidth + X].X = ( MathTools::ATan(Div) * MathTools::GetRadToDegMultiplier() );
227  }
228 
229  if( Orientations[Y * TargetWidth + X].X < 22.5 ) {
230  Orientations[Y * TargetWidth + X].X = 0.0;
231  }else if( Orientations[Y * TargetWidth + X].X < 67.5 ) {
232  Orientations[Y * TargetWidth + X].X = 45.0;
233  }else if( Orientations[Y * TargetWidth + X].X < 112.5 ) {
234  Orientations[Y * TargetWidth + X].X = 90.0;
235  }else if( Orientations[Y * TargetWidth + X].X < 157.5 ) {
236  Orientations[Y * TargetWidth + X].X = 135.0;
237  }else{
238  Orientations[Y * TargetWidth + X].X = 0.0;
239  }
240  }
241  if( TempVec1.Y == 0.0 ) {
242  Orientations[Y * TargetWidth + X].Y = ( TempVec2.Y == 0.0 ) ? 0.0 : 90.0;
243  }else{
244  Div = TempVec2.Y / TempVec1.Y;
245  if( Div < 0.0 ) {
246  Orientations[Y * TargetWidth + X].Y = 180.0 - ( MathTools::ATan(-Div) * MathTools::GetRadToDegMultiplier() );
247  }else{
248  Orientations[Y * TargetWidth + X].Y = ( MathTools::ATan(Div) * MathTools::GetRadToDegMultiplier() );
249  }
250 
251  if( Orientations[Y * TargetWidth + X].Y < 22.5 ) {
252  Orientations[Y * TargetWidth + X].Y = 0.0;
253  }else if( Orientations[Y * TargetWidth + X].Y < 67.5 ) {
254  Orientations[Y * TargetWidth + X].Y = 45.0;
255  }else if( Orientations[Y * TargetWidth + X].Y < 112.5 ) {
256  Orientations[Y * TargetWidth + X].Y = 90.0;
257  }else if( Orientations[Y * TargetWidth + X].Y < 157.5 ) {
258  Orientations[Y * TargetWidth + X].Y = 135.0;
259  }else{
260  Orientations[Y * TargetWidth + X].Y = 0.0;
261  }
262  }
263  if( TempVec1.Z == 0.0 ) {
264  Orientations[Y * TargetWidth + X].Z = ( TempVec2.Z == 0.0 ) ? 0.0 : 90.0;
265  }else{
266  Div = TempVec2.Z / TempVec1.Z;
267  if( Div < 0.0 ) {
268  Orientations[Y * TargetWidth + X].Z = 180.0 - ( MathTools::ATan(-Div) * MathTools::GetRadToDegMultiplier() );
269  }else{
270  Orientations[Y * TargetWidth + X].Z = ( MathTools::ATan(Div) * MathTools::GetRadToDegMultiplier() );
271  }
272 
273  if( Orientations[Y * TargetWidth + X].Z < 22.5 ) {
274  Orientations[Y * TargetWidth + X].Z = 0.0;
275  }else if( Orientations[Y * TargetWidth + X].Z < 67.5 ) {
276  Orientations[Y * TargetWidth + X].Z = 45.0;
277  }else if( Orientations[Y * TargetWidth + X].Z < 112.5 ) {
278  Orientations[Y * TargetWidth + X].Z = 90.0;
279  }else if( Orientations[Y * TargetWidth + X].Z < 157.5 ) {
280  Orientations[Y * TargetWidth + X].Z = 135.0;
281  }else{
282  Orientations[Y * TargetWidth + X].Z = 0.0;
283  }
284  }
285  }
286  }
287 
288  // STEP 3 - suppres non maximums
289  for( Integer Y = 1 ; Y < ( TargetHeight - 1 ) ; ++Y )
290  {
291  for( Integer X = 1 ; X < ( TargetWidth - 1 ) ; ++X )
292  {
293  Div = Gradients[Y * TargetWidth + X].X / Difference.X;
294  switch( static_cast<Integer>( Orientations[Y * TargetWidth + X].X ) )
295  {
296  default:
297  case 0:
298  {
299  if( ( Gradients[Y * TargetWidth + X].X < Gradients[Y * TargetWidth + (X - 1)].X ) || ( Gradients[Y * TargetWidth + X].X < Gradients[Y * TargetWidth + (X + 1)].X ) ) {
300  Div = 0.0;
301  }
302  break;
303  }
304  case 45:
305  {
306  if( ( Gradients[Y * TargetWidth + X].X < Gradients[(Y + 1) * TargetWidth + (X - 1)].X ) || ( Gradients[Y * TargetWidth + X].X < Gradients[(Y - 1) * TargetWidth + (X + 1)].X ) ) {
307  Div = 0.0;
308  }
309  break;
310  }
311  case 90:
312  {
313  if( ( Gradients[Y * TargetWidth + X].X < Gradients[(Y + 1) * TargetWidth + X].X ) || ( Gradients[Y * TargetWidth + X].X < Gradients[(Y - 1) * TargetWidth + X].X ) ) {
314  Div = 0.0;
315  }
316  break;
317  }
318  case 135:
319  {
320  if( ( Gradients[Y * TargetWidth + X].X < Gradients[(Y + 1) * TargetWidth + (X + 1)].X ) || ( Gradients[Y * TargetWidth + X].X < Gradients[(Y - 1) * TargetWidth + (X - 1)].X ) ) {
321  Div = 0.0;
322  }
323  break;
324  }
325  }
326  TempBuffer.SetRedReal( static_cast<Whole>(X), static_cast<Whole>(Y), Div );
327  Div = Gradients[Y * TargetWidth + X].Y / Difference.Y;
328  switch( static_cast<Integer>( Orientations[Y * TargetWidth + X].Y ) )
329  {
330  default:
331  case 0:
332  {
333  if( ( Gradients[Y * TargetWidth + X].Y < Gradients[Y * TargetWidth + (X - 1)].Y ) || ( Gradients[Y * TargetWidth + X].Y < Gradients[Y * TargetWidth + (X + 1)].Y ) ) {
334  Div = 0.0;
335  }
336  break;
337  }
338  case 45:
339  {
340  if( ( Gradients[Y * TargetWidth + X].Y < Gradients[(Y + 1) * TargetWidth + (X - 1)].Y ) || ( Gradients[Y * TargetWidth + X].Y < Gradients[(Y - 1) * TargetWidth + (X + 1)].Y ) ) {
341  Div = 0.0;
342  }
343  break;
344  }
345  case 90:
346  {
347  if( ( Gradients[Y * TargetWidth + X].Y < Gradients[(Y + 1) * TargetWidth + X].Y ) || ( Gradients[Y * TargetWidth + X].Y < Gradients[(Y - 1) * TargetWidth + X].Y ) ) {
348  Div = 0.0;
349  }
350  break;
351  }
352  case 135:
353  {
354  if( ( Gradients[Y * TargetWidth + X].Y < Gradients[(Y + 1) * TargetWidth + (X + 1)].Y ) || ( Gradients[Y * TargetWidth + X].Y < Gradients[(Y - 1) * TargetWidth + (X - 1)].Y ) ) {
355  Div = 0.0;
356  }
357  break;
358  }
359  }
360  TempBuffer.SetGreenReal( static_cast<Whole>(X), static_cast<Whole>(Y), Div );
361  Div = Gradients[Y * TargetWidth + X].Z / Difference.Z;
362  switch( static_cast<Integer>( Orientations[Y * TargetWidth + X].Z ) )
363  {
364  default:
365  case 0:
366  {
367  if( ( Gradients[Y * TargetWidth + X].Z < Gradients[Y * TargetWidth + (X - 1)].Z ) || ( Gradients[Y * TargetWidth + X].Z < Gradients[Y * TargetWidth + (X + 1)].Z ) ) {
368  Div = 0.0;
369  }
370  break;
371  }
372  case 45:
373  {
374  if( ( Gradients[Y * TargetWidth + X].Z < Gradients[(Y + 1) * TargetWidth + (X - 1)].Z ) || ( Gradients[Y * TargetWidth + X].Z < Gradients[(Y - 1) * TargetWidth + (X + 1)].Z ) ) {
375  Div = 0.0;
376  }
377  break;
378  }
379  case 90:
380  {
381  if( ( Gradients[Y * TargetWidth + X].Z < Gradients[(Y + 1) * TargetWidth + X].Z ) || ( Gradients[Y * TargetWidth + X].Z < Gradients[(Y - 1) * TargetWidth + X].Z ) ) {
382  Div = 0.0;
383  }
384  break;
385  }
386  case 135:
387  {
388  if( (Gradients[Y * TargetWidth + X].Z < Gradients[(Y + 1) * TargetWidth + (X + 1)].Z ) || ( Gradients[Y * TargetWidth + X].Z < Gradients[(Y - 1) * TargetWidth + (X - 1)].Z ) ) {
389  Div = 0.0;
390  }
391  break;
392  }
393  }
394  TempBuffer.SetBlueReal( static_cast<Whole>(X), static_cast<Whole>(Y), Div );
395  }
396  }
397 
398  // STEP 4 - hysteresis
399  Buffer.SetData(TempBuffer);
400  Real MaxThreshold = static_cast<Real>( this->UpperThreshold ) / 255.0;
401  for( Integer Y = 1 ; Y < ( TargetHeight - 1 ) ; ++Y )
402  {
403  for( Integer X = 1 ; X < ( TargetWidth - 1 ) ; ++X )
404  {
405  ColourValue Pixel = Buffer.GetPixel(static_cast<Whole>(X), static_cast<Whole>(Y));
406  if( Pixel.RedChannel < MaxThreshold )
407  {
408  if( Pixel.RedChannel < static_cast<Real>( this->LowerThreshold ) / 255.0 )
409  TempBuffer.SetRedReal( static_cast<Whole>(X), static_cast<Whole>(Y), 0.0 );
410  else
411  {
412  if( ( Buffer.GetRedReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y )) < MaxThreshold ) &&
413  ( Buffer.GetRedReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y )) < MaxThreshold ) &&
414  ( Buffer.GetRedReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
415  ( Buffer.GetRedReal(static_cast<Whole>(X ),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
416  ( Buffer.GetRedReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
417  ( Buffer.GetRedReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y + 1)) < MaxThreshold ) &&
418  ( Buffer.GetRedReal(static_cast<Whole>(X ),static_cast<Whole>(Y + 1)) < MaxThreshold ) &&
419  ( Buffer.GetRedReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y + 1)) < MaxThreshold ) )
420  {
421  TempBuffer.SetRedReal( static_cast<Whole>(X), static_cast<Whole>(Y), 0.0 );
422  }
423  }
424  }
425  if( Pixel.GreenChannel < MaxThreshold )
426  {
427  if( Pixel.GreenChannel < static_cast<Real>( this->LowerThreshold ) / 255.0 )
428  TempBuffer.SetGreenReal( static_cast<Whole>(X), static_cast<Whole>(Y), 0.0 );
429  else
430  {
431  if( ( Buffer.GetGreenReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y )) < MaxThreshold ) &&
432  ( Buffer.GetGreenReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y )) < MaxThreshold ) &&
433  ( Buffer.GetGreenReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
434  ( Buffer.GetGreenReal(static_cast<Whole>(X ),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
435  ( Buffer.GetGreenReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
436  ( Buffer.GetGreenReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y + 1)) < MaxThreshold ) &&
437  ( Buffer.GetGreenReal(static_cast<Whole>(X ),static_cast<Whole>(Y + 1)) < MaxThreshold ) &&
438  ( Buffer.GetGreenReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y + 1)) < MaxThreshold ) )
439  {
440  TempBuffer.SetGreenReal( static_cast<Whole>(X), static_cast<Whole>(Y), 0.0 );
441  }
442  }
443  }
444  if( Pixel.BlueChannel < MaxThreshold )
445  {
446  if( Pixel.BlueChannel < static_cast<Real>( this->LowerThreshold ) / 255.0 )
447  TempBuffer.SetBlueReal( static_cast<Whole>(X), static_cast<Whole>(Y), 0.0 );
448  else
449  {
450  if( ( Buffer.GetBlueReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y )) < MaxThreshold ) &&
451  ( Buffer.GetBlueReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y )) < MaxThreshold ) &&
452  ( Buffer.GetBlueReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
453  ( Buffer.GetBlueReal(static_cast<Whole>(X ),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
454  ( Buffer.GetBlueReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y - 1)) < MaxThreshold ) &&
455  ( Buffer.GetBlueReal(static_cast<Whole>(X - 1),static_cast<Whole>(Y + 1)) < MaxThreshold ) &&
456  ( Buffer.GetBlueReal(static_cast<Whole>(X ),static_cast<Whole>(Y + 1)) < MaxThreshold ) &&
457  ( Buffer.GetBlueReal(static_cast<Whole>(X + 1),static_cast<Whole>(Y + 1)) < MaxThreshold ) )
458  {
459  TempBuffer.SetBlueReal( static_cast<Whole>(X), static_cast<Whole>(Y), 0.0 );
460  }
461  }
462  }
463  }
464  }
465 
466  Buffer.SetData(TempBuffer);
467  }
468 
470  {
471  VectorBlock Block(9);
472  ColourValue Pixel = Buffer.GetPixel(static_cast<Whole>(X),static_cast<Whole>(Y));
473  for( Integer Row = -1 ; Row < 2 ; ++Row )
474  {
475  for( Integer Column = -1 ; Column < 2 ; ++Column )
476  {
477  Integer CurrIndex = ( Row + 1 ) * 3 + ( Column + 1 );
478  // Assign safe default of no change
479  Block[CurrIndex].SetValues(Pixel.RedChannel,Pixel.GreenChannel,Pixel.BlueChannel);
480  if( Row != 0 || Column != 0 ) {
481  if( ( X + Column ) < 0 || ( X + Column ) >= static_cast<Integer>( Buffer.GetWidth() ) ) {
482  continue;
483  }
484  if( ( Y + Row ) < 0 || ( Y + Row ) >= static_cast<Integer>( Buffer.GetHeight() ) ) {
485  continue;
486  }
487  // Apply the change now that we know we have a valid pixel that isn't the current Pixel
488  Block[CurrIndex].SetValues( Buffer.GetRedReal(static_cast<Whole>(X+Column),static_cast<Whole>(Y+Row)),
489  Buffer.GetGreenReal(static_cast<Whole>(X+Column),static_cast<Whole>(Y+Row)),
490  Buffer.GetBlueReal(static_cast<Whole>(X+Column),static_cast<Whole>(Y+Row)) );
491  }
492  }
493  }
494  return Block;
495  }
496 
497  ///////////////////////////////////////////////////////////////////////////////
498  // Utility
499 
501  {
502  switch( this->EdgeDetect )
503  {
504  case EdgeDetectionModifier::DM_Homogenity: this->ProcessHomogenity(Buffer); break;
505  case EdgeDetectionModifier::DM_Difference: this->ProcessDifference(Buffer); break;
506  default:
507  case EdgeDetectionModifier::DM_Sobel: this->ProcessSobel(Buffer); break;
508  case EdgeDetectionModifier::DM_Canny: this->ProcessCanny(Buffer); break;
509  }
510  }
511 
513  { return "EdgeDetectionModifier"; }
514 
515  ///////////////////////////////////////////////////////////////////////////////
516  // Configuration
517 
519  {
520  this->EdgeDetect = Mode;
521  return *this;
522  }
523 
525  {
526  this->LowerThreshold = Low;
527  return *this;
528  }
529 
531  {
532  this->UpperThreshold = High;
533  return *this;
534  }
535 
537  {
538  this->EdgeDetectionSigma = Sigma;
539  return *this;
540  }
541  }//Procedural
542  }//Graphics
543 }//Mezzanine
544 
545 #endif
VectorBlock GetBlock(const Integer X, const Integer Y, TextureBuffer &Buffer) const
Gets the colours of the specified pixel and all surrounding pixels.
ColourChannelType & GetPixel(const Whole X, const Whole Y, const UInt16 Component)
Gets access to the pixel at the specified position in this buffer. the X or Y location go beyond the ...
Real X
Coordinate on the X vector.
Definition: vector3.h:85
Real Z
Coordinate on the Z vector.
Definition: vector3.h:89
Real GetBlueReal(const Whole X, const Whole Y) const
Gets the blue colour value of a specified pixel. the X or Y location go beyond the set size of this t...
Whole GetWidth() const
Get the pixel width of this texture.
Real GetAlphaReal(const Whole X, const Whole Y) const
Gets the alpha colour value of a specified pixel. the X or Y location go beyond the set size of this ...
Use the Sobel Operator algorithm to detect edges.
DetectionMode EdgeDetect
The algorithm to use to detect edges.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Real GetGreenReal(const Whole X, const Whole Y) const
Gets the green colour value of a specified pixel. the X or Y location go beyond the set size of this ...
Use the Canny multi-stage algorthm to detect edges.
UInt8 UpperThreshold
The highest value for each colour channel that will be considered valid.
virtual String GetName() const
Gets the name of this modifier.
void ProcessDifference(TextureBuffer &Buffer)
Detect edges in the provided texture by processing pixels above and below the current pixel...
virtual void Modify(TextureBuffer &Buffer)
Alters the generated pixels in a TextureBuffer.
Fills full image with given colour.
static ColourValue Black()
Creates a ColourValue representing the colour Black.
SolidGenerator & SetColour(const ColourValue &Colour)
Sets the colour of the background.
uint8_t UInt8
An 8-bit unsigned integer.
Definition: datatypes.h:118
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
virtual void AddToTextureBuffer(TextureBuffer &Buffer) const
Replaces and populates the pixels as configured in this generator to a TextureBuffer.
A modifier that will attempt to reduce the sharpness of the texture.
Definition: blurmodifier.h:82
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
EdgeDetectionModifier & SetDetectionType(const DetectionMode Mode)
Sets the detection algorith to use for detecting edges.
BlurModifier & SetSigma(const UInt8 Sigma)
Sets the sigma for each each pixel to be processed.
Real GreenChannel
Value from 0.0 to 1.0 representing the amount of green present in the colour. 1.0 if very green...
Definition: colourvalue.h:73
std::vector< Vector3 > VectorContainer
Convenience typedef for an array of Vector3's.
void ProcessSobel(TextureBuffer &Buffer)
Detect edges using the Sobel Operator.
VectorContainer VectorBlock
Convenience typedef for returning a 3x3 matrix of Vector3's containing colour data.
UInt8 LowerThreshold
The lowest value for each colour channel that will be considered valid.
DetectionMode
An enum describing which algorithm to use for edge detection.
A modifier that will reduce the texture to an outline of the detected edges in the texture...
Real Y
Coordinate on the Y vector.
Definition: vector3.h:87
EdgeDetectionModifier & SetEdgeDetectionSigma(const UInt8 Sigma)
Sets the sigma for the blur stage of the Canny filter.
BlurModifier & SetBlurType(const BlurType Blur)
Sets the type of bluring operation to be used.
Detect edges based on difference in colour between the pixel being processed and all surrounding pixe...
EdgeDetectionModifier & SetUpperThreshold(const UInt8 High)
Sets the upper threshold for the Canny filter.
Real GetRedReal(const Whole X, const Whole Y) const
Gets the red colour value of a specified pixel. the X or Y location go beyond the set size of this te...
void ProcessCanny(TextureBuffer &Buffer)
Detect edges using the Canny multi-stage algorithm.
A convenience buffer that stores pixel colour values of a texture to be generated.
Definition: texturebuffer.h:86
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Whole GetHeight() const
Get the pixel height of this texture.
Real BlueChannel
Value from 0.0 to 1.0 representing the amount of blue present in the colour. 1.0 if very blue...
Definition: colourvalue.h:76
void ProcessHomogenity(TextureBuffer &Buffer)
Detect edges in the provided texture by processing all surrounding pixels.
Real RedChannel
Value from 0.0 to 1.0 representing the amount of red present in the colour. 1.0 if very red...
Definition: colourvalue.h:70
Detect edges based on difference in colour between opposite surrounding pixels.
UInt8 EdgeDetectionSigma
The sigma to set for the blur step of the Canny filter.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
EdgeDetectionModifier & SetLowerThreshold(const UInt8 Low)
Sets the lower threshold for the Canny filter.
void SetData(const TextureBuffer &Other)
Copies image data from another buffer into this buffer.
virtual void Modify(TextureBuffer &Buffer)
Alters the generated pixels in a TextureBuffer.