-
Notifications
You must be signed in to change notification settings - Fork 0
/
PEL.pyx
14452 lines (11903 loc) · 582 KB
/
PEL.pyx
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
###cython: boundscheck=False, wraparound=False, nonecheck=False, optimize.use_switch=True
"""
MIT License
Copyright (c) 2019 Yoann Berenguer
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.
"""
# LINKS http://seancode.com/demofx/
# https://tympanus.net/codrops/2016/05/03/animated-heat-distortion-effects-webgl/
# https://tympanus.net/codrops/2017/10/10/liquid-distortion-effects/
# fire effect -> http://lodev.org
# distutils: extra_compile_args = -fopenmp
# distutils: extra_link_args = -fopenmp
# NUMPY IS REQUIRED
try:
import numpy
from numpy import ndarray, zeros, empty, uint8, int32, float64, float32, dstack, full, ones,\
asarray, ascontiguousarray
except ImportError:
print("\n<numpy> library is missing on your system."
"\nTry: \n C:\\pip install numpy on a window command prompt.")
raise SystemExit
# CYTHON IS REQUIRED
try:
cimport cython
from cython.parallel cimport prange
except ImportError:
print("\n<cython> library is missing on your system."
"\nTry: \n C:\\pip install cython on a window command prompt.")
raise SystemExit
cimport numpy as np
# OPENCV IS REQUIRED
try:
import cv2
except ImportError:
print("\n<cv2> library is missing on your system."
"\nTry: \n C:\\pip install opencv-python on a window command prompt.")
raise SystemExit
# PYGAME IS REQUIRED
try:
import pygame
from pygame import Color, Surface, SRCALPHA, RLEACCEL, BufferProxy
from pygame.surfarray import pixels3d, array_alpha, pixels_alpha, array3d
from pygame.image import frombuffer
except ImportError:
print("\n<Pygame> library is missing on your system."
"\nTry: \n C:\\pip install pygame on a window command prompt.")
raise SystemExit
cimport numpy as np
import random
from random import randint
import math
from libc.math cimport sin, sqrt, cos, atan2, pi, round, floor, fmax, fmin, pi, tan, exp, ceil, fmod
from libc.stdio cimport printf
from libc.stdlib cimport srand, rand, RAND_MAX, qsort, malloc, free, abs
from RippleEffect import droplet_float, droplet_int, droplet_grad
__author__ = "Yoann Berenguer"
__credits__ = ["Yoann Berenguer"]
__version__ = "1.0.0 untested"
__maintainer__ = "Yoann Berenguer"
__email__ = "yoyoberenguer@hotmail.com"
DEF OPENMP = True
# num_threads – The num_threads argument indicates how many threads the team should consist of.
# If not given, OpenMP will decide how many threads to use.
# Typically this is the number of cores available on the machine. However,
# this may be controlled through the omp_set_num_threads() function,
# or through the OMP_NUM_THREADS environment variable.
if OPENMP == True:
DEF THREAD_NUMBER = 8
else:
DEF THREAD_NUMNER = 1
# static:
# If a chunksize is provided, iterations are distributed to all threads ahead of
# time in blocks of the given chunksize. If no chunksize is given, the iteration
# space is divided into chunks that are approximately equal in size,
# and at most one chunk is assigned to each thread in advance.
# This is most appropriate when the scheduling overhead matters and the problem can be
# cut down into equally sized chunks that are known to have approximately the same runtime.
# dynamic:
# The iterations are distributed to threads as they request them, with a default chunk size of 1.
# This is suitable when the runtime of each chunk differs and is not known in advance and
# therefore a larger number of smaller chunks is used in order to keep all threads busy.
# guided:
# As with dynamic scheduling, the iterations are distributed to threads as they request them,
# but with decreasing chunk size. The size of each chunk is proportional to the number of
# unassigned iterations divided by the number of participating threads,
# decreasing to 1 (or the chunksize if provided).
# This has an advantage over pure dynamic scheduling when it turns out that the last chunks
# take more time than expected or are otherwise being badly scheduled, so that
# most threads start running idle while the last chunks are being worked on by
# only a smaller number of threads.
# runtime:
# The schedule and chunk size are taken from the runtime scheduling variable,
# which can be set through the openmp.omp_set_schedule() function call,
# or the OMP_SCHEDULE environment variable. Note that this essentially
# disables any static compile time optimisations of the scheduling code itself
# and may therefore show a slightly worse performance than when the same scheduling
# policy is statically configured at compile time. The default schedule is implementation defined.
# For more information consult the OpenMP specification [1].
DEF SCHEDULE = 'static'
DEF HALF = 1.0/2.0
DEF ONE_THIRD = 1.0/3.0
DEF ONE_FOURTH = 1.0/4.0
DEF ONE_FIFTH = 1.0/5.0
DEF ONE_SIXTH = 1.0/6.0
DEF ONE_SEVENTH = 1.0/7.0
DEF ONE_HEIGHT = 1.0/8.0
DEF ONE_NINTH = 1.0/9.0
DEF ONE_TENTH = 1.0/10.0
DEF ONE_ELEVENTH = 1.0/11.0
DEF ONE_TWELVE = 1.0/12.0
DEF ONE_255 = 1.0/255.0
DEF ONE_360 = 1.0/360.0
DEF TWO_THIRD = 2.0/3.0
DEF DEG_TO_RAD = 3.14159265359 / 180.0
DEF RAD_TO_DEG = 180.0 / 3.14159265359
cdef:
float [360] MSIN = numpy.zeros(360, dtype=numpy.float32)
float [360] MCOS = numpy.zeros(360, dtype=numpy.float32)
int i = 0
# Pre-calculate sin and cos for angle [0...360] degrees
for i in range(360):
MCOS[i] = <float>(cos(1 * DEG_TO_RAD))
MSIN[i] = <float>(sin(1 * DEG_TO_RAD))
cdef struct color_tuple:
unsigned char red
unsigned char green
unsigned char blue
ctypedef color_tuple t_color
cdef extern from 'library.c' nogil:
double distance (double x1, double y1, double x2, double y2)
double gaussian (double v, double sigma)
int * quickSort(int arr[], int low, int high)
double * rgb_to_hsv(double red, double green, double blue)
double * hsv_to_rgb(double h, double s, double v)
double * rgb_to_hls(double r, double g, double b)
double * hls_to_rgb(double h, double l, double s)
double fmax_rgb_value(double red, double green, double blue)
double fmin_rgb_value(double red, double green, double blue)
unsigned char max_rgb_value(unsigned char red, unsigned char green, unsigned char blue);
unsigned char min_rgb_value(unsigned char red, unsigned char green, unsigned char blue);
unsigned char umax_(unsigned char a, unsigned char b);
#***********************************************
#********** METHOD HSV TO RGB ***************
#***********************************************
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
# CYTHON
# cython version of hsv to rgb and rgb to hsv conversion model,
# are offering much better performances than
# colorsys methods.
# The C version of those two technics are offering by far the best
# performances.
def hsv2rgb(h: float, s: float, v: float):
cdef float *rgb
rgb = hsv2rgb_c(h, s, v)
return rgb[0], rgb[1], rgb[2]
# CYTHON
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def rgb2hsv(r: float, g: float, b: float):
cdef float *hsv
hsv = rgb2hsv_c(r, g, b)
return hsv[0], hsv[1], hsv[2]
# C VERSION
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def rgb_to_hsv_c(r, g, b):
cdef double *hsv
hsv = rgb_to_hsv(r, g, b)
return hsv[0], hsv[1], hsv[2]
# C VERSION
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def hsv_to_rgb_c(r: float, g: float, b: float):
cdef double *hsv
hsv = hsv_to_rgb(r, g, b)
return hsv[0], hsv[1], hsv[2]
#***********************************************
#********** METHOD RGB TO HLS ***************
#***********************************************
# CYTHON
# cython version of hls to rgb and rgb to hls conversion model,
# are offering much better performances than
# colorsys methods.
# The C version of those two technics are offering by far the best
# performances.
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def rgb2hls(r: float, g: float, b: float):
cdef float *hls
hls = rgb2hls_c(r, g, b)
return hls[0], hls[1], hls[2]
# CYTHON
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def hls2rgb(h:float , l: float, s: float):
cdef float *rgb
rgb = hls2rgb_c(h, l, s)
return rgb[0], rgb[1], rgb[2]
# C VERSION
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def rgb_to_hls_c(r: float, g: float, b: float):
cdef double *hls
hls = rgb_to_hls(r, g, b)
return hls[0], hls[1], hls[2]
# C VERSION
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def hls_to_rgb_c(h:float , l: float, s: float):
cdef double *rgb
rgb = hls_to_rgb(h, l, s)
return rgb[0], rgb[1], rgb[2]
#***********************************************
#********** METHOD TRANSPARENCY ***************
#***********************************************
# Create transparent/opaque texture
# 3 methods for controlling transparency and opacity of an image
# 1) All pixels.
# 2) Value (threshold) control pixels that needs to be adjusted.
# 3) Use a mask to determine parts of the image that will be affected
# ADD TRANSPARENCY -----------------------------
def make_transparent(image_: Surface, alpha_: int)->Surface:
return make_transparent_c(image_, alpha_)
# Use a buffer instead of numpy arrays (faster)
# image/texture has to be converted with convert_alpha()
# Return an BGRA texture if conversion is omitted.
def make_transparent_buffer(image_: Surface, alpha_: int)->Surface:
return make_transparent_b(image_, alpha_)
def make_array_transparent(rgb_array_: ndarray, alpha_array_: ndarray, alpha_: int)->Surface:
return make_array_transparent_c(rgb_array_, alpha_array_, alpha_)
def transparent_mask(image: pygame.Surface, mask_alpha: numpy.ndarray):
return transparent_mask_c(image, mask_alpha)
# ADD OPACITY ----------------------------------
def make_opaque(image_:Surface, alpha_: int) -> Surface:
return make_opaque_c(image_, alpha_)
# Image/texture have to be converted with convert_alpha()
# If the conversion is omitted, output image will be BGRA format.
def make_opaque_buffer(image_:Surface, alpha_: int) -> Surface:
return make_opaque_b(image_, alpha_)
def make_array_opaque(rgb_array_:ndarray, alpha_array_:ndarray, alpha_: int) -> Surface:
return make_array_opaque_c(rgb_array_, alpha_array_, alpha_)
# Change pixels with alpha value = 255 only (preserve transparency mask).
# Value are not cap to [0..255] allowing value to shift from 255 to 0 and vice versa creating
# an illusion of blinking when animated.
# Compatible only with 32-bit surface (use convert_alpha) prior processing.
def blink32(image_: Surface, alpha_: int) -> Surface:
return blink32_b(image_, alpha_)
# change alpha value for pixels RGBA with alpha != 255
# This method allow to change only the transparency part of an
# image (transparency mask) and not the visible pixels.
# This method is the exact opposite of blink32
def blink32_mask(image_: Surface, alpha_: int) -> Surface:
return blink32_mask_b(image_, alpha_)
# FILTERING RGB VALUES -------------------------
def low_th_alpha(surface_: Surface, new_alpha_: int, threshold_: int) -> Surface:
return low_threshold_alpha_c(surface_, new_alpha_, threshold_)
def high_th_alpha(surface_: Surface, new_alpha_: int, threshold_: int) -> Surface:
return high_threshold_alpha_c(surface_, new_alpha_, threshold_)
#***********************************************
#********** METHOD GREYSCALE ******************
#***********************************************
# CONSERVE LIGHTNESS ----------------------------
def greyscale_light_alpha(image: Surface)->Surface:
return greyscale_lightness_alpha_c(image)
def greyscale_light(image: Surface)->Surface:
return greyscale_lightness_c(image)
# CONSERVE LUMINOSITY --------------------------
# compatible 32-bit
def greyscale_lum_alpha(image: Surface)->Surface:
return greyscale_luminosity_alpha_c(image)
# compatible 24-bit
def greyscale_lum(image: Surface)->Surface:
return greyscale_luminosity_c(image)
# AVERAGE VALUES --------------------------------
# compatible 32-bit
def make_greyscale_32(image: Surface)->Surface:
return make_greyscale_32_c(image)
# compatible 24-bit
def make_greyscale_24(image: Surface)->Surface:
return make_greyscale_24_c(image)
# GREYSCALE ARRAYS ------------------------------
def make_greyscale_altern(image: Surface)->Surface:
return make_greyscale_altern_c(image)
# 3d array to surface
# in : RGB array shape (width, height, 3)
# out: Greyscale pygame surface
def greyscale_arr2surf(array_: ndarray)->Surface:
return greyscale_arr2surf_c(array_)
# in : RGB array shape (width, height, 3)
# out: greyscale array (width, height, 3)
def greyscale_array(array_: ndarray)->ndarray:
return greyscale_array_c(array_)
# in : RGB array shape (width, height, 3)
# out: greyscale 2d array shape (width, height)
def greyscale_3d_to_2d(array_: ndarray)->ndarray:
return greyscale_3d_to_2d_c(array_)
# in : 2d array shape (width, height)
# out: greyscale 3d array shape (width, height, 3)
def greyscale_2d_to_3d(array_: ndarray)->ndarray:
return greyscale_2d_to_3d_c(array_)
# TODO: need testing
def greyscale_mask(image: pygame.Surface, mask_array: numpy.ndarray):
raise NotImplemented
# TODO NOT TESTED
# in : pygame Surface (color)
# out: tuple (greyscale surface, 1d buffer )
def buffer_greyscale(image: pygame.Surface)->tuple:
return buffer_greyscale_c(image)
#-----------------------------------------------
#***********************************************
#******* BLACK & WHITE TRANSFORM *************
#***********************************************
# compatible 24-bit
def bw_surface24(image: pygame.Surface)->tuple:
return bw_surface24_c(image)
# compatible 32-bit
def bw_surface32(image: pygame.Surface)->tuple:
return bw_surface32_c(image)
# in : 3d or 2d array (RGB colors or greyscale values)
# out: Black and white 3d or 2d array (Depends on input array)
def bw_array(array: numpy.ndarray)->numpy.ndarray:
return bw_array_c(array)
#-----------------------------------------------
#***********************************************
#********** METHOD COLORIZE ******************
#***********************************************
# --------- functions uses buffer --------------------
def redscale_buffer(image: Surface)->Surface:
return redscale_b(image)
# compatible 32-bit
def redscale_alpha_buffer(image: Surface)->Surface:
return redscale_alpha_b(image)
# compatible 24-bit
def greenscale_buffer(image: Surface)->Surface:
return greenscale_b(image)
# compatible 32-bit
def greenscale_alpha_buffer(image: Surface)->Surface:
return greenscale_alpha_b(image)
# compatible 24-bit
def bluescale_buffer(image: Surface)->Surface:
return bluescale_b(image)
# compatible 32-bit
def bluescale_alpha_buffer(image: Surface)->Surface:
return bluescale_alpha_b(image)
# ---- Same functions but use arrays --------------
def redscale(image: Surface)->Surface:
return redscale_c(image)
def redscale_alpha(image: Surface)->Surface:
return redscale_alpha_c(image)
def greenscale(image: Surface)->Surface:
return greenscale_c(image)
def greenscale_alpha(image: Surface)->Surface:
return greenscale_alpha_c(image)
def bluescale(image: Surface)->Surface:
return bluescale_c(image)
def bluescale_alpha(image: Surface)->Surface:
return bluescale_alpha_c(image)
#***********************************************
#********* METHOD LOADING PER-PIXEL **********
#***********************************************
def load_per_pixel(file: str)->Surface:
return load_per_pixel_c(file)
def load_image32(path: str)->Surface:
return load_image32_c(path)
#***********************************************
#********* METHOD LOAD SPRITE SHEET ************
#***********************************************
def spritesheet_per_pixel(file_: str, chunk_: int,
colums_: int, rows_: int)->list:
return spritesheet_per_pixel_c(file_, chunk_, colums_, rows_)
def spritesheet_per_pixel_fs8(file: str, chunk: int,
columns_: int, rows_: int, tweak_:bool=False, *args)->list:
...
def spritesheet_alpha(file: str, chunk: int, columns_: int,
rows_: int, tweak_:bool=False, *args)->list:
...
def spritesheet(file, int chunk, int columns_, int rows_,
tweak_: bool = False, *args)->list:
...
def spritesheet_fs8(file: str, chunk: int, columns_: int,
rows_: int, tweak_: bool=False, *args) -> list:
...
def spritesheet_new(file_: str, chunk_: int, columns_: int, rows_: int):
return spritesheet_new_c(file_, chunk_, columns_, rows_)
#***********************************************
#********** METHOD SHADOW *********************
#***********************************************
def shadow32(image: Surface, attenuation: float)->Surface:
return shadow_32c(image, attenuation)
def shadow32buffer(image: Surface, attenuation: float)->Surface:
return shadow_32b(image, attenuation)
#***********************************************
#********** METHOD MAKE_ARRAY ****************
#***********************************************
# create a 3d array shape (w, h, 4) from RGB and ALPHA array values
# in : RGB shape (w, h, 3) and alpha (w, h) shape
# out: numpy.ndarray shape (w, h, 4).
# All stacked values are copied into a new array keeping the exact same
# indexing (stride) and homogeneity.
def make_array(rgb_array_: ndarray, alpha_:ndarray):
return make_array_c_code(rgb_array_, alpha_)
# Create a 3d array shape (w, h, 4) from RGB and ALPHA array values
# in : RGB shape (w, h, 3) and alpha (w, h) shape
# out: numpy.ndarray shape (w, h, 4). numpy.dstack equivalent
# All values are transpose into a new array (this method is used when the
# array need to be flipped before creating the pygame surface with pygame.frombuffer
# *pygame.frombuffer cannot flipped the surface.
def make_array_trans(rgb_array_: ndarray, alpha_:ndarray):
return make_array_c_transpose(rgb_array_, alpha_)
# Create a 3d array shape (w, h, 4) from a bufferproxy object
# in : BufferProxy
# out: numpy.ndarray shape (w, h, 4)
def make_array_from_buffer(buffer_: BufferProxy, size_: tuple)->ndarray:
return make_array_from_buffer_c(buffer_, size_)
#***********************************************
#********** METHOD MAKE_SURFACE ***************
#***********************************************
def make_surface(rgba_array: ndarray) -> Surface:
return make_surface_c(rgba_array)
def make_surface_c1(buffer_: BufferProxy, w, h)->Surface:
return make_surface_c_1(buffer_, w, h)
def make_surface_c2(rgba_array_: ndarray)->Surface:
return make_surface_c_2(rgba_array_)
def make_surface_c4(rgba_array_: ndarray)->Surface:
return make_surface_c_4(rgba_array_)
def make_surface_c5(rgba_array_: ndarray)->Surface:
return make_surface_c_5(rgba_array_)
def make_surface_c6(rgba_array_: ndarray)->Surface:
return make_surface_c_6(rgba_array_)
#*********************************************
#********** METHOD SPLIT RGB ****************
#*********************************************
def rgb_split(surface_: Surface)->tuple:
return rgb_split_c(surface_)
def rgb_split_buffer(surface_: Surface)-> tuple:
return rgb_split_b(surface_)
def rgb_split32(surface_: Surface):
return rgb_split32_c(surface_)
def rgb_split32_buffer(surface_: Surface):
return rgb_split32_b(surface_)
def red_channel(surface_: Surface)->Surface:
return red_channel_c(surface_)
def green_channel(surface_: Surface)->Surface:
return green_channel_c(surface_)
def blue_channel(surface_: Surface)->Surface:
return blue_channel_c(surface_)
def red_channel_buffer(surface_: Surface)->Surface:
return red_channel_b(surface_)
def green_channel_buffer(surface_: Surface)->Surface:
return green_channel_b(surface_)
def blue_channel_buffer(surface_: Surface)->Surface:
return blue_channel_b(surface_)
#*********************************************
#********** SWAP CHANNELS *****************
#*********************************************
def swap_channels(surface: Surface, model: str):
return swap_channels_c(surface, model)
#*********************************************
#********** METHOD FISHEYE *****************
#*********************************************
def fish_eye(image)->Surface:
return fish_eye_c(image)
def fish_eye_32(image)->Surface:
return fish_eye_32c(image)
#*********************************************
#********** METHOD ROTATE ******************
#*********************************************
def rotate_inplace(image: Surface, angle: int)->Surface:
return rotate_inplace_c(image, angle)
def rotate_24(image: Surface, angle: int)->Surface:
return rotate_c24(image, angle)
def rotate_32(image: Surface, angle: int)->Surface:
return rotate_c32(image, angle)
#*********************************************
#********** METHOD HUE SHIFT ***************
#*********************************************
# TODO CREATE SIMILAR METHOD WITH BUFFER
def hue_surface_24(surface_: Surface, float shift_)->Surface:
return hue_surface_24c(surface_, shift_)
def hue_surface_32(surface_: Surface, float shift_)->Surface:
return hue_surface_32c(surface_, shift_)
# HUE A SURFACE WITH A GIVEN MASK
# TODO NOT TESTED
def hue_mask(surface_: Surface, shift_: float, mask_array=None)->Surface:
return hue_mask_c(surface_, shift_, mask_array)
# HUE GIVEN COLOR VALUE FROM A SURFACE 24BIT
def hue_surface_24_color(surface_: Surface, float shift_, red, green, blue)->Surface:
return hue_surface_24_color_c(surface_, shift_, red, green, blue)
# HUE GIVEN COLOR VALUE FROM A SURFACE 32BIT
def hue_surface_32_color(surface_: Surface, float shift_, red, green, blue)->Surface:
raise NotImplemented
# HUE PIXEL MEAN AVG VALUE OVER OR EQUAL TO A GIVEN THRESHOLD VALUE
def hsah(surface_: Surface, threshold_: int, shift_: float)->Surface:
return hsah_c(surface_, threshold_, shift_)
# HUE PIXEL MEAN AVG VALUE BELOW OR EQUAL TO A GIVEN THRESHOLD VALUE
def hsal(surface_: Surface, threshold_: int, shift_: float)->Surface:
return hsal_c(surface_, threshold_, shift_)
# HUE ARRAY (RED CHANNEL)
def hue_array_red(array_: ndarray, shift_: float)->ndarray:
return hue_array_red_c(array_, shift_)
# HUE ARRAY (GREEN CHANNEL)
def hue_array_green(array: ndarray, shift_: float)->ndarray:
return hue_array_green_c(array, shift_)
# HUE ARRAY (BLUE CHANNEL)
def hue_array_blue(array: ndarray, shift_: float)->ndarray:
return hue_array_blue_c(array, shift_)
# --------------- BUFFERS -----------------------------
# RED CHANNEL
def hue_red24(array_: ndarray, shift_: float)->Surface:
return hue_red24_b(array_, shift_)
def hue_red32(array_: ndarray, shift_: float)->Surface:
return hue_red32_b(array_, shift_)
# GREEN CHANNEL
def hue_green24(array_: ndarray, shift_: float)->Surface:
return hue_green24_b(array_, shift_)
def hue_green32(array_: ndarray, shift_: float)->Surface:
return hue_green32_b(array_, shift_)
# BLUE CHANNEL
def hue_blue24(array_: ndarray, shift_: float)->Surface:
return hue_blue24_b(array_, shift_)
def hue_blue32(array_: ndarray, shift_: float)->Surface:
return hue_blue32_b(array_, shift_)
#*********************************************
#********** METHOD BRIGHTNESS **************
#*********************************************
def brightness_24(surface_: Surface, shift_: float)->Surface:
return brightness_24c(surface_, shift_)
def brightness_32(surface_: Surface, shift_: float)->Surface:
return brightness_32c(surface_, shift_)
def brightness_24_i(surface_: Surface, shift_: float)->Surface:
return brightness_24_fast(surface_, shift_)
def brightness_32_i(surface_: Surface, shift_: float)->Surface:
return brightness_32_fast(surface_, shift_)
# TODO NOT TESTED
def brightness_mask_32(surface_:Surface, shift_: float, mask_array=None):
return brightness_mask_32c(surface_, shift_, mask_array)
# TODO NOT TESTED
def brightness_mask_24(surface_:Surface, shift_: float, mask_array=None):
return brightness_mask_24c(surface_, shift_, mask_array)
#*********************************************
#********** METHOD SATURATION **************
#*********************************************
def saturation_24(surface_: Surface, shift_: float)->Surface:
return saturation_24_c(surface_, shift_)
def saturation_32(surface_: Surface, shift_: float)->Surface:
return saturation_32_c(surface_, shift_)
def saturation_mask_24(surface_: Surface, shift_: float,
mask_array: numpy.ndarray)->Surface:
return saturation_mask_24_c(surface_, shift_, mask_array)
def saturation_mask_32(surface_: Surface, shift_: float,
mask_array: numpy.ndarray)->Surface:
return saturation_mask_32_c(surface_, shift_, mask_array)
#*********************************************
#********** METHOD ROLL/SCROLL *************
#*********************************************
# ROLL ARRAY 3D TYPE (W, H, 3) NUMPY.UINT8
def scroll_array(array: ndarray, dy: int=0, dx: int=0) -> ndarray:
return scroll_array_c(array, dy, dx)
# ROLL ARRAY 3D TYPE (W, H, 4) NUMPY.UINT8
def scroll_array_32(array: ndarray, dy: int=0, dx: int=0) -> ndarray:
return scroll_array_32_c(array, dy, dx)
# USE NUMPY LIBRARY (NUMPY.ROLL)
def scroll_surface_org(array: ndarray, dx: int=0, dy: int=0)->tuple:
...
# Roll the value of an entire array (lateral and vertical)
# Identical algorithm (scroll_array) but returns a tuple (surface, array)
def scroll_surface(surface: pygame.Surface, dy: int=0, dx: int=0)->tuple:
return scroll_surface_c(surface, dy, dx)
# ROLL IMAGE TRANSPARENCY INSTEAD
def scroll_surface_alpha(surface: pygame.Surface, dy: int=0, dx: int=0) -> tuple:
return scroll_surface_alpha_c(surface, dy, dx)
#*********************************************
#********** METHOD GRADIENT ****************
#*********************************************
# Create an horizontal array filled with gradient color, shape(w, h) and w > h
def gradient_horizarray(width: int, height: int, start_color: tuple, end_color: tuple)->ndarray:
return gradient_horizarray_c(width, height, start_color, end_color)
# Create a vertical array filled with gradient color shape(w, h) and h > w
def gradient_vertarray(width: int, height: int, top_value: tuple, bottom_value: tuple)-> ndarray:
return gradient_vertarray_c(width, height, top_value, bottom_value)
def gradient_horiz_2darray(width: int, height: int, start_value: int, end_value: int)-> ndarray:
return gradient_horiz_2darray_c(width, height, start_value, end_value)
def gradient_vert_2darray(width: int, height: int, top_value: int, bottom_value: int)-> ndarray:
return gradient_vert_2darray_c(width, height, top_value, bottom_value)
#*********************************************
#********** METHOD BLENDING ****************
#*********************************************
def blend_texture(surface_: Surface, max_steps: int,
final_color_:(tuple, Color), goto_value: int) -> Surface:
return blend_texture_c(surface_, max_steps, final_color_, goto_value)
def blend_2_textures(source_: Surface,
destination_: Surface,
steps: int,
lerp_to_step: int) -> Surface:
return blend_2_textures_c(source_, destination_, steps, lerp_to_step)
def alpha_blending(surface1: Surface, surface2: Surface) -> Surface:
return alpha_blending_c(surface1, surface2)
def alpha_blending_static(surface1: Surface, surface2: Surface, float a1, float a2)->Surface:
return alpha_blending_static_c(surface1, surface2, a1, a2)
#*********************************************
#********** METHOD INVERT ******************
#*********************************************
def invert_surface_24bit(image: Surface)->Surface:
return invert_surface_24bit_c(image)
def invert_surface_32bit(image: Surface)->Surface:
return invert_surface_32bit_c(image)
def invert_array_24(array_: ndarray)->ndarray:
return invert_array_24_c(array_)
def invert_array_32(array_: ndarray)->ndarray:
return invert_array_32_c(array_)
# --------------- BUFFERS --------------------
def invert_surface_24bit_buffer(image: Surface)->Surface:
return invert_surface_24bit_b(image)
def invert_surface_32bit_buffer(image: Surface)->Surface:
return invert_surface_32bit_b(image)
#*********************************************
#********** KERNEL OPERATION ***************
#*********************************************
def kernel_deviation(sigma: float, kernel_size: int)->ndarray:
return deviation(sigma, kernel_size)
# Sharp filter
# Sharpening an image increases the contrast between bright and dark regions
# to bring out features. The sharpening process is basically the application
# of a high pass filter to an image. The following array is a kernel for a
# common high pass filter used to sharpen an image.
# This method cannot use a defined kernel.
# The border pixels are convolved with the adjacent pixels.
# The final image will have the exact same size than the original
def sharpen3x3(image:Surface)-> ndarray:
return sharpen3x3_c(image)
def sharpen3x3_mask():
raise NotImplemented
def sharpen5x5(image: Surface)-> ndarray:
raise NotImplemented
# Blur effect
def guaussian_boxblur3x3(image: Surface)->ndarray:
return guaussian_boxblur3x3_c(image)
def guaussian_boxblur3x3_approx(image: Surface)->ndarray:
return guaussian_boxblur3x3_capprox(image)
def guaussian_blur3x3(image: Surface)->ndarray:
return guaussian_blur3x3_c(image)
def gaussian_blur5x5(rgb_array: ndarray):
return gaussian_blur5x5_c(rgb_array)
# blur mask
def gaussian_blur5x5_mask(rgb_array: ndarray, mask: ndarray):
raise gaussian_blur5x5_mask_c(rgb_array, mask)
# Edge detection
def edge_detection3x3(image: Surface)->ndarray:
return edge_detection3x3_c(image)
def edge_detection3x3_alter(image: Surface)->ndarray:
return edge_detection3x3_c1(image)
def edge_detection3x3_fast(image: Surface)->ndarray:
return edge_detection3x3_c2(image)
def edge_detection5x5(image: Surface)->ndarray:
raise NotImplemented
# http//www.sciencedirect.com
# High pass filter
# a high-pass filter enhances the high-frequency parts of an image be reducing
# the low frequency components.This type of filter can be used to sharpen image.
def highpass_3x3(image:Surface)->ndarray:
raise highpass_3x3_c(image)
def highpass_5x5(image: Surface)->ndarray:
raise highpass_5x5_c(image)
# Laplacien Filter
# The Laplacien filter enhances discontinuities, it outputs brighter pixel values
# as it passes over parts of the image, that have abrupt changes in intensity,
# and outputs darker values where the image in not changing rapidly.
def laplacien_3x3(image: Surface)->ndarray:
raise laplacien_3x3_c(image)
def laplacien_5x5(image: Surface)->ndarray:
raise laplacien_5x5(image)
# Gradient detection (Embossing)
# Changes in value over 3 pixels can be detected using kernels called gradient
# masks or prewitt masks. The filter detects changes in gradient along limited
# directions, named after points of the compass (with north equal to the up
# direction on the screen)
def emboss_3x3(image: Surface)->ndarray:
raise NotImplemented
def emboss_5x5(image: Surface)->ndarray:
raise NotImplemented
def motion_blur(image: Surface)->ndarray:
"""
https://lodev.org/cgtutor/filtering.html
double filter[filterHeight][filterWidth] =
{
1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1,
};
"""
raise NotImplemented
#*********************************************
#********** WATER DROP EFFECT **************
#*********************************************
def water_effect_f(texture, frames: int, dropx: int, dropy: int, intensity: int, drops_number: int):
return water_ripple_effect_cf(texture, frames, dropx, dropy, intensity, drops_number)
def water_effect_i(texture, frames: int, dropx: int, dropy: int, intensity: int, drops_number: int):
return water_ripple_effect_ci(texture, frames, dropx, dropy, intensity, drops_number)
def water_ripple_effect_rand(texture, frames: int, intensity: int, drops_number: int):
return water_ripple_effect_crand(texture, frames, intensity, drops_number)
#*********************************************
#************** HPF/LPF ********************
#*********************************************
def lpf(rgb_array: ndarray)->ndarray:
...
def hpf(rgb_array: ndarray)->ndarray:
...
#*********************************************
#************** WOBBLY IMAGE ****************
#*********************************************
def wobbly_array(rgb_array: ndarray, alpha_array: ndarray, f: float)->Surface:
return wobbly_array_c(rgb_array, alpha_array, f)
def wobbly_surface(surface: Surface, f: float)->Surface:
return wobbly_surface_c(surface, f)
#*********************************************
#************** SWIRL IMAGE ****************
#*********************************************
def swirl_surface(surface: Surface, degrees: float)->Surface:
return swirl_surface_c(surface, degrees)
def swirl_surf2surf(surface: Surface, degrees: float)->Surface:
return swirl_surf2surf_c(surface, degrees)
#*********************************************
#************* LIGHT EFFECT ****************
#*********************************************
# Create realistic light effect on texture/surface
def light_area(x: int, y: int, background_rgb: ndarray, mask_alpha: ndarray)->Surface:
return light_area_c(x, y, background_rgb, mask_alpha)
def light(rgb: ndarray, alpha: ndarray,
intensity: float, color: ndarray)->Surface:
return light_c(rgb, alpha, intensity, color)
# TODO TEST BELOW AND IMPLEMENT THE METHOD TO THE REST
# def light_buffer(rgb_buffer_: BufferProxy, alpha_buffer_: BufferProxy,
# intensity: float, color: ndarray, int w, int h)->Surface:
# return light_b(rgb_buffer_, alpha_buffer_, intensity, color, w, h)
def light_volume(x: int, y: int, background_rgb: ndarray,
mask_alpha: ndarray, volume: ndarray, magnitude=1e-6)->Surface:
return light_volume_c(x, y, background_rgb, mask_alpha, volume, magnitude)
def light_volumetric(rgb: ndarray, alpha: ndarray, intensity: float,
color: ndarray, volume: ndarray)->Surface:
return light_volumetric_c(rgb, alpha, intensity, color, volume)
#*********************************************
#**************** SEPIA *******************
#*********************************************
def sepia24(surface_: Surface)->Surface:
return sepia24_c(surface_)
# TODO TEST BELOW AND IMPLEMENT THE SAME FOR 32bit
def sepia24_buffer(surface_: Surface)->Surface:
return sepia24_b(surface_)
def sepia32(surface_: Surface)->Surface:
return sepia32_c(surface_)
def sepia_mask_24(surface_: Surface, mask: numpy.ndarray)-> Surface:
return sepia24_mask_c(surface_, mask)
def sepia_mask_32(surface_: Surface, mask: numpy.ndarray)-> Surface:
return sepia32_mask_c(surface_, mask)
# def unsepia24(surface_: Surface)->Surface:
# return unsepia24_c(surface_)