/
ProceduralTextureModifiers.cs
5961 lines (5370 loc) · 251 KB
/
ProceduralTextureModifiers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-----------------------------------------------------------------------------
This source file is part of mogre-procedural
For the latest info, see http://code.google.com/p/mogre-procedural/
my blog:http://hi.baidu.com/rainssoft
this is overwrite ogre-procedural c++ project using c#, look ogre-procedural c++ source http://code.google.com/p/ogre-procedural/
Copyright (c) 2013-2020 rains soft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
//#define PROCEDURAL_USE_FREETYPE
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
//* \addtogroup texturegrp Textures
//Elements for procedural texture creation.
//@{
//
//*
//\brief Use alpha channel as an mask for an other image.
//\details Can take normal, height or quaternion map as second input.
//
//<b>Map translations</b>
//<ul><li>%Alpha channel: a(0, 255) -> angle(0, 2pi)</li>
//<li>Coords: r(0,255) -> x(-1, 1)</li>
//<li>Height map: luminance(0, 255) -> angle(0, 2pi) (axis is Z)</li>
//<li>Normal map: is blended with the source</li></ul>
//
//<b>Options</b>
//<ul><li>Rotation: w(0, 1) -> angle(0, 2pi), rest are axis direction coordinates</li>
//<li>Sensitivity: (0, 255) -> angle(0, 2pi) * map alpha</li>
//<li>Compensation: How to deal with map</li>
//<li>Mirror: For broken normal maps</li></ul>
//
//Example:
//\code{.cpp}
//// Image colour
//Procedural::TextureBuffer bufferGradient(256);
//Procedural::Gradient(&bufferGradient).setColours(Ogre::ColourValue::Black, Ogre::ColourValue::Red, Ogre::ColourValue::Green, Ogre::ColourValue::Blue).process();
//
//// Image structure
//Procedural::TextureBuffer bufferCell(256);
//Procedural::Cell(&bufferCell).setDensity(4).setRegularity(234).process();
//
//// Filter
//Procedural::Abnormals(&bufferGradient).setParameterImage(&bufferCell).process();
//\endcode
//\dotfile texture_02.gv
//\todo Need bugfix
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Abnormals : public TextureProcessing
public class Abnormals : TextureProcessing
{
//! Methods how to work with parameter map
public enum ABNORMALS_COMPENSATION : int
{
COMPENSATION_NORMAL, //!< Use as a normal map
COMPENSATION_HEIGHT, //!< Use as a height map
COMPENSATION_QUATERNION //!< Use as a list of quaternion
}
//! Methods how to fix broken normal maps
public enum ABNORMALS_MIRROR : int
{
MIRROR_NONE, //!< None
MIRROR_X_YZ, //!< X : YZ
MIRROR_Y_XZ, //!< Y : XZ
MIRROR_X_Y_Z //!< X+Y : Z
}
private TextureBuffer mParam;
private Radian mW = new Radian();
private Vector3 mAxis = new Vector3();
private byte mSensitivity = 0;
private ABNORMALS_COMPENSATION mCompensation;
private ABNORMALS_MIRROR mMirror;
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public Abnormals(TextureBuffer pBuffer)
: base(pBuffer, "Abnormals") {
mParam = null;
mW = 0.0f;
mAxis = new Vector3(0.0f, 0.0f, 1.0f);
mSensitivity = 127;
mCompensation = ABNORMALS_COMPENSATION.COMPENSATION_NORMAL;
mMirror = ABNORMALS_MIRROR.MIRROR_NONE;
}
// *
// Set parameter image for compensation.
// \param image Pointer to second image (default NULL)
// \note If the parameter image is set to NULL there won't be any compensation.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Abnormals setParameterImage(TextureBuffer image) {
mParam = image;
return this;
}
// *
// Set rotation angle.
// \param rotation New rotation angle [0.0, 1.0] \(default 0.0)
//
public Abnormals setRotation(float rotation) {
mW = new Radian(rotation * Math.TWO_PI);
return this;
}
// *
// Set rotation angle.
// \param rotation New rotation angle [0.0, Ogre::Math::TWO_PI] rad \(default 0.0)
//
public Abnormals setRotation(Radian rotation) {
//
//ORIGINAL LINE: mW = rotation;
mW = (rotation);
return this;
}
// *
// Set rotation angle.
// \param rotation New rotation angle [0, 360] degree \(default 0)
//
public Abnormals setRotation(Degree rotation) {
mW = (Radian)rotation.ValueRadians;
return this;
}
// *
// Set rotation axis.
// \param axis New rotation axis (default Ogre::Vector3(0.0f, 0.0f, 1.0f))
//
public Abnormals setAxis(Vector3 axis) {
//
//ORIGINAL LINE: mAxis = axis;
mAxis = (axis);
return this;
}
// *
// Set rotation axis.
// \param x New x coordinate of rotation axis \(default 0.0)
// \param y New y coordinate of rotation axis \(default 0.0)
// \param z New z coordinate of rotation axis \(default 1.0)
//
public Abnormals setAxis(float x, float y) {
return setAxis(x, y, 1.0f);
}
//
//ORIGINAL LINE: Abnormals& setAxis(Ogre::float x, Ogre::float y, Ogre::float z = 1.0f)
public Abnormals setAxis(float x, float y, float z) {
mAxis = new Vector3(x, y, z);
return this;
}
// *
// Set sensitivity.
// \param sensitivity New sensitivity value [0, 255] (default 127)
//
public Abnormals setSensitivity(byte sensitivity) {
mSensitivity = sensitivity;
return this;
}
// *
// Set compensation method.
// \param compensation Compensation method to use (default COMPENSATION_NORMAL)
//
public Abnormals setCompensation(ABNORMALS_COMPENSATION compensation) {
mCompensation = compensation;
return this;
}
// *
// Set mirror method.
// \param mirror Compensation method to use (default MIRROR_NONE)
//
public Abnormals setMirror(ABNORMALS_MIRROR mirror) {
mMirror = mirror;
return this;
}
// *
// Run image manipulation
// \return Pointer to image buffer which has been set in the constructor.
//
public override TextureBuffer process() {
Quaternion qion = new Quaternion();
float sum = 0f;
Vector3 q = new Vector3();
int w = (int)mBuffer.getWidth();
int h = (int)mBuffer.getHeight();
Quaternion rotation = new Quaternion(mW, mAxis);
if (mParam != null && (mParam.getWidth() < w || mParam.getHeight() < h))
return mBuffer;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
ColourValue pixel = mBuffer.getPixel(x, y);
Quaternion v = new Quaternion(0.0f, ((pixel.r * 255.0f) - 127.5f) / 127.5f, ((pixel.b * 255.0f) - 127.5f) / 127.5f, ((pixel.g * 255.0f) - 127.5f) / 127.5f);
if (mParam != null) {
pixel = mParam.getPixel(x, y);
switch (mCompensation) {
case ABNORMALS_COMPENSATION.COMPENSATION_NORMAL:
qion = new Quaternion(0.0f, (pixel.r * 255.0f) - 127.5f, (pixel.b * 255.0f) - 127.5f, (pixel.g * 255.0f) - 127.5f);
v = v * (float)(1 - mSensitivity);
v = v + qion * ((float)mSensitivity / 127.5f);
break;
case ABNORMALS_COMPENSATION.COMPENSATION_HEIGHT:
sum = ((pixel.r + pixel.g + pixel.b) / 3.0f) * 255.0f;
qion = new Quaternion(new Radian(Math.TWO_PI * sum / 765.0f * mSensitivity), new Vector3(0.0f, 1.0f, 0.0f));
rotation = rotation * qion;
break;
case ABNORMALS_COMPENSATION.COMPENSATION_QUATERNION:
q = new Vector3((pixel.r * 255.0f) - 127.5f, (pixel.b * 255.0f) - 127.5f, (pixel.g * 255.0f) - 127.5f);
qion = new Quaternion(new Radian(2.0f / 255.0f * Math.PI * pixel.a * mSensitivity), q);
rotation = rotation * qion;
break;
}
}
v = rotation * v * rotation.Inverse();
float norm = v.Normalise();
if (mMirror == ABNORMALS_MIRROR.MIRROR_X_YZ || mMirror == ABNORMALS_MIRROR.MIRROR_X_Y_Z)
mBuffer.setRed(x, y, (1.0f - v.x * 0.5f + 0.5f));
else
mBuffer.setRed(x, y, (v.x * 0.5f + 0.5f));
if (mMirror == ABNORMALS_MIRROR.MIRROR_Y_XZ || mMirror == ABNORMALS_MIRROR.MIRROR_X_Y_Z)
mBuffer.setGreen(x, y, (1.0f - v.z * 0.5f + 0.5f));
else
mBuffer.setGreen(x, y, (v.z * 0.5f + 0.5f));
mBuffer.setBlue(x, y, (v.y * 0.5f + 0.5f));
}
}
Utils.log("Modify texture with abnormals filter");
return mBuffer;
}
}
//*
//\brief Colour extraction.
//\details Extract single color intensity, replace it with white smoke, discard the rest.
//
//Example:
//\code{.cpp}
//Procedural::TextureBuffer bufferCell(256);
//Procedural::Cell(&bufferCell).setDensity(4).setRegularity(234).process();
//Procedural::Alpha(&bufferCell).process();
//\endcode
//\dotfile texture_03.gv
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Alpha : public TextureProcessing
public class Alpha : TextureProcessing
{
private ColourValue mExtractColour = new ColourValue();
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public Alpha(TextureBuffer pBuffer)
: base(pBuffer, "Alpha") {
mExtractColour = ColourValue.White;
}
// *
// Set the colour to extract.
// \param colour New colour for extraction (default Ogre::ColourValue::White)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Alpha setExtractColour(ColourValue colour) {
//
//ORIGINAL LINE: mExtractColour = colour;
mExtractColour = (colour);
return this;
}
// *
// Set the colour to extract.
// \param red Red value of extraction colour [0.0, 1.0] \(default 1.0)
// \param green Green value of extraction colour [0.0, 1.0] \(default 1.0)
// \param blue Blue value of extraction colour [0.0, 1.0] \(default 1.0)
// \param alpha %Alpha value of extraction colour [0.0, 1.0] \(default 1.0)
//
public Alpha setExtractColour(float red, float green, float blue) {
return setExtractColour(red, green, blue, 1.0f);
}
//
//ORIGINAL LINE: Alpha& setExtractColour(Ogre::float red, Ogre::float green, Ogre::float blue, Ogre::float alpha = 1.0f)
public Alpha setExtractColour(float red, float green, float blue, float alpha) {
mExtractColour = new ColourValue(red, green, blue, alpha);
return this;
}
// *
// Run image manipulation
// \return Pointer to image buffer which has been set in the constructor.
//
public override TextureBuffer process() {
int w = (int)mBuffer.getWidth();
int h = (int)mBuffer.getHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
float r = (float)mBuffer.getPixelRedByte(x, y) * mExtractColour.r;
float g = (float)mBuffer.getPixelGreenByte(x, y) * mExtractColour.g;
float b = (float)mBuffer.getPixelBlueByte(x, y) * mExtractColour.b;
byte a = (byte)(((uint)mBuffer.getPixelAlphaByte(x, y) + (uint)(r + g + b)) >> 1);
mBuffer.setPixel(x, y, a, a, a, a);
}
}
Utils.log("Modify texture with alpha filter");
return mBuffer;
}
}
//*
//\brief Use alpha channel as an mask for an other image.
//\details <ol><li>Extract alpha channel as an opaque monochrome bitmap or</li><li>multiply alpha channel with prameter image luminance, or</li><li>use parameter image as color alpha mask.</li></ol> The luminance is not taken into account.
//
//Example:
//\code{.cpp}
//Procedural::TextureBuffer bufferGradient(256);
//Procedural::Gradient(&bufferGradient).setColours(Ogre::ColourValue::Black, Ogre::ColourValue::Red, Ogre::ColourValue::Green, Ogre::ColourValue::Blue).process();
//
//Procedural::TextureBuffer bufferCell(256);
//Procedural::Cell(&bufferCell).setDensity(4).setRegularity(234).process();
//
//Procedural::AlphaMask(&bufferGradient).setParameterImage(&bufferCell).process();
//\endcode
//\dotfile texture_04.gv
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport AlphaMask : public TextureProcessing
public class AlphaMask : TextureProcessing
{
private TextureBuffer mParam;
private bool mColourMask;
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public AlphaMask(TextureBuffer pBuffer)
: base(pBuffer, "AlphaMask") {
mColourMask = false;
mParam = null;
}
// *
// Set mode of alpha masking.
// \param colourmask If set to true parameter image will be used as alph mask (default false)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public AlphaMask setColourAlphaMask(bool colourmask) {
mColourMask = colourmask;
return this;
}
// *
// Set parameter image for masking/colouring.
// \param image Pointer to second image (default NULL)
// \note Methode 1 is used if the parameter image is set to zero. If the size of the parameter image is smaller than the base buffer the operation will be canceled without any image manipulation.
//
public AlphaMask setParameterImage(TextureBuffer image) {
mParam = image;
return this;
}
// *
// Run image manipulation
// \return Pointer to image buffer which has been set in the constructor.
//
public override TextureBuffer process() {
int w = (int)mBuffer.getWidth();
int h = (int)mBuffer.getHeight();
if (mParam != null && (mParam.getWidth() < w || mParam.getHeight() < h))
return mBuffer;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (mParam != null) {
if (mColourMask) {
ColourValue pixelA = mBuffer.getPixel(x, y);
ColourValue pixelB = mParam.getPixel(x, y);
Vector3 c1 = new Vector3(pixelA.r * 255.0f, pixelA.g * 255.0f, pixelA.b * 255.0f);
Vector3 c2 = new Vector3(pixelB.r * 255.0f, pixelB.g * 255.0f, pixelB.b * 255.0f);
float c1norm = c1.Normalise();
float c2norm = c2.Normalise();
float correctness = 0;
if (c1norm > 0.0f && c2norm > 0.0f)
correctness = c1.x * c2.x + c1.y * c2.y + c1.z * c2.z;
mBuffer.setAlpha(x, y, (byte)(pixelA.a * correctness));
}
else {
ColourValue pixel = mParam.getPixel(x, y);
float alpha = (pixel.r + pixel.g + pixel.b) / 3.0f;
mBuffer.setAlpha(x, y, mBuffer.getPixelAlphaReal(x, y) * alpha);
}
}
else {
byte a = mBuffer.getPixelAlphaByte(x, y);
mBuffer.setPixel(x, y, a, a, a, 255);
}
}
}
Utils.log("Modify texture with alphamask filter");
return mBuffer;
}
}
//*
//\brief copies a part of the input buffer towards the current buffer.
//
//Example:
//\code{.cpp}
//Procedural::TextureBuffer bufferImage(256);
//Procedural::Image(&bufferImage).setFile("red_brick.jpg").process();
//
//Procedural::TextureBuffer bufferGradient(256);
//Procedural::Gradient(&bufferGradient).setColours(Ogre::ColourValue::Black, Ogre::ColourValue::Red, Ogre::ColourValue::Green, Ogre::ColourValue::Blue).process();
//
//Procedural::Blit(&bufferImage).setInputBuffer(&bufferGradient).setInputRect(0.0f, 0.0f, 0.5f, 0.5f).setOutputRect(0.25f, 0.25f, 0.75f, 0.75f).process();
//\endcode
//\dotfile texture_31.gv
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Blit : public TextureProcessing
public class Blit : TextureProcessing
{
private TextureBuffer mInputBuffer;
private Rect mInputRect = new Rect();
private Rect mOutputRect = new Rect();
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public Blit(TextureBuffer pBuffer)
: base(pBuffer, "Blit") {
mInputBuffer = null;
mOutputRect.left = 0;
mOutputRect.top = 0;
mOutputRect.right = (int)pBuffer.getWidth();
mOutputRect.bottom = (int)pBuffer.getHeight();
}
// *
// Sets the texture buffer that must be copied towards the current texture buffer
// \param inputBuffer Pointer on image where to copy from
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Blit setInputBuffer(TextureBuffer inputBuffer) {
if (inputBuffer != null)
if (inputBuffer.getHeight() >= mBuffer.getHeight() && inputBuffer.getWidth() >= mBuffer.getWidth()) {
mInputBuffer = inputBuffer;
mInputRect.left = 0;
mInputRect.top = 0;
mInputRect.right = (int)inputBuffer.getWidth();
mInputRect.bottom = (int)inputBuffer.getHeight();
}
return this;
}
// *
// Set the full rectangle coordinates of the input buffer to copy.
// \param rect Full rectangle description (default: left=0.0, top=0.0, right=1.0, bottom=1.0)
// \param relative If this is set to true (default) the rectangle data are relative [0.0, 1.0]; else absolut [px]
//
public Blit setInputRect(RealRect rect) {
return setInputRect(rect, true);
}
//
//ORIGINAL LINE: Blit& setInputRect(RealRect rect, bool relative = true)
public Blit setInputRect(RealRect rect, bool relative) {
if (mInputBuffer == null)
return this;
if (relative) {
mInputRect.left = (int)((float)mInputBuffer.getWidth() * (float)System.Math.Min(rect.left, 1.0f));
mInputRect.top = (int)((float)mInputBuffer.getHeight() * (float)System.Math.Min(rect.top, 1.0f));
mInputRect.right = (int)((float)mInputBuffer.getWidth() * (float)System.Math.Min(rect.right, 1.0f));
mInputRect.bottom = (int)((float)mInputBuffer.getHeight() * (float)System.Math.Min(rect.bottom, 1.0f));
}
else {
mInputRect.left = (int)System.Math.Min((int)rect.left, mInputBuffer.getWidth());
mInputRect.top = (int)System.Math.Min((int)rect.top, mInputBuffer.getHeight());
mInputRect.right = (int)System.Math.Min((int)rect.right, mInputBuffer.getWidth());
mInputRect.bottom = (int)System.Math.Min((int)rect.bottom, mInputBuffer.getHeight());
}
return this;
}
// *
// Set the full rectangle coordinates of the input buffer to copy.
// \param rect Full absolute rectangle description (default: left=0, top=0, right=image width, bottom=image height)
//
public Blit setInputRect(Rect rect) {
if (mInputBuffer == null)
return this;
mInputRect.left = (int)System.Math.Min(rect.left, mInputBuffer.getWidth());
mInputRect.top = (int)System.Math.Min(rect.top, mInputBuffer.getHeight());
mInputRect.right = (int)System.Math.Min(rect.right, mInputBuffer.getWidth());
mInputRect.bottom = (int)System.Math.Min(rect.bottom, mInputBuffer.getHeight());
return this;
}
// *
// Set the full rectangle coordinates of the input buffer to copy.
// \param pos1 Vector to top left start point of the rectangle (default: x=0.0, y=0.0)
// \param pos2 Vector to bottom right end point of the rectangle (default: x=1.0, y=1.0)
// \param relative If this is set to true (default) the vector data are relative [0.0, 1.0]; else absolut [px]
//
public Blit setInputRect(Vector2 pos1, Vector2 pos2) {
return setInputRect(pos1, pos2, true);
}
//
//ORIGINAL LINE: Blit& setInputRect(Ogre::Vector2 pos1, Ogre::Vector2 pos2, bool relative = true)
public Blit setInputRect(Vector2 pos1, Vector2 pos2, bool relative) {
if (mInputBuffer == null)
return this;
if (relative) {
mInputRect.left = (int)((float)mInputBuffer.getWidth() * System.Math.Min(pos1.x, 1.0f));
mInputRect.top = (int)((float)mInputBuffer.getHeight() * System.Math.Min(pos1.y, 1.0f));
mInputRect.right = (int)((float)mInputBuffer.getWidth() * System.Math.Min(pos2.x, 1.0f));
mInputRect.bottom = (int)((float)mInputBuffer.getHeight() * System.Math.Min(pos2.y, 1.0f));
}
else {
mInputRect.left = (int)System.Math.Min((int)pos1.x, mInputBuffer.getWidth());
mInputRect.top = (int)System.Math.Min((int)pos1.y, mInputBuffer.getHeight());
mInputRect.right = (int)System.Math.Min((int)pos2.x, mInputBuffer.getWidth());
mInputRect.bottom = (int)System.Math.Min((int)pos2.y, mInputBuffer.getHeight());
}
return this;
}
// *
// Set the full rectangle coordinates of the input buffer to copy.
// \param x1 New absolute x position of rectangle start (default 0)
// \param y1 New absolute y position of rectangle start (default 0)
// \param x2 New absolute x position of rectangle end (default: image width)
// \param y2 New absolute y position of rectangle end (default: image height)
//
public Blit setInputRect(int x1, int y1, int x2, int y2) {
if (mInputBuffer == null)
return this;
mInputRect.left = (int)System.Math.Min(x1, mInputBuffer.getWidth());
mInputRect.top = (int)System.Math.Min(y1, mInputBuffer.getHeight());
mInputRect.right = (int)System.Math.Min(x2, mInputBuffer.getWidth());
mInputRect.bottom = (int)System.Math.Min(y2, mInputBuffer.getHeight());
return this;
}
// *
// Set the full rectangle coordinates of the input buffer to copy.
// \param x1 New relative x position of rectangle start [0.0, 1.0] \(default 0.0)
// \param y1 New relative y position of rectangle start [0.0, 1.0] \(default 0.0)
// \param x2 New relative x position of rectangle end [0.0, 1.0] \(default 1.0)
// \param y2 New relative y position of rectangle end [0.0, 1.0] \(default 1.0)
//
public Blit setInputRect(float x1, float y1, float x2, float y2) {
if (mInputBuffer == null)
return this;
mInputRect.left = (int)((float)mInputBuffer.getWidth() * System.Math.Min(x1, 1.0f));
mInputRect.top = (int)((float)mInputBuffer.getHeight() * System.Math.Min(y1, 1.0f));
mInputRect.right = (int)((float)mInputBuffer.getWidth() * System.Math.Min(x2, 1.0f));
mInputRect.bottom = (int)((float)mInputBuffer.getHeight() * System.Math.Min(y2, 1.0f));
return this;
}
// *
// Set the full rectangle coordinates of the output buffer where the input is copied to.
// \param rect Full rectangle description (default: left=0.0, top=0.0, right=1.0, bottom=1.0)
// \param relative If this is set to true (default) the rectangle data are relative [0.0, 1.0]; else absolut [px]
//
public Blit setOutputRect(RealRect rect) {
return setOutputRect(rect, true);
}
//
//ORIGINAL LINE: Blit& setOutputRect(RealRect rect, bool relative = true)
public Blit setOutputRect(RealRect rect, bool relative) {
if (relative) {
mOutputRect.left = (int)((float)mBuffer.getWidth() * System.Math.Min(rect.left, 1.0f));
mOutputRect.top = (int)((float)mBuffer.getHeight() * System.Math.Min(rect.top, 1.0f));
mOutputRect.right = (int)((float)mBuffer.getWidth() * System.Math.Min(rect.right, 1.0f));
mOutputRect.bottom = (int)((float)mBuffer.getHeight() * System.Math.Min(rect.bottom, 1.0f));
}
else {
mOutputRect.left = (int)System.Math.Min((int)rect.left, mBuffer.getWidth());
mOutputRect.top = (int)System.Math.Min((int)rect.top, mBuffer.getHeight());
mOutputRect.right = (int)System.Math.Min((int)rect.right, mBuffer.getWidth());
mOutputRect.bottom = (int)System.Math.Min((int)rect.bottom, mBuffer.getHeight());
}
return this;
}
// *
// Set the full rectangle coordinates of the output buffer where the input is copied to.
// \param rect Full absolute rectangle description (default: left=0, top=0, right=image width, bottom=image height)
//
public Blit setOutputRect(Rect rect) {
mOutputRect.left = (int)System.Math.Min(rect.left, mBuffer.getWidth());
mOutputRect.top = (int)System.Math.Min(rect.top, mBuffer.getHeight());
mOutputRect.right = (int)System.Math.Min(rect.right, mBuffer.getWidth());
mOutputRect.bottom = (int)System.Math.Min(rect.bottom, mBuffer.getHeight());
return this;
}
// *
// Set the full rectangle coordinates of the output buffer where the input is copied to.
// \param pos1 Vector to top left start point of the rectangle (default: x=0.0, y=0.0)
// \param pos2 Vector to bottom right end point of the rectangle (default: x=1.0, y=1.0)
// \param relative If this is set to true (default) the vector data are relative [0.0, 1.0]; else absolut [px]
//
public Blit setOutputRect(Vector2 pos1, Vector2 pos2) {
return setOutputRect(pos1, pos2, true);
}
//
//ORIGINAL LINE: Blit& setOutputRect(Ogre::Vector2 pos1, Ogre::Vector2 pos2, bool relative = true)
public Blit setOutputRect(Vector2 pos1, Vector2 pos2, bool relative) {
if (relative) {
mOutputRect.left = (int)((float)mBuffer.getWidth() * System.Math.Min(pos1.x, 1.0f));
mOutputRect.top = (int)((float)mBuffer.getHeight() * System.Math.Min(pos1.y, 1.0f));
mOutputRect.right = (int)((float)mBuffer.getWidth() * System.Math.Min(pos2.x, 1.0f));
mOutputRect.bottom = (int)((float)mBuffer.getHeight() * System.Math.Min(pos2.y, 1.0f));
}
else {
mOutputRect.left = (int)System.Math.Min((int)pos1.x, mBuffer.getWidth());
mOutputRect.top = (int)System.Math.Min((int)pos1.y, mBuffer.getHeight());
mOutputRect.right = (int)System.Math.Min((int)pos2.x, mBuffer.getWidth());
mOutputRect.bottom = (int)System.Math.Min((int)pos2.y, mBuffer.getHeight());
}
return this;
}
// *
// Set the full rectangle coordinates of the output buffer where the input is copied to.
// \param x1 New absolute x position of rectangle start (default 0)
// \param y1 New absolute y position of rectangle start (default 0)
// \param x2 New absolute x position of rectangle end (default: image width)
// \param y2 New absolute y position of rectangle end (default: image height)
//
public Blit setOutputRect(int x1, int y1, int x2, int y2) {
mOutputRect.left = (int)System.Math.Min(x1, mBuffer.getWidth());
mOutputRect.top = (int)System.Math.Min(y1, mBuffer.getHeight());
mOutputRect.right = (int)System.Math.Min(x2, mBuffer.getWidth());
mOutputRect.bottom = (int)System.Math.Min(y2, mBuffer.getHeight());
return this;
}
// *
// Set the full rectangle coordinates of the output buffer where the input is copied to.
// \param x1 New relative x position of rectangle start [0.0, 1.0] \(default 0.0)
// \param y1 New relative y position of rectangle start [0.0, 1.0] \(default 0.0)
// \param x2 New relative x position of rectangle end [0.0, 1.0] \(default 1.0)
// \param y2 New relative y position of rectangle end [0.0, 1.0] \(default 1.0)
//
public Blit setOutputRect(float x1, float y1, float x2, float y2) {
mOutputRect.left = (int)((float)mBuffer.getWidth() * System.Math.Min(x1, 1.0f));
mOutputRect.top = (int)((float)mBuffer.getHeight() * System.Math.Min(y1, 1.0f));
mOutputRect.right = (int)((float)mBuffer.getWidth() * System.Math.Min(x2, 1.0f));
mOutputRect.bottom = (int)((float)mBuffer.getHeight() * System.Math.Min(y2, 1.0f));
return this;
}
// *
// Run image manipulation
// \return Pointer to image buffer which has been set in the constructor.
//
public override TextureBuffer process() {
if (mInputBuffer == null)
return mBuffer;
for (int y = mOutputRect.top; y < mOutputRect.bottom; y++) {
for (int x = mOutputRect.left; x < mOutputRect.right; x++) {
int x0 = (int)((float)(x - mOutputRect.left) / (float)(mOutputRect.Width) * (float)(mInputRect.Width) + (float)(mInputRect.left));
int y0 = (int)((float)(y - mOutputRect.top) / (float)(mOutputRect.Height) * (float)(mInputRect.Height) + (float)(mInputRect.top));
mBuffer.setPixel(x, y, mInputBuffer.getPixel(x0, y0));
}
}
Utils.log("Modify texture with blit filter");
return mBuffer;
}
}
//*
//\brief Reduce sharpness on input image.
//\details Blurs the input image by a specified algorithm.
//
//Examples:
//\b BLUR_MEAN (default)
//\code{.cpp}
//Procedural::TextureBuffer bufferImage(256);
//Procedural::Image(&bufferImage).setFile("red_brick.jpg").process();
//Procedural::Blur(&bufferImage).setType(Procedural::Blur::BLUR_MEAN).process();
//\endcode
//\dotfile texture_05a.gv
//
//\b BLUR_GAUSSIAN
//\code{.cpp}
//Procedural::TextureBuffer bufferImage(256);
//Procedural::Image(&bufferImage).setFile("red_brick.jpg").process();
//Procedural::Blur(&bufferImage).setType(Procedural::Blur::BLUR_GAUSSIAN).process();
//\endcode
//\dotfile texture_05b.gv
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Blur : public TextureProcessing
public class Blur : TextureProcessing
{
//! List of algorithms to blur
public enum BLUR_TYPE : int
{
BLUR_BOX, //!< Use simplified block filter to blur
BLUR_MEAN, //!< Use mean filter to blur
BLUR_GAUSSIAN //!< Use gaussian filter to blur
}
private byte mSize = 0;
private byte mSigma = 0;
private BLUR_TYPE mType;
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public Blur(TextureBuffer pBuffer)
: base(pBuffer, "Blur") {
mSize = 5;
mSigma = 92;
mType = (int)BLUR_TYPE.BLUR_BOX;
}
// *
// Set the gaussian block size.
// \param size New block size for gaussian blur filter [3, 255] (default 5)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Blur setSize(byte size) {
mSize = size;
if (mSize < 3)
mSize = 3;
if ((mSize % 2) == 0)
mSize++;
return this;
}
// *
// Set sigma constant for gaussian filter.
// \param sigma New sigma constant for gaussian blur filter [0, 255] (default 92)
//
public Blur setSigma(byte sigma) {
mSigma = sigma;
return this;
}
// *
// Set the algorithm to blur.
// \param type New algorithm to blur (default BLUR_BOX)
//
public Blur setType(BLUR_TYPE type) {
mType = type;
return this;
}
// *
// Run image manipulation
// \return Pointer to image buffer which has been set in the constructor.
//
public override TextureBuffer process() {
float[] blurKernel = { 1, 2, 3, 2, 1, 2, 4, 5, 4, 2, 3, 5, 6, 5, 3, 2, 4, 5, 4, 2, 1, 2, 3, 2, 1 };
Convolution filter = new Convolution(mBuffer);
switch (mType) {
default:
//C++ TO C# CONVERTER TODO TASK: C# does not allow fall-through from a non-empty 'case':
case BLUR_TYPE.BLUR_BOX:
filter.setKernel(5, blurKernel);
break;
case BLUR_TYPE.BLUR_MEAN:
filter.setKernel(new Matrix3(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f)).calculateDivisor();
break;
case BLUR_TYPE.BLUR_GAUSSIAN:
float fSigma = 0.5f + ((3.0f - 0.5f) / 255.0f) * (float)mSigma;
int r = (int)mSize / 2;
double min = System.Math.Exp((float)(2 * r * r) / (-2.0f * fSigma * fSigma)) / (Math.TWO_PI * fSigma * fSigma);
int[] kernel = new int[mSize * mSize];
int divisor = 0;
int y = -r;
int x = -r;
for (int i = 0; i < mSize; i++) {
for (int j = 0; j < mSize; j++) {
kernel[i * mSize + j] = (int)((System.Math.Exp((float)(x * x + y * y) / (-2.0f * fSigma * fSigma)) / (Math.TWO_PI * fSigma * fSigma)) / min);
divisor += kernel[i * mSize + j];
x++;
}
y++;
}
filter.setKernel(mSize, kernel).setDivisor((float)divisor);
kernel = null;
break;
}
filter.setIncludeAlphaChannel(true).process();
Utils.log("Modify texture with blur filter : " + (mType).ToString());
return mBuffer;
}
}
//*
//\brief Extract one channel or create gray image.
//\details Reduce given image to a given channel or calculate gray image of it
//
//Examples:
//\b SELECT_BLUE
//\code{.cpp}
//Procedural::TextureBuffer bufferImage(256);
//Procedural::Image(&bufferImage).setFile("red_brick.jpg").process();
//Procedural::Channel(&bufferImage).setSelection(Procedural::Channel::SELECT_BLUE).process();
//\endcode
//\dotfile texture_06a.gv
//
//\b SELECT_GRAY
//\code{.cpp}
//Procedural::TextureBuffer bufferImage(256);
//Procedural::Image(&bufferImage).setFile("red_brick.jpg").process();
//Procedural::Channel(&bufferImage).setSelection(Procedural::Channel::SELECT_GRAY).process();
//\endcode
//\dotfile texture_06b.gv
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Channel : public TextureProcessing
public class Channel : TextureProcessing
{
public enum CANNEL_SELECTION : int
{
SELECT_RED,
SELECT_GREEN,
SELECT_BLUE,
SELECT_ALPHA,
SELECT_GRAY
}
private CANNEL_SELECTION mSelection;
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public Channel(TextureBuffer pBuffer)
: base(pBuffer, "Channel") {
mSelection = CANNEL_SELECTION.SELECT_GRAY;
}
// *
// Set selection.
// \param selection Mode which channel should selected (default SELECT_GRAY)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Channel setSelection(CANNEL_SELECTION selection) {
mSelection = selection;
return this;
}
// *
// Run image manipulation
// \return Pointer to image buffer which has been set in the constructor.
//
public override TextureBuffer process() {
for (int y = 0; y < mBuffer.getHeight(); y++) {
for (int x = 0; x < mBuffer.getWidth(); x++) {
if (mSelection == CANNEL_SELECTION.SELECT_GRAY) {
ColourValue pixel = mBuffer.getPixel(x, y);
float gray = (pixel.r + pixel.g + pixel.b) / 3.0f;
mBuffer.setPixel(x, y, gray, gray, gray, pixel.a);
}
else {
if (mSelection != CANNEL_SELECTION.SELECT_RED)
mBuffer.setRed(x, y, 0.0f);
if (mSelection != CANNEL_SELECTION.SELECT_GREEN)
mBuffer.setGreen(x, y, 0.0f);
if (mSelection != CANNEL_SELECTION.SELECT_BLUE)
mBuffer.setBlue(x, y, 0.0f);
if (mSelection != CANNEL_SELECTION.SELECT_BLUE)
mBuffer.setBlue(x, y, 0.0f);
}
}
}
Utils.log("Modify texture with channel filter : " + (mSelection).ToString());
return mBuffer;
}
}
//*
//\brief Draw a circle.
//\details Draw a filled circle on top of previous content.
//
//Example:
//\code{.cpp}
//Procedural::TextureBuffer bufferSolid(256);
//Procedural::Solid(&bufferSolid).setColour(Ogre::ColourValue(0.0f, 0.5f, 1.0f, 1.0f)).process();
//Procedural::CircleTexture(&bufferSolid).setColour(Ogre::ColourValue::Red).setRadius(0.3f).process();
//\endcode
//\dotfile texture_32.gv
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport CircleTexture : public TextureProcessing
public class CircleTexture : TextureProcessing
{
private ColourValue mColour = new ColourValue();
private int mX = new int();
private int mY = new int();
private int mRadius = new int();
// *
// Default constructor.
// \param pBuffer Image buffer where to modify the image.
//
public CircleTexture(TextureBuffer pBuffer)
: base(pBuffer, "CircleTexture") {
mColour = ColourValue.White;
mRadius = (int)System.Math.Min(pBuffer.getWidth(), pBuffer.getHeight()) / 2;
mX = mRadius;
mY = mRadius;
}
// *
// Set the fill colour of the circle.
// \param colour New colour for processing (default Ogre::ColourValue::White)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public CircleTexture setColour(ColourValue colour) {
//
//ORIGINAL LINE: mColour = colour;
mColour = (colour);
return this;
}
// *
// Set the fill colour of the circle.
// \param red Red value of the fill colour [0.0, 1.0] \(default 1.0)
// \param green Green value of the fill colour [0.0, 1.0] \(default 1.0)
// \param blue Blue value of the fill colour [0.0, 1.0] \(default 1.0)