-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransform.cpp
More file actions
2078 lines (1698 loc) · 72.1 KB
/
transform.cpp
File metadata and controls
2078 lines (1698 loc) · 72.1 KB
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <complex>
#include <math.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "Transform.h"
#include "Util/Segment.h"
//#include "FFT/kiss_fft.h"
using namespace transform;
using namespace std;
//----------------------------------------------------------------------------------------------------------------------//
// Constructor Functions: //
//----------------------------------------------------------------------------------------------------------------------//
Vec2i::Vec2i(){
x = 0; y = 0;
}
inline SDL_Rect CreateRect(int x, int y, int w, int h){
SDL_Rect temp;
temp.x = x; temp.y = y; temp.w = w; temp.h = h;
return temp;
}
inline SDL_Rect CreateRect(double x, double y, double w, double h){
SDL_Rect temp;
temp.x = int(x); temp.y = int(y); temp.w = int(w); temp.h = int(h);
return temp;
}
inline SDL_Rect CreateRect(float x, float y, float w, float h){
SDL_Rect temp;
temp.x = int(x); temp.y = int(y); temp.w = int(w); temp.h = int(h);
return temp;
}
inline SDL_Color CreateColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a){
SDL_Color temp;
temp.r = r; temp.g = g; temp.b = b; temp.a = a;
return temp;
}
inline SDL_Color CreateColor(int r, int g, int b, int a){
SDL_Color temp;
temp.r = Uint8(r); temp.g = Uint8(g); temp.b = Uint8(b); temp.a = Uint8(a);
return temp;
}
inline SDL_Color CreateColor(double r, double g, double b, double a){
SDL_Color temp;
temp.r = Uint8(r); temp.g = Uint8(g); temp.b = Uint8(b); temp.a = Uint8(a);
return temp;
}
inline SDL_Color CreateColor(float r, float g, float b, float a){
SDL_Color temp;
temp.r = Uint8(r); temp.g = Uint8(g); temp.b = Uint8(b); temp.a = Uint8(a);
return temp;
}
inline Vec2i CreateVec2i(int x, int y){
Vec2i temp(x, y); return temp;
}
inline Vec2i CreateVec2i(double x, double y){
int iX = int(x); int iY = int(y);
Vec2i temp(iX, iY); return temp;
}
inline Vec2i CreateVec2i(float x, float y){
int iX = int(x); int iY = int(y);
Vec2i temp(iX, iY); return temp;
}
inline Vec2d CreateVec2d(double x, double y){
Vec2d temp(x, y); return temp;
}
inline Vec2d CreateVec2d(int x, int y){
double dX = double(x); double dY = double(y);
Vec2d temp(dX, dY); return temp;
}
inline Vec2d CreateVec2d(float x, float y){
double dX = double(x); double dY = double(y);
Vec2d temp(dX, dY); return temp;
}
//----------------------------------------------------------------------------------------------------------------------//
// Assist Functions: //
//----------------------------------------------------------------------------------------------------------------------//
Vec2i getCenter(SDL_Surface* surface){
Vec2i temp(surface->w / 2, surface->h / 2);
return temp;
}
SDL_Color getColor(flag id){
//Source:
// RGB values borrowed from: http://www.tayloredmktg.com/rgb/
switch (id){
case COLOR_WHITE:
return CreateColor(255,255,255,255);
case COLOR_GRAY:
return CreateColor(127, 127, 127, 255);
case COLOR_BLACK:
return CreateColor(0, 0, 0, 255);
case COLOR_RED:
return CreateColor(255, 0, 0, 255);
case COLOR_ORANGE:
return CreateColor(255, 165, 0, 255);
case COLOR_YELLOW:
return CreateColor(255,255,0,255);
case COLOR_GREEN:
return CreateColor(0,255,0,255);
case COLOR_BLUE:
return CreateColor(0, 0, 255, 255);
case COLOR_INDIGO:
return CreateColor(147, 112, 219, 255);
case COLOR_PURPLE:
return CreateColor(160, 32, 240, 255);
case COLOR_SALMON:
return CreateColor(250, 128, 114, 255);
case COLOR_BROWN:
return CreateColor(139, 69, 19, 255);
case COLOR_ORANGE_RED:
return CreateColor(255,69,0,255);
case COLOR_DARK_ORANGE:
return CreateColor(255, 140, 0, 255);
case COLOR_BEIGE:
return CreateColor(245,245,220,255);
case COLOR_TAN:
return CreateColor(210, 180, 140, 255);
default:
return CreateColor(255, 255, 255, 255);
}
}
SDL_Color getTemperatureColor(double kelvin){
//Code translated from http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
//Credit to Tanner Helland for conversion math
if (kelvin < 1000) kelvin = 1000; else if (kelvin > 40000) kelvin = 40000;
kelvin /= 100;
double r, g, b, calc;
SDL_Color out;
//Red
if (kelvin <= 66)
r = 255;
else{
//'Note: the R-squared value for this approximation is .988
calc = kelvin - 60;
calc = 329.698727446 * pow(calc, -0.1332047592);
r = calc;
if (r < 0) r = 0; else if (r > 255) r = 255;
}
//Green
if (kelvin <= 66){
//'Note: the R-squared value for this approximation is .996
calc = kelvin;
calc = (99.4708025861 * log(calc)) - 161.1195681661;
g = calc;
if (g < 0) g = 0; else if (g > 255) g = 255;
}
else{
//'Note: the R-squared value for this approximation is .987
calc = kelvin - 60;
calc = 288.1221695283 * pow(calc, -0.0755148492);
g = calc;
if (g < 0) g = 0; else if (g > 255) g = 255;
}
//Blue
if (kelvin >= 66)
b = 255;
else if (kelvin <= 19)
b = 0;
else{
//'Note: the R-squared value for this approximation is .998
calc = kelvin - 10;
calc = 138.5177312231 * log(calc) - 305.0447927307;
b = calc;
if (b < 0) b = 0; else if (b > 255) b = 255;
}
out = CreateColor(Uint8(r), Uint8(g), Uint8(b), Uint8(255));
//out = correctGamma(out, 0.454545);
return out;
}
SDL_Color grabColor(SDL_Surface* surface, int x, int y){
Uint32 color = GetPixel(surface, x, y); SDL_Color rgba;
SDL_GetRGBA(color, surface->format, &rgba.r, &rgba.g, &rgba.b, &rgba.a);
return rgba;
}
SDL_Color correctGamma(SDL_Color color, double amount){
double correction = 1 / amount;
double r = 255 * pow(double(color.r) / 255, correction);
double g = 255 * pow(double(color.g) / 255, correction);
double b = 255 * pow(double(color.b) / 255, correction);
if (r < 0) r = 0; else if (r > 255) r = 255;
if (g < 0) g = 0; else if (g > 255) g = 255;
if (b < 0) b = 0; else if (b > 255) b = 255;
return CreateColor(Uint8(r), Uint8(g), Uint8(b), color.a);
}
int getColors(SDL_Surface* surface){
vector<SDL_Color> colors;
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 color = GetPixel(surface, x, y); SDL_Color rgba;
SDL_GetRGBA(color, surface->format, &rgba.r, &rgba.g, &rgba.b, &rgba.a);
bool found = false;
for (int a = 0; a < signed(colors.size()); a++){
if (rgba.r == colors[a].r && rgba.g == colors[a].g && rgba.b == colors[a].b)
found = true;
}
if (found != true)
colors.push_back(rgba);
}
}
return colors.size();
}
bool hasPoint(SDL_Surface* surface, Vec2i point){
if (point.x >= 0 && point.x < surface->w && point.y >= 0 && point.y < surface->h)
return true;
else
return false;
}
int findMax(vector<int> vec){
int max = 0;
for (int a = 0; a < signed(vec.size()); a++){
if (max < vec[a])
max = vec[a];
}
return max;
}
//----------------------------------------------------------------------------------------------------------------------//
// Manipulation Functions: //
//----------------------------------------------------------------------------------------------------------------------//
Uint32 GetPixel(SDL_Surface* surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch (bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void SetPixel(SDL_Surface* surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch (bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
}
else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
SDL_Surface* EmptySurface(SDL_Surface* surface){
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface* temp = SDL_CreateRGBSurface(NULL, surface->w, surface->h, surface->format->BitsPerPixel, rmask, gmask, bmask, amask);
return temp;
}
SDL_Surface* EmptySurface(SDL_Surface* surface, int wid, int hei){
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface* temp = SDL_CreateRGBSurface(NULL, wid, hei, surface->format->BitsPerPixel, rmask, gmask, bmask, amask);
return temp;
}
SDL_Surface* EmptySurface(int wid, int hei){
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface* temp = SDL_CreateRGBSurface(NULL, wid, hei, 32, rmask, gmask, bmask, amask);
return temp;
}
SDL_Surface* CopySurface(SDL_Surface* surface){
SDL_Surface* temp = EmptySurface(surface);
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
SetPixel(temp, x, y, col);
}
}
return temp;
}
void CopyRegion(SDL_Surface* surfaceA, SDL_Surface* surfaceB, SDL_Rect rectA, SDL_Rect rectB){
}
SDL_Surface* ScaleSurface(SDL_Surface* surface, double scale){
SDL_Surface* temp = EmptySurface(surface, int(surface->w*scale), int(surface->h*scale));
for (int y = 0; y < temp->h; y++){
for (int x = 0; x < temp->w; x++){
SetPixel(temp, x, y, GetPixel(surface, int(x / scale), int(y / scale)));
}
}
return temp;
}
SDL_Surface* ScaleSurface(SDL_Surface* surface, double scaleX, double scaleY){
SDL_Surface* temp = EmptySurface(surface, int(surface->w*scaleX), int(surface->h*scaleY));
for (int y = 0; y < temp->h; y++){
for (int x = 0; x < temp->w; x++){
SetPixel(temp, x, y, GetPixel(surface, int(x / scaleX), int(y / scaleY)));
}
}
return temp;
}
SDL_Surface* ResizeSurface(SDL_Surface* surface, int wid, int hei){
SDL_Surface* temp = EmptySurface(surface, wid, hei);
double dw = double(wid)/surface->w;
double dh = double(hei)/surface->h;
for (int y = 0; y < temp->h; y++){
for (int x = 0; x < temp->w; x++){
SetPixel(temp, x, y, GetPixel(surface,int(x/dw),int(y/dh)));
}
}
return temp;
}
SDL_Surface* ScaleRegion(SDL_Surface* surface, SDL_Rect rect){
//Note: For some really wild behavior, change the step scalar ( x * dw ) & (y * dh ) to division rather than multiplication.
SDL_Surface* temp = EmptySurface(surface);
double dw = double(rect.w) / surface->w;
double dh = double(rect.h) / surface->h;
for (int y = 0; y < temp->h; y++){
for (int x = 0; x < temp->w; x++){
SetPixel(temp, x, y, GetPixel(surface, int(rect.x + ( x * dw)), int(rect.y + ( y * dh))));
}
}
return temp;
}
//----------------------------------------------------------------------------------------------------------------------//
// Kernel Functions: //
//----------------------------------------------------------------------------------------------------------------------//
void Kernel3x3::Normalize(){
double sum = 0;
//Find sum of all values within matrix
for (int col = 0; col < 3; col++){
for (int row = 0; row < 3; row++){
sum += val[col][row];
}
}
//Divide each value by the sum
for (int col = 0; col < 3; col++){
for (int row = 0; row < 3; row++){
val[col][row] = val[col][row] / sum;
}
}
}
void Kernel3x3::Scale(double amount){
for (int col = 0; col < 3; col++){
for (int row = 0; row < 3; row++){
val[col][row] *= amount;
}
}
}
void ApplyKernel(SDL_Surface* srcSurface, SDL_Surface* dstSurface, Kernel3x3 kernel, flag edge){
for (int y = 0; y < srcSurface->h; y++){
for (int x = 0; x < srcSurface->w; x++){
//Create temporary variables
UMatrix3x3 r, g, b; //Color matrix (stores initial color values)
DMatrix3x3 nR, nG, nB; //New values after kernel multiplication
double addR, addG, addB; //Sums of all values after kernel operation
addR = 0; addG = 0; addB = 0;
//Fill matrices with image color values
for (int col = 0; col < 3; col++){
for (int row = 0; row < 3; row++){
//Store color values
int locX = x - 1 + row;
int locY = y - 1 + col;
Uint32 color;
//Check if position is an edge
if (locX < 0 || locX > srcSurface->w-1 || locY < 0 || locY > srcSurface->h-1){
switch (edge){
case EDGE_CROP_WHITE:
color = SDL_MapRGB(srcSurface->format, 255, 255, 255);
SDL_GetRGB(color, srcSurface->format, &r.val[col][row], &g.val[col][row], &b.val[col][row]);
break;
case EDGE_CROP_BLACK:
color = SDL_MapRGB(srcSurface->format, 0, 0, 0);
SDL_GetRGB(color, srcSurface->format, &r.val[col][row], &g.val[col][row], &b.val[col][row]);
break;
case EDGE_EXTEND:
if (locX < 0) locX = 0;
else if (locX > srcSurface->w-1) locX = srcSurface->w-1;
else if (locY < 0) locY = 0;
else if (locY > srcSurface->h-1) locY = srcSurface->h-1;
color = GetPixel(srcSurface, locX, locY);
SDL_GetRGB(color, srcSurface->format, &r.val[col][row], &g.val[col][row], &b.val[col][row]);
break;
case EDGE_WRAP:
if (locX < 0) locX = srcSurface->w-1;
else if (locX > srcSurface->w-1) locX = 0;
else if (locY < 0) locY = srcSurface->h-1;
else if (locY > srcSurface->h-1) locY = 0;
color = GetPixel(srcSurface, locX, locY);
SDL_GetRGB(color, srcSurface->format, &r.val[col][row], &g.val[col][row], &b.val[col][row]);
break;
}
}
else{
color = GetPixel(srcSurface, locX, locY);
SDL_GetRGB(color, srcSurface->format, &r.val[col][row], &g.val[col][row], &b.val[col][row]);
}
//Multiply color values by normalized kernel
nR.val[col][row] = r.val[col][row] * kernel.val[col][row];
nG.val[col][row] = g.val[col][row] * kernel.val[col][row];
nB.val[col][row] = b.val[col][row] * kernel.val[col][row];
//Add sums of new values
addR = addR + nR.val[col][row];
addG = addG + nG.val[col][row];
addB = addB + nB.val[col][row];
}
}
//Wrap calculated values between 0 and 255
if (addR > 255) addR = 255; else if (addR < 0) addR = 0;
if (addG > 255) addG = 255; else if (addG < 0) addG = 0;
if (addB > 255) addB = 255; else if (addB < 0) addB = 0;
Uint32 newCol = SDL_MapRGB(dstSurface->format, Uint8(addR), Uint8(addG), Uint8(addB));
SetPixel(dstSurface, x, y, newCol);
}
}
}
SDL_Surface* FilterSurface(SDL_Surface* surface, flag method, flag edge){
Kernel3x3 kernel;
SDL_Surface* temp = EmptySurface(surface);
switch (method){
case FILTER_BLUR:
kernel.Blur();
ApplyKernel(surface, temp, kernel, edge);
break;
case FILTER_SHARPEN:
kernel.Sharpen();
ApplyKernel(surface, temp, kernel, edge);
break;
case FILTER_EDGE:
kernel.Edge();
ApplyKernel(surface, temp, kernel, edge);
break;
case FILTER_EMBOSS:
kernel.Emboss();
ApplyKernel(surface, temp, kernel, edge);
break;
case FILTER_NONE:
break;
}
return temp;
}
SDL_Surface* FilterBlur(SDL_Surface* surface, int passes, flag edge){
SDL_Surface* post = EmptySurface(surface);
for (int a = 0; a < passes; a++){
post = FilterSurface(surface, FILTER_BLUR, edge);
surface = CopySurface(post);
}
return surface;
}
SDL_Surface* FilterOffset(SDL_Surface* surface, int x, int y){
SDL_Surface* temp = EmptySurface(surface);
for (int cy = 0; cy < surface->h; cy++){
for (int cx = 0; cx < surface->w; cx++){
int targetx, targety;
if (cx + x < 0)
targetx = surface->w - (x + cx);
else if (cy + x > surface->w - 1)
targetx = cx + x - surface->w;
else
targetx = cx + x;
if (cy + y < 0)
targety = surface->h - (y + cy);
else if (cy + y > surface->h - 1)
targety = cy + y - surface->h;
else
targety = cy + y;
Uint32 color = GetPixel(surface, targetx, targety);
SetPixel(temp, cx, cy, color);
}
}
return temp;
}
SDL_Surface* FlipHorizontal(SDL_Surface* surface){
return FilterOffset(surface, -(surface->w-1), 0);
}
SDL_Surface* FlipVertical(SDL_Surface* surface){
return FilterOffset(surface, 0, -(surface->h-1));
}
//----------------------------------------------------------------------------------------------------------------------//
// Image Adjustment Functions: //
//----------------------------------------------------------------------------------------------------------------------//
void Grayscale(SDL_Surface* surface, flag method){
switch (method){
case GRAY_AVERAGE:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; int val;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
val = (int(r) + int(g) + int(b)) / 3;
Uint32 newCol = SDL_MapRGBA(surface->format, Uint8(val), Uint8(val), Uint8(val), a);
SetPixel(surface, x, y, newCol);
}
}
break;
case GRAY_LUMINANCE:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; double val;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
val = (int(r)*0.72) + (int(g)*0.21) + (int(b)*0.07);
Uint32 newCol = SDL_MapRGBA(surface->format, Uint8(val), Uint8(val), Uint8(val), a);
SetPixel(surface, x, y, newCol);
}
}
break;
case GRAY_LIGHTNESS:
//(min(R, G, B) + max(R, G, B)) / 2
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; double val;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
val = (min(int(r), min(int(g), int(b))) + max(int(r), max(int(g), int(b)))) / 2;
Uint32 newCol = SDL_MapRGBA(surface->format, Uint8(val), Uint8(val), Uint8(val), a);
SetPixel(surface, x, y, newCol);
}
}
break;
case GRAY_RED:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; int val;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
val = int(r);
Uint32 newCol = SDL_MapRGBA(surface->format, Uint8(val), Uint8(val), Uint8(val), a);
SetPixel(surface, x, y, newCol);
}
}
break;
case GRAY_GREEN:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; int val;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
val = int(g);
Uint32 newCol = SDL_MapRGBA(surface->format, Uint8(val), Uint8(val), Uint8(val), a);
SetPixel(surface, x, y, newCol);
}
}
break;
case GRAY_BLUE:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; int val;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
val = int(b);
Uint32 newCol = SDL_MapRGBA(surface->format, Uint8(val), Uint8(val), Uint8(val), a);
SetPixel(surface, x, y, newCol);
}
}
break;
}
}
void Desaturate(SDL_Surface* surface, double value){
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b, a; int average, dR, dG, dB;
SDL_GetRGBA(col, surface->format, &r, &g, &b, &a);
//Calculate average at pixel, as well as difference between calculated average and the original value per color channel
average = (int(r) + int(g) + int(b)) / 3;
dR = average - int(r); dG = average - int(g); dB = average - int(b);
r = Uint8(r + (value*dR)); g = Uint8(g + (value*dG)); b = Uint8(b + (value*dB));
if (r < 0) r = 0; else if (r > 255) r = 255;
if (g < 0) g = 0; else if (g > 255) g = 255;
if (b < 0) b = 0; else if (b > 255) b = 255;
Uint32 newCol = SDL_MapRGBA(surface->format, r, g, b, a);
SetPixel(surface, x, y, newCol);
}
}
}
void Threshold(SDL_Surface* surface, flag channel, int max){
switch (channel){
case CHANNEL_RED:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b; int val;
SDL_GetRGB(col, surface->format, &r, &g, &b);
if (int(r) < max) val = 0; else if (int(r) >= max) val = 255;
Uint32 newCol = SDL_MapRGB(surface->format, Uint8(val), Uint8(val), Uint8(val));
SetPixel(surface, x, y, newCol);
}
}
break;
case CHANNEL_GREEN:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b; int val;
SDL_GetRGB(col, surface->format, &r, &g, &b);
if (int(g) < max) val = 0; else if (int(g) >= max) val = 255;
Uint32 newCol = SDL_MapRGB(surface->format, Uint8(val), Uint8(val), Uint8(val));
SetPixel(surface, x, y, newCol);
}
}
break;
case CHANNEL_BLUE:
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y);
Uint8 r, g, b; int val;
SDL_GetRGB(col, surface->format, &r, &g, &b);
if (int(b) < max) val = 0; else if (int(b) >= max) val = 255;
Uint32 newCol = SDL_MapRGB(surface->format, Uint8(val), Uint8(val), Uint8(val));
SetPixel(surface, x, y, newCol);
}
}
break;
}
}
void ReplaceColor(SDL_Surface* surface, SDL_Color srcColor, SDL_Color dstColor, int threshold){
for (int y = 0; y < surface->h; y++){
for (int x = 0; x < surface->w; x++){
Uint32 color = GetPixel(surface, x, y); SDL_Color values;
SDL_GetRGBA(color, surface->format, &values.r, &values.g, &values.b, &values.a);
if (values.r > srcColor.r - threshold && values.r < srcColor.r + threshold &&
values.g > srcColor.g - threshold && values.g < srcColor.g + threshold &&
values.b > srcColor.b - threshold && values.b < srcColor.b + threshold)
SetPixel(surface, x, y, SDL_MapRGBA(surface->format, dstColor.r, dstColor.g, dstColor.b, dstColor.a));
}
}
}
void Interlace(SDL_Surface* surface, int amount){
for (int y = 0; y < surface->h/amount; y++){
for (int x = 0; x < surface->w/amount; x++){
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
if(hasPoint(surface,CreateVec2i((x*amount)+amount/2,(y*amount)+amount/2)) != false )
SetPixel(surface, (x*amount)+amount/2, (y*amount)+amount/2, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
}
}
}
void Interlace(SDL_Surface* surface, int amount, SDL_Color color){
for (int y = 0; y < surface->h / amount; y++){
for (int x = 0; x < surface->w / amount; x++){
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount/2, (y*amount) + amount/2, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
}
}
}
void Interlace(SDL_Surface* surface, int amount, SDL_Color color, flag style){
switch (style){
case IL_DEFAULT: //IL_DEFAULT and IL_WEAVE perform same operation, this case layout is known as 'falling through' (notice no break statement under IL_DEFAULT case)
case IL_WEAVE:
for (int y = 0; y < surface->h / amount; y++){
for (int x = 0; x < surface->w / amount; x++){
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount / 2, (y*amount) + amount / 2, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
}
}
break;
case IL_HORIZONTAL:
for (int y = 0; y < surface->h / amount; y++){
for (int x = 0; x < surface->w; x++){
SetPixel(surface, x, y*amount + amount / 2, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
}
}
break;
case IL_VERTICAL:
for (int x = 0; x < surface->w / amount; x++){
for (int y = 0; y < surface->h; y++){
SetPixel(surface, x*amount + amount/2, y, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
}
}
break;
case IL_DOTTED:
for (int y = 0; y < surface->h / amount; y++){
for (int x = 0; x < surface->w / amount; x++){
SetPixel(surface, x*amount + amount/2, y*amount + amount/2, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
}
}
break;
}
}
void Interlace(SDL_Surface* surface, int amount, SDL_Color color, flag style, flag blend){
switch (style){
case IL_DEFAULT: //IL_DEFAULT and IL_WEAVE perform same operation, this case layout is known as 'falling through' (notice no break statement under IL_DEFAULT case)
case IL_WEAVE:
for (int y = 0; y < surface->h / amount; y++){
for (int x = 0; x < surface->w / amount; x++){
Uint32 col = GetPixel(surface, x*amount, y*amount); SDL_Color rgba;
SDL_GetRGBA(col, surface->format, &rgba.r, &rgba.g, &rgba.b, &rgba.a);
switch (blend){
case BLEND_DEFAULT:
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount / 2, (y*amount) + amount / 2, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
break;
case BLEND_ADD:
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r + rgba.r <= 255 ? color.r + rgba.r : 255,
color.g + rgba.g <= 255 ? color.g + rgba.g : 255,
color.b + rgba.b <= 255 ? color.b + rgba.b : 255,
color.a + rgba.a <= 255 ? color.a + rgba.a : 255));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount / 2, (y*amount) + amount / 2, SDL_MapRGBA(surface->format, color.r + rgba.r <= 255 ? color.r + rgba.r : 255,
color.g + rgba.g <= 255 ? color.g + rgba.g : 255,
color.b + rgba.b <= 255 ? color.b + rgba.b : 255,
color.a + rgba.a <= 255 ? color.a + rgba.a : 255));
break;
case BLEND_SUBTRACT:
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r - rgba.r >= 0 ? color.r - rgba.r : 0,
color.g - rgba.g >= 0 ? color.g - rgba.g : 0,
color.b - rgba.b >= 0 ? color.b - rgba.b : 0,
color.a - rgba.a >= 0 ? color.a - rgba.a : 0));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount / 2, (y*amount) + amount / 2, SDL_MapRGBA(surface->format, color.r - rgba.r >= 0 ? color.r - rgba.r : 0,
color.g - rgba.g >= 0 ? color.g - rgba.g : 0,
color.b - rgba.b >= 0 ? color.b - rgba.b : 0,
color.a - rgba.a >= 0 ? color.a - rgba.a : 0));
break;
/*
case BLEND_MULTIPLY:
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r * rgba.r <= 255 ? color.r * rgba.r : 255,
color.g * rgba.g <= 255 ? color.g * rgba.g : 255,
color.b * rgba.b <= 255 ? color.b * rgba.b : 255,
color.a * rgba.a <= 255 ? color.a * rgba.a : 255));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount / 2, (y*amount) + amount / 2, SDL_MapRGBA(surface->format, color.r * rgba.r <= 255 ? color.r * rgba.r : 255,
color.g * rgba.g <= 255 ? color.g * rgba.g : 255,
color.b * rgba.b <= 255 ? color.b * rgba.b : 255,
color.a * rgba.a <= 255 ? color.a * rgba.a : 255));
break;
case BLEND_DIVIDE:
SetPixel(surface, x*amount, y*amount, SDL_MapRGBA(surface->format, color.r > 0 ? color.r/rgba.r : 0, color.g > 0 ? color.g/rgba.g:0, color.b > 0 ? color.b/rgba.b : 0, color.a > 0 ? color.a/rgba.a : 0));
if (hasPoint(surface, CreateVec2i((x*amount) + amount / 2, (y*amount) + amount / 2)) != false)
SetPixel(surface, (x*amount) + amount / 2, (y*amount) + amount / 2, SDL_MapRGBA(surface->format, color.r > 0 ? color.r/rgba.r : 0, color.g > 0 ? color.g/rgba.g : 0, color.b > 0 ? color.b/rgba.b : 0, color.a > 0 ? color.a/rgba.a : 0));
break;
*/
}
}
}
break;
case IL_HORIZONTAL:
for (int y = 0; y < surface->h / amount; y++){
for (int x = 0; x < surface->w; x++){
Uint32 col = GetPixel(surface, x, y*amount + amount/2); SDL_Color rgba;
SDL_GetRGBA(col, surface->format, &rgba.r, &rgba.g, &rgba.b, &rgba.a);
switch (blend){
case BLEND_DEFAULT:
SetPixel(surface, x, y*amount + amount / 2, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
break;
case BLEND_ADD:
SetPixel(surface, x, y*amount + amount / 2, SDL_MapRGBA(surface->format, color.r + rgba.r <= 255 ? color.r + rgba.r : 255,
color.g + rgba.g <= 255 ? color.g + rgba.g : 255,
color.b + rgba.b <= 255 ? color.b + rgba.b : 255,
color.a + rgba.a <= 255 ? color.a + rgba.a : 255));
break;
case BLEND_SUBTRACT:
SetPixel(surface, x, y*amount + amount / 2, SDL_MapRGBA(surface->format, color.r - rgba.r >= 0 ? color.r - rgba.r : 0,
color.g - rgba.g >= 0 ? color.g - rgba.g : 0,
color.b - rgba.b >= 0 ? color.b - rgba.b : 0,
color.a - rgba.a >= 0 ? color.a - rgba.a : 0));
break;
}
}
}
break;
case IL_VERTICAL:
for (int x = 0; x < surface->w / amount; x++){
for (int y = 0; y < surface->h; y++){
Uint32 col = GetPixel(surface, x*amount + amount/2, y); SDL_Color rgba;
SDL_GetRGBA(col, surface->format, &rgba.r, &rgba.g, &rgba.b, &rgba.a);
switch (blend){
case BLEND_DEFAULT:
SetPixel(surface, x*amount + amount / 2, y, SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a));
break;