-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLOOM.pyx
2215 lines (1786 loc) · 86.7 KB
/
BLOOM.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
"""
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.
DEFINITION:
Bloom effet is a computer graphics effect used in video games, demos,
and high dynamic range rendering to reproduce an imaging artifact of real-world cameras.
Requirements:
- Pygame 3
- Numpy
- Cython (C extension for python)
- A C compiler for windows (Visual Studio, MinGW etc) install on your system
and linked to your windows environment.
Note that some adjustment might be needed once a compiler is install on your system,
refer to external documentation or tutorial in order to setup this process.
e.g https://devblogs.microsoft.com/python/unable-to-find-vcvarsall-bat/
METHOD:
Acronyme : bpf (Bright Pass Filter)
1)First apply a bright pass filter to the pygame surface(SDL surface) using methods
bpf24_b_c or bpf32_b_c (adjust the luminence threshold value to get the best filter effect).
2) Downscale the newly created bpf image into sub-surface factor x2, x4, x8, x16 using
pygame transform.scale method. No need to use smoothscale (bilinear filtering method).
3) Apply a Gaussian blur 5x5 filter on each of the downsized bpf images (if variable smooth_ is > 1,
then the Gaussian filter 5x5 will by applied more than once. Note, this will have little effect
on the final image quality.
4) Re-scale to orinial sizes all the bpf images using a bilinear filtering method.
Note : Using an un-filtered rescaling method will pixelate the final output image.
Recommandation: For best performances sets smoothscale acceleration.
A value of 'GENERIC' turns off acceleration. 'MMX' uses MMX instructions only.
'SSE' allows SSE extensions as well.
5) Blit all the bpf images on the original pygame surface (input image), use pygame additive
blend mode effect.
BUILDING THE PROJECT:
In a command prompt and under the directory containing the source files
C:\>python setup_bloom.py build_ext --inplace
If the compilation fail, refers to the requirement section and make sure cython
and a C-compiler are correctly install on your system.
TWO METHODS:
# Below bloom method is using massively numpy.ndarray to manipulate data.
bloom = bloom_effect_array(surface, threshold, smooth_=1)
# Below method is using BufferProxy or C-Buffer data structure.
# It is also the fastest algorithm available.
bloom = bloom_effect_buffer(surface, threshold, smooth_=1)
Surface : pygame.Surface to be bloom (compatible 24, 32-bit format)
threshold : Integer value for the bright pass filter, filter threshold value
smooth_ : Smooth define the quantity of Gaussian blur5x5 kernel passes that will be
applied to all sub-surface (default is 1, vertical & horizontal)
Note the Gaussian algorithm is cpu demanding. 4 is plenty smoothing
Left image with smooth_=1, right image with smooth_=10
TIPS:
python test_bloom.py
If you get the following error message after execution, refer to
the section building the project.
<<ModuleNotFoundError: No module named 'bloom'>>
DEMO VERSION:
Decompress the archive in the source directory
In order to work, demo.exe must be under the same directory with i2.jpg
Copy/move i2.jpg if necessary to demo.exe location.
TIMING:
print(timeit.timeit("bloom_effect_array(im, 255, smooth_=1)",
"from __main__ import bloom_effect_array, im", number=10) / 10)
print(timeit.timeit("bloom_effect_buffer(im, 255, smooth_=1)",
"from __main__ import bloom_effect_buffer, im", number=10) / 10)
Method 1:
bloom_effect_array
texture 600x600 24-bit gives a modest 0.0793 (79ms) processing time
Method 2:
bloom_effect_buffer
texture 600x600 24-bit gives a modest 0.04516 (45ms) processing time
Those values are not too bad considering that all the texture processing is done
entirely by the CPU.
Soon I will implement a mask method that will improve efficieny of both techniques.
LINKS:
https://learnopengl.com/Advanced-Lighting/Bloom
https://kalogirou.net/2006/05/20/how-to-do-good-bloom-for-hdr-rendering/
https://catlikecoding.com/unity/tutorials/advanced-rendering/bloom/
"""
__version__ = 1.00
try:
import pygame
from pygame.transform import scale, smoothscale
from pygame import BLEND_RGB_ADD
except ImportError:
print("\n<pygame> library is missing on your system."
"\nTry: \n C:\\pip install numpy on a window command prompt.")
raise SystemExit
try:
cimport cython
from cython.parallel cimport prange
from cpython cimport PyObject, PyObject_HasAttr, PyObject_IsInstance, PyObject_CallFunctionObjArgs
from cpython.list cimport PyList_Append, PyList_GetItem, PyList_Size, PyList_SetItem
from cpython.dict cimport PyDict_Values, PyDict_Keys, PyDict_Items, PyDict_GetItem, \
PyDict_SetItem, PyDict_Copy
except ImportError:
print("\n<cython> library is missing on your system."
"\nTry: \n C:\\pip install cython on a window command prompt.")
raise SystemExit
try:
import numpy
from numpy import asarray, uint8, float32, zeros, float64, frombuffer
except ImportError:
print("\n<numpy> library is missing on your system."
"\nTry: \n C:\\pip install numpy on a window command prompt.")
cimport numpy as np
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
import timeit
# TODO
# 1)Create a method equivalent of pygame.Surface.copy() very slow
DEF THREADS = 8
DEF METHOD = 'static'
# --------------------------- INTERFACE ----------------------------------
# MAP BUFFER INDEX VALUE INTO 3D INDEXING
cpdef to3d(index, width, depth):
return to3d_c(index, width, depth)
# MAP 3D INDEX VALUE INTO BUFFER INDEXING
cpdef to1d(x, y, z, width, depth):
return to1d_c(x, y, z, width, depth)
# VERTICALLY FLIP A SINGLE BUFFER VALUE
cpdef vmap_buffer(index, width, height, depth):
return vmap_buffer_c(index, width, height, depth)
# FLIP VERTICALLY A BUFFER (TYPE RGB)
cpdef vfb_rgb(source, target, width, height):
return vfb_rgb_c(source, target, width, height)
# FLIP VERTICALLY A BUFFER (TYPE RGBA)
cpdef vfb_rgba(source, target, width, height):
return vfb_rgba_c(source, target, width, height)
# FLIP VERTICALLY A BUFFER (TYPE ALPHA, (WIDTH, HEIGHT))
cpdef vfb(source, target, width, height):
return vfb_c(source, target, width, height)
# C-structure to store 3d array index values
cdef struct xyz:
int x;
int y;
int z;
cpdef blur5x5_buffer24(rgb_buffer, width, height, depth, mask=None):
return blur5x5_buffer24_c(rgb_buffer, width, height, depth, mask=None)
cpdef blur5x5_buffer32(rgba_buffer, width, height, depth, mask=None):
return blur5x5_buffer32_c(rgba_buffer, width, height, depth, mask=None)
cpdef blur5x5_array24(rgb_array_, mask=None):
return blur5x5_array24_c(rgb_array_, mask=None)
cpdef blur5x5_array32(rgb_array_, mask=None):
return blur5x5_array32_c(rgb_array_, mask=None)
# ******* METHODS THAT CAN BE ACCESS DIRECTLY FROM PYTHON SCRIPT ********
cpdef bloom_effect_buffer24(surface_, threshold_, smooth_, mask_=None):
return bloom_effect_buffer24_c(surface_, threshold_, smooth_, mask_=None)
cpdef bloom_effect_buffer32(surface_, int threshold_, int smooth_, mask_=None):
return bloom_effect_buffer32_c(surface_, threshold_, smooth_, mask_=None)
cpdef bloom_effect_array24(surface_, threshold_, smooth_, mask_=None):
return bloom_effect_array24_c(surface_, threshold_, smooth_, mask_=None)
cpdef bloom_effect_array32(surface_, threshold_, smooth_, mask_=None):
return bloom_effect_array32_c(surface_, threshold_, smooth_, mask_=None)
cpdef scale_array24_mult(rgb_array):
return scale_array24_mult_c(rgb_array)
# --------------------------- IMPLEMENTATION -----------------------------
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef inline xyz to3d_c(int index, int width, int depth)nogil:
"""
Map a 1d buffer pixel values into a 3d array, e.g buffer[index] --> array[i, j, k]
Both (buffer and array) must have the same length (width * height * depth)
To speed up the process, no checks are performed upon the function call and
index, width and depth values must be > 0.
:param index: integer; Buffer index value
:param width: integer; image width
:param depth: integer; image depth (3)RGB, (4)RGBA
:return: Array index/key [x][y][z] pointing to a pixel RGB(A) identical
to the buffer index value. Array index values are placed into a C structure (xyz)
"""
cdef xyz v;
cdef int ix = index // depth
v.y = <int>(ix / width)
v.x = ix % width
v.z = index % depth
return v
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef inline int to1d_c(int x, int y, int z, int width, int depth)nogil:
"""
Map a 3d array value RGB(A) into a 1d buffer. e.g array[i, j, k] --> buffer[index]
To speed up the process, no checks are performed upon the function call and
x, y, z, width and depth values must be > 0 and both (buffer and array) must
have the same length (width * height * depth)
:param x: integer; array row value
:param y: integer; array column value
:param z: integer; RGB(3) or RGBA(4)
:param width: source image width
:param depth: integer; source image depth (3)RGB or (4)RGBA
:return: return the index value into a buffer for the given 3d array indices [x][y][z].
"""
return <int>(y * width * depth + x * depth + z)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef inline int vmap_buffer_c(int index, int width, int height, int depth)nogil:
"""
Vertically flipped a single buffer value.
:param index: integer; index value to convert
:param width: integer; Original image width
:param height: integer; Original image height
:param depth: integer; Original image depth=3 for RGB or 4 for RGBA
:return: integer value pointing to the pixel in the buffer (traversed vertically).
"""
cdef:
int ix
int x, y, z
ix = index // depth
y = int(ix / width)
x = ix % width
z = index % depth
return (x * height * depth) + (depth * y) + z
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef inline unsigned char [:] vfb_rgb_c(unsigned char [:] source, unsigned char [:] target,
int width, int height)nogil:
"""
Vertically flipped buffer type RGB
Flip a C-buffer vertically filled with RGB values
Re-sample a buffer in order to swap rows and columns of its equivalent 3d model
For a 3d numpy.array this function would be equivalent to a transpose (1, 0, 2)
Buffer length must be equivalent to width x height x RGB otherwise a valuerror
will be raised.
SOURCE AND TARGET ARRAY MUST BE SAME SIZE.
This method is using Multiprocessing OPENMP
e.g
Here is a 9 pixels buffer (length = 27), pixel format RGB
buffer = [RGB1, RGB2, RGB3, RGB4, RGB5, RGB6, RGB7, RGB8, RGB9]
Equivalent 3d model would be (3x3x3):
3d model = [RGB1 RGB2 RGB3]
[RGB4 RGB5 RGB6]
[RGB7 RGB8 RGB9]
After vbf_rgb:
output buffer = [RGB1, RGB4, RGB7, RGB2, RGB5, RGB8, RGB3, RGB6, RGB9]
and its equivalent 3d model
3D model = [RGB1, RGB4, RGB7]
[RGB2, RGB5, RGB8]
[RGB3, RGB6, RGB9]
:param source: 1d buffer to flip vertically (unsigned char values).
The array length is known with (width * height * depth). The buffer represent
image 's pixels RGB.
:param target: Target buffer must have same length than source buffer)
:param width: integer; Source array's width (or width of the original image)
:param height: integer; source array's height (or height of the original image)
:return: Return a vertically flipped 1D RGB buffer (swapped rows and columns of the 2d model)
"""
cdef:
int i, j, k, index
unsigned char [:] flipped_array = target
for i in prange(0, height * 3, 3):
for j in range(0, width):
index = i + (height * 3 * j)
for k in range(3):
flipped_array[(j * 3) + (i * width) + k] = <unsigned char>source[index + k]
return flipped_array
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef inline unsigned char [:] vfb_rgba_c(unsigned char [:] source, unsigned char [:] target,
int width, int height)nogil:
"""
Vertically flipped buffer
Flip a C-buffer vertically filled with RGBA values
Re-sample a buffer in order to swap rows and columns of its equivalent 3d model
For a 3d numpy.array this function would be equivalent to a transpose (1, 0, 2)
Buffer length must be equivalent to width x height x RGBA otherwise a valuerror
will be raised.
SOURCE AND TARGET ARRAY MUST BE SAME SIZE.
This method is using Multiprocessing OPENMP
e.g
Here is a 9 pixels buffer (length = 36), pixel format RGBA
buffer = [RGBA1, RGBA2, RGBA3, RGBA4, RGBA5, RGBA6, RGBA7, RGBA8, RGBA9]
Equivalent 3d model would be (3x3x4):
3d model = [RGBA1 RGBA2 RGBA3]
[RGBA4 RGBA5 RGBA6]
[RGBA7 RGBA8 RGBA9]
After vbf_rgba:
output buffer = [RGB1A, RGB4A, RGB7A, RGB2A, RGB5A, RGBA8, RGBA3, RGBA6, RGBA9]
and its equivalent 3d model
3D model = [RGBA1, RGBA4, RGBA7]
[RGBA2, RGBA5, RGBA8]
[RGBA3, RGBA6, RGBA9]
:param source: 1d buffer to flip vertically (unsigned char values).
The array length is known with (width * height * depth). The buffer represent
image 's pixels RGBA.
:param target: Target buffer must have same length than source buffer)
:param width: integer; Source array's width (or width of the original image)
:param height: integer; source array's height (or height of the original image)
:return: Return a vertically flipped 1D RGBA buffer (swapped rows and columns of the 2d model)
"""
cdef:
int i, j, k, index, v
unsigned char [:] flipped_array = target
for i in prange(0, height * 4, 4):
for j in range(0, width):
index = i + (height * 4 * j)
v = (j * 4) + (i * width)
for k in range(4):
flipped_array[v + k] = <unsigned char>source[index + k]
return flipped_array
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef inline unsigned char [::1] vfb_c(
unsigned char [:] source, unsigned char [::1] target, int width, int height)nogil:
"""
Flip vertically the content (e.g alpha values) of an 1d buffer structure.
buffer representing an array type (w, h)
:param source: 1d buffer created from array type(w, h)
:param target: 1d buffer numpy.empty(ax_ * ay_, dtype=numpy.uint8) that will be the equivalent
of the source array but flipped vertically
:param width: source width
:param height: source height
:return: return 1d buffer (source array flipped)
"""
cdef:
int i, j
unsigned char [::1] flipped_array = target
for i in range(0, height, 1):
for j in range(0, width, 1):
flipped_array[j + (i * width)] = <unsigned char>source[i + (height * j)]
return flipped_array
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef blur5x5_buffer24_c(unsigned char [::1] rgb_buffer,
int width, int height, int depth, mask=None):
"""
Method using a C-buffer as input image (width * height * depth) uint8 data type
5 x5 Gaussian kernel used:
# |1 4 6 4 1|
# |4 16 24 16 4|
# |6 24 36 24 6| x 1/256
# |4 16 24 16 4|
# |1 4 6 4 1|
It uses convolution property to process the image in two passes (horizontal and vertical passes).
Pixels convoluted outside the image edges will be set to an adjacent pixel edge values
:param depth: integer; image depth (3)RGB, default 3
:param height: integer; image height
:param width: integer; image width
:param rgb_buffer: 1d buffer representing a 24bit format pygame.Surface
:return: 24-bit Pygame.Surface without per-pixel information and array
"""
cdef:
int b_length = len(rgb_buffer)
# check if the buffer length equal theoretical length
if b_length != (width * height * depth):
print("\nIncorrect 24-bit format image "
"expecting %s bytes got %s " % (b_length, width * height * depth))
# kernel 5x5 separable
cdef:
float [::1] kernel = \
numpy.array(([1.0/16.0,
4.0/16.0,
6.0/16.0,
4.0/16.0,
1.0/16.0]), dtype=numpy.float32, copy=False)
short int kernel_half = 2
int xx, yy, index, i, ii
float k, r, g, b
char kernel_offset
unsigned char red, green, blue
xyz v;
# convolve array contains pixels of the first pass(horizontal convolution)
# convolved array contains pixels of the second pass.
# buffer_ source pixels
unsigned char [::1] convolve = numpy.empty(width * height * depth, numpy.uint8)
unsigned char [::1] convolved = numpy.empty(width * height * depth, numpy.uint8)
unsigned char [:] buffer_ = rgb_buffer
with nogil:
# horizontal convolution
# goes through all RGB values of the buffer and apply the convolution
for i in prange(0, b_length, depth, schedule=METHOD, num_threads=THREADS):
r, g, b = 0, 0, 0
# v.x point to the row value of the equivalent 3d array (width, height, depth)
# v.y point to the column value ...
# v.z is always = 0 as the i value point always
# to the red color of a pixel in the C-buffer structure
v = to3d_c(i, width, depth)
# testing
# index = to1d_c(v.x, v.y, v.z, width, 3)
# print(v.x, v.y, v.z, i, index)
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
# Convert 1d indexing into a 3d indexing
# v.x correspond to the row index value in a 3d array
# v.x is always pointing to the red color of a pixel (see for i loop with
# step = 3) in the C-buffer data structure.
xx = v.x + kernel_offset
# avoid buffer overflow
if xx < 0 or xx > (width - 1):
red, green, blue = 0, 0, 0
else:
# Convert the 3d indexing into 1d buffer indexing
# The index value must always point to a red pixel
# v.z = 0
index = to1d_c(xx, v.y, v.z, width, depth)
# load the color value from the current pixel
red = buffer_[index]
green = buffer_[index + 1]
blue = buffer_[index + 2]
r = r + red * k
g = g + green * k
b = b + blue * k
# place the new RGB values into an empty array (convolve)
convolve[i ] = <unsigned char>r
convolve[i + 1] = <unsigned char>g
convolve[i + 2] = <unsigned char>b
# Vertical convolution
# In order to vertically convolve the kernel, we have to re-order the index value
# to fetch data vertically with the vmap_buffer function.
for i in prange(0, b_length, depth, schedule=METHOD, num_threads=THREADS):
index = vmap_buffer_c(i, width, height, 3)
r, g, b = 0, 0, 0
v = to3d_c(index, width, depth)
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
yy = v.y + kernel_offset
if yy < 0 or yy > (height-1):
red, green, blue = 0, 0, 0
else:
ii = to1d_c(v.x, yy, v.z, width, depth)
red, green, blue = convolve[ii],\
convolve[ii+1], convolve[ii+2]
r = r + red * k
g = g + green * k
b = b + blue * k
convolved[index ] = <unsigned char>r
convolved[index + 1] = <unsigned char>g
convolved[index + 2] = <unsigned char>b
return pygame.image.frombuffer(convolve, (width, height), "RGB"), convolve
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef blur5x5_buffer32_c(unsigned char [:] rgba_buffer,
int width, int height, int depth, mask=None):
"""
Method using a C-buffer as input image (width * height * depth) uint8 data type
5 x5 Gaussian kernel used:
# |1 4 6 4 1|
# |4 16 24 16 4|
# |6 24 36 24 6| x 1/256
# |4 16 24 16 4|
# |1 4 6 4 1|
It uses convolution property to process the image in two passes (horizontal and vertical passes).
Pixels convoluted outside the image edges will be set to an adjacent pixel edge values
:param depth: integer; image depth (3)RGB, default 3
:param height: integer; image height
:param width: integer; image width
:param rgba_buffer: 1d buffer representing a 24bit format pygame.Surface
:return: 24-bit Pygame.Surface without per-pixel information and array
"""
cdef:
int b_length= len(rgba_buffer)
# check if the buffer length equal theoretical length
if b_length != (width * height * depth):
raise ValueError(
"\nIncorrect 32-bit format image, "
"expecting %s got %s " % (width * height * depth, b_length))
# kernel 5x5 separable
cdef:
float [::1] kernel = \
numpy.array(([1.0/16.0,
4.0/16.0,
6.0/16.0,
4.0/16.0,
1.0/16.0]), dtype=numpy.float32, copy=False)
short int kernel_half = 2
int xx, yy, index, i, ii
float k, r, g, b
char kernel_offset
unsigned char red, green, blue
xyz v;
# convolve array contains pixels of the first pass(horizontal convolution)
# convolved array contains pixels of the second pass.
# buffer_ source pixels
unsigned char [:] convolve = numpy.empty(width * height * depth, numpy.uint8)
unsigned char [:] convolved = numpy.empty(width * height * depth, numpy.uint8)
unsigned char [:] buffer_ = numpy.frombuffer(rgba_buffer, numpy.uint8)
with nogil:
# horizontal convolution
# goes through all RGB values of the buffer and apply the convolution
for i in prange(0, b_length, depth, schedule=METHOD, num_threads=THREADS):
r, g, b = 0, 0, 0
# v.x point to the row value of the equivalent 3d array (width, height, depth)
# v.y point to the column value ...
# v.z is always = 0 as the i value point always
# to the red color of a pixel in the C-buffer structure
v = to3d_c(i, width, depth)
# testing
# index = to1d_c(v.x, v.y, v.z, width, 4)
# print(v.x, v.y, v.z, i, index)
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
# Convert 1d indexing into a 3d indexing
# v.x correspond to the row index value in a 3d array
# v.x is always pointing to the red color of a pixel (see for i loop with
# step = 4) in the C-buffer data structure.
xx = v.x + kernel_offset
# avoid buffer overflow
if xx < 0 or xx > (width - 1):
red, green, blue = 0, 0, 0
else:
# Convert the 3d indexing into 1d buffer indexing
# The index value must always point to a red pixel
# v.z = 0
index = to1d_c(xx, v.y, v.z, width, depth)
# load the color value from the current pixel
red = buffer_[index ]
green = buffer_[index + 1]
blue = buffer_[index + 2]
r = r + red * k
g = g + green * k
b = b + blue * k
# place the new RGB values into an empty array (convolve)
convolve[i ] = <unsigned char>r
convolve[i + 1] = <unsigned char>g
convolve[i + 2] = <unsigned char>b
convolve[i + 3] = buffer_[i + 3]
# Vertical convolution
# In order to vertically convolve the kernel, we have to re-order the index value
# to fetch data vertically with the vmap_buffer function.
for i in prange(0, b_length, depth, schedule=METHOD, num_threads=THREADS):
index = vmap_buffer_c(i, width, height, depth)
r, g, b = 0, 0, 0
v = to3d_c(index, width, depth)
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
yy = v.y + kernel_offset
if yy < 0 or yy > (height-1):
red, green, blue = 0, 0, 0
else:
ii = to1d_c(v.x, yy, v.z, width, depth)
red, green, blue = convolve[ii],\
convolve[ii+1], convolve[ii+2]
r = r + red * k
g = g + green * k
b = b + blue * k
convolved[index ] = <unsigned char>r
convolved[index + 1] = <unsigned char>g
convolved[index + 2] = <unsigned char>b
convolved[index + 3] = buffer_[index + 3]
return pygame.image.frombuffer(convolved, (width, height), "RGBA"), convolved
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef unsigned char [:, :, ::1] blur5x5_array24_c(unsigned char [:, :, :] rgb_array_, mask=None):
"""
# Gaussian kernel 5x5
# |1 4 6 4 1|
# |4 16 24 16 4|
# |6 24 36 24 6| x 1/256
# |4 16 24 16 4|
# |1 4 6 4 1|
This method is using convolution property and process the image in two passes,
first the horizontal convolution and last the vertical convolution
pixels convoluted outside image edges will be set to adjacent edge value
:param rgb_array_: numpy.ndarray type (w, h, 3) uint8
:return: Return 24-bit a numpy.ndarray type (w, h, 3) uint8
"""
cdef int w, h, dim
try:
w, h, dim = (<object>rgb_array_).shape[:3]
except (ValueError, pygame.error) as e:
raise ValueError('\nArray shape not understood.')
# kernel_ = numpy.array(([1.0 / 16.0,
# 4.0 / 16.0,
# 6.0 / 16.0,
# 4.0 / 16.0,
# 1.0 / 16.0]), dtype=float32, copy=False)
# kernel 5x5 separable
cdef:
# float [::1] kernel = kernel_
float[5] kernel = [1.0/16.0, 4.0/16.0, 6.0/16.0, 4.0/16.0, 1.0/16.0]
short int kernel_half = 2
unsigned char [:, :, ::1] convolve = numpy.empty((w, h, 3), dtype=uint8)
unsigned char [:, :, ::1] convolved = numpy.empty((w, h, 3), dtype=uint8)
short int kernel_length = len(kernel)
int x, y, xx, yy
float k, r, g, b, s
char kernel_offset
unsigned char red, green, blue
with nogil:
# horizontal convolution
for y in prange(0, h, schedule=METHOD, num_threads=THREADS): # range [0..h-1)
for x in range(0, w): # range [0..w-1]
r, g, b = 0, 0, 0
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
xx = x + kernel_offset
# check boundaries.
# Fetch the edge pixel for the convolution
if xx < 0:
red, green, blue = rgb_array_[0, y, 0],\
rgb_array_[0, y, 1], rgb_array_[0, y, 2]
elif xx > (w - 1):
red, green, blue = rgb_array_[w-1, y, 0],\
rgb_array_[w-1, y, 1], rgb_array_[w-1, y, 2]
else:
red, green, blue = rgb_array_[xx, y, 0],\
rgb_array_[xx, y, 1], rgb_array_[xx, y, 2]
r = r + red * k
g = g + green * k
b = b + blue * k
convolve[x, y, 0], convolve[x, y, 1], convolve[x, y, 2] = <unsigned char>r,\
<unsigned char>g, <unsigned char>b
# Vertical convolution
for x in prange(0, w, schedule=METHOD, num_threads=THREADS):
for y in range(0, h):
r, g, b = 0, 0, 0
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
yy = y + kernel_offset
if yy < 0:
red, green, blue = convolve[x, 0, 0],\
convolve[x, 0, 1], convolve[x, 0, 2]
elif yy > (h -1):
red, green, blue = convolve[x, h-1, 0],\
convolve[x, h-1, 1], convolve[x, h-1, 2]
else:
red, green, blue = convolve[x, yy, 0],\
convolve[x, yy, 1], convolve[x, yy, 2]
r = r + red * k
g = g + green * k
b = b + blue * k
convolved[x, y, 0], convolved[x, y, 1], convolved[x, y, 2] = \
<unsigned char>r, <unsigned char>g, <unsigned char>b
return convolved
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef unsigned char [:, :, ::1] blur5x5_array32_c(unsigned char [:, :, :] rgb_array_, mask=None):
"""
# Gaussian kernel 5x5
# |1 4 6 4 1|
# |4 16 24 16 4|
# |6 24 36 24 6| x 1/256
# |4 16 24 16 4|
# |1 4 6 4 1|
This method is using convolution property and process the image in two passes,
first the horizontal convolution and last the vertical convolution
pixels convoluted outside image edges will be set to adjacent edge value
:param rgb_array_: 3d numpy.ndarray type (w, h, 4) uint8, RGBA values
:return: Return a numpy.ndarray type (w, h, 4) uint8
"""
cdef int w, h, dim
try:
w, h, dim = rgb_array_.shape[:3]
except (ValueError, pygame.error) as e:
raise ValueError('\nArray shape not understood.')
# kernel_ = numpy.array(([1.0 / 16.0,
# 4.0 / 16.0,
# 6.0 / 16.0,
# 4.0 / 16.0,
# 1.0 / 16.0]), dtype=float32, copy=False)
# kernel 5x5 separable
cdef:
# float [::1] kernel = kernel_
float[5] kernel = [1.0/16.0, 4.0/16.0, 6.0/16.0, 4.0/16.0, 1.0/16.0]
short int kernel_half = 2
unsigned char [:, :, ::1] convolve = numpy.empty((w, h, 3), dtype=uint8)
unsigned char [:, :, ::1] convolved = numpy.empty((w, h, 4), dtype=uint8)
short int kernel_length = len(kernel)
int x, y, xx, yy
float k, r, g, b
char kernel_offset
unsigned char red, green, blue
with nogil:
# horizontal convolution
for y in prange(0, h, schedule=METHOD, num_threads=THREADS):
for x in range(0, w):
r, g, b = 0, 0, 0
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
xx = x + kernel_offset
# check boundaries.
# Fetch the edge pixel for the convolution
if xx < 0:
red, green, blue = rgb_array_[0, y, 0],\
rgb_array_[0, y, 1], rgb_array_[0, y, 2]
elif xx > (w - 1):
red, green, blue = rgb_array_[w-1, y, 0],\
rgb_array_[w-1, y, 1], rgb_array_[w-1, y, 2]
else:
red, green, blue = rgb_array_[xx, y, 0],\
rgb_array_[xx, y, 1], rgb_array_[xx, y, 2]
r = r + red * k
g = g + green * k
b = b + blue * k
convolve[x, y, 0], convolve[x, y, 1], convolve[x, y, 2] = <unsigned char>r,\
<unsigned char>g, <unsigned char>b
# Vertical convolution
for x in prange(0, w, schedule=METHOD, num_threads=THREADS):
for y in range(0, h):
r, g, b = 0, 0, 0
for kernel_offset in range(-kernel_half, kernel_half + 1):
k = kernel[kernel_offset + kernel_half]
yy = y + kernel_offset
if yy < 0:
red, green, blue = convolve[x, 0, 0],\
convolve[x, 0, 1], convolve[x, 0, 2]
elif yy > (h -1):
red, green, blue = convolve[x, h-1, 0],\
convolve[x, h-1, 1], convolve[x, h-1, 2]
else:
red, green, blue = convolve[x, yy, 0],\
convolve[x, yy, 1], convolve[x, yy, 2]
r = r + red * k
g = g + green * k
b = b + blue * k
convolved[x, y, 0], convolved[x, y, 1],\
convolved[x, y, 2], convolved[x, y, 3] = \
<unsigned char>r, <unsigned char>g, <unsigned char>b, rgb_array_[x, y, 3]
return convolved
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef bpf24_c(image, int threshold = 128, bint transpose=False):
"""
Bright pass filter compatible 24-bit
Bright pass filter for 24bit image (method using 3d array data structure)
Calculate the luminance of every pixels and applied an attenuation c = lum2 / lum
with lum2 = max(lum - threshold, 0) and
lum = rgb[i, j, 0] * 0.299 + rgb[i, j, 1] * 0.587 + rgb[i, j, 2] * 0.114
The output image will keep only bright area. You can adjust the threshold value
default 128 in order to get the desire changes.
:param transpose: Transpose the final array (width and height are transpose if True)
:param image: pygame.Surface 24 bit format (RGB) without per-pixel information
:param threshold: integer; Threshold to consider for filtering pixels luminance values,
default is 128 range [0..255] unsigned char (python integer)
:return: Return a Pygame Surface and a 3d numpy.ndarray format (w, h, 3)
(only bright area of the image remains).
"""
# Fallback to default threshold value if argument
# threshold value is incorrect
if 0 > threshold > 255:
printf("\nArgument threshold must be in range [0...255], fallback to default value 128.")
threshold = 128
assert isinstance(image, pygame.Surface), \
"\nExpecting pygame surface for argument image, got %s " % type(image)
# make sure the surface is 24-bit format RGB
if not image.get_bitsize() == 24:
raise ValueError('Surface is not 24-bit format.')
try:
rgb_array = pygame.surfarray.pixels3d(image)
except (pygame.error, ValueError):
raise ValueError('\nInvalid surface.')
cdef:
int w, h
w, h = rgb_array.shape[:2]