@@ -995,7 +995,8 @@ flood_fill(PyObject *self, PyObject *arg, PyObject *kwargs)
995
995
int startx , starty ;
996
996
Uint32 color ;
997
997
SDL_Surface * pattern = NULL ;
998
- SDL_bool did_lock = SDL_FALSE ;
998
+ SDL_bool did_lock = SDL_FALSE ;
999
+ int flood_fill_result ;
999
1000
1000
1001
int drawn_area [4 ] = {INT_MAX , INT_MAX , INT_MIN ,
1001
1002
INT_MIN }; /* Used to store bounding box values */
@@ -1016,47 +1017,50 @@ flood_fill(PyObject *self, PyObject *arg, PyObject *kwargs)
1016
1017
PG_SURF_BytesPerPixel (surf ));
1017
1018
}
1018
1019
1019
- if (pgSurface_Check (colorobj )){
1020
- pat_surfobj = ((pgSurfaceObject * ) colorobj );
1020
+ if (pgSurface_Check (colorobj )) {
1021
+ pat_surfobj = ((pgSurfaceObject * )colorobj );
1021
1022
1022
- pattern = SDL_ConvertSurface
1023
- (pat_surfobj -> surf , surf -> format , 0 );
1023
+ pattern = SDL_ConvertSurface (pat_surfobj -> surf , surf -> format , 0 );
1024
1024
1025
- if (pattern == NULL ){
1025
+ if (pattern == NULL ) {
1026
1026
return RAISE (PyExc_RuntimeError , "error converting pattern surf" );
1027
1027
}
1028
1028
1029
- SDL_SetSurfaceRLE (pattern ,
1030
- SDL_FALSE );
1029
+ SDL_SetSurfaceRLE (pattern , SDL_FALSE );
1031
1030
1032
- color = 0 ;
1033
- } else {
1031
+ color = 0 ;
1032
+ }
1033
+ else {
1034
1034
CHECK_LOAD_COLOR (colorobj );
1035
1035
}
1036
1036
1037
1037
if (!pg_TwoIntsFromObj (start , & startx , & starty )) {
1038
1038
return RAISE (PyExc_TypeError , "invalid start_pos argument" );
1039
1039
}
1040
1040
1041
- if (SDL_MUSTLOCK (surf )){
1042
- did_lock = SDL_TRUE ;
1041
+ if (SDL_MUSTLOCK (surf )) {
1042
+ did_lock = SDL_TRUE ;
1043
1043
if (!pgSurface_Lock (surfobj )) {
1044
1044
return RAISE (PyExc_RuntimeError , "error locking surface" );
1045
1045
}
1046
1046
}
1047
1047
1048
- flood_fill_inner (surf , startx , starty , color , pattern , drawn_area );
1048
+ flood_fill_result = flood_fill_inner (surf , startx , starty , color , pattern , drawn_area );
1049
1049
1050
- if (pattern != NULL ){
1050
+ if (pattern != NULL ) {
1051
1051
SDL_FreeSurface (pattern );
1052
1052
}
1053
1053
1054
- if (did_lock ){
1054
+ if (did_lock ) {
1055
1055
if (!pgSurface_Unlock (surfobj )) {
1056
1056
return RAISE (PyExc_RuntimeError , "error unlocking surface" );
1057
1057
}
1058
1058
}
1059
1059
1060
+ if (flood_fill_result == -1 ){
1061
+ return RAISE (PyExc_RuntimeError , "flood fill allocation fail" );
1062
+ }
1063
+
1060
1064
/* Compute return rect. */
1061
1065
if (drawn_area [0 ] != INT_MAX && drawn_area [1 ] != INT_MAX &&
1062
1066
drawn_area [2 ] != INT_MIN && drawn_area [3 ] != INT_MIN )
@@ -1079,20 +1083,25 @@ swap(float *a, float *b)
1079
1083
1080
1084
#define WORD_BITS (8 * sizeof(unsigned int))
1081
1085
1082
- struct point2d {
1086
+ struct point2d {
1083
1087
Uint32 x ;
1084
1088
Uint32 y ;
1085
1089
};
1086
1090
1087
- static inline void _bitarray_set (unsigned int * bitarray , size_t idx , SDL_bool value ){
1088
- if (value ){
1091
+ static inline void
1092
+ _bitarray_set (unsigned int * bitarray , size_t idx , SDL_bool value )
1093
+ {
1094
+ if (value ) {
1089
1095
bitarray [idx / WORD_BITS ] |= (1 << (idx % WORD_BITS ));
1090
- } else {
1096
+ }
1097
+ else {
1091
1098
bitarray [idx / WORD_BITS ] &= (~(1 ) << (idx % WORD_BITS ));
1092
1099
}
1093
1100
}
1094
1101
1095
- static inline SDL_bool _bitarray_get (unsigned int * bitarray , size_t idx ) {
1102
+ static inline SDL_bool
1103
+ _bitarray_get (unsigned int * bitarray , size_t idx )
1104
+ {
1096
1105
if (bitarray [idx / WORD_BITS ] & (1 << (idx % WORD_BITS )))
1097
1106
return SDL_TRUE ;
1098
1107
else
@@ -1786,42 +1795,44 @@ draw_line(SDL_Surface *surf, int x1, int y1, int x2, int y2, Uint32 color,
1786
1795
1787
1796
1788
1797
static int
1789
- flood_fill_inner (SDL_Surface * surf , int x1 , int y1 , Uint32 new_color , SDL_Surface * pattern , int * drawn_area )
1798
+ flood_fill_inner (SDL_Surface * surf , int x1 , int y1 , Uint32 new_color ,
1799
+ SDL_Surface * pattern , int * drawn_area )
1790
1800
{
1791
1801
SDL_Rect cliprect ;
1792
1802
1793
1803
SDL_GetClipRect (surf , & cliprect );
1794
- size_t frontier_bufsize = 8 , frontier_size = 1 , next_frontier_size = 0 ;
1804
+ size_t frontier_bufsize = 8 , frontier_size = 1 , next_frontier_size = 0 ;
1795
1805
1796
- struct point2d * frontier = malloc (frontier_bufsize * sizeof (struct point2d ));
1797
- if (frontier == NULL ){
1806
+ struct point2d * frontier =
1807
+ malloc (frontier_bufsize * sizeof (struct point2d ));
1808
+ if (frontier == NULL ) {
1798
1809
return -1 ;
1799
1810
}
1800
1811
1801
- struct point2d * frontier_next = malloc (frontier_bufsize * sizeof (struct point2d ));
1812
+ struct point2d * frontier_next =
1813
+ malloc (frontier_bufsize * sizeof (struct point2d ));
1802
1814
1803
- if (frontier_next == NULL ){
1815
+ if (frontier_next == NULL ) {
1804
1816
free (frontier );
1805
1817
return -1 ;
1806
1818
}
1807
- size_t mask_size = cliprect .w * cliprect .h ;
1819
+ size_t mask_size = cliprect .w * cliprect .h ;
1808
1820
1809
1821
unsigned int * mask = calloc ((mask_size ) / 8 + 1 , sizeof (unsigned int ));
1810
- if (mask == NULL ){
1822
+ if (mask == NULL ) {
1811
1823
free (frontier );
1812
1824
free (frontier_next );
1813
1825
free (mask );
1814
1826
return -1 ;
1815
1827
}
1816
- Uint32 old_color = 0 ;
1828
+ Uint32 old_color = 0 ;
1817
1829
Uint8 * pix ;
1818
1830
1819
- int VN_X []= {0 ,0 , 1 , -1 };
1820
- int VN_Y []= {1 ,-1 ,0 , 0 };
1831
+ int VN_X [] = {0 , 0 , 1 , -1 };
1832
+ int VN_Y [] = {1 , -1 , 0 , 0 };
1821
1833
1822
-
1823
- if (!(x1 >=cliprect .x && x1 < (cliprect .x + cliprect .w )
1824
- && y1 >=cliprect .y && y1 < (cliprect .y + cliprect .h ))) {
1834
+ if (!(x1 >= cliprect .x && x1 < (cliprect .x + cliprect .w ) &&
1835
+ y1 >= cliprect .y && y1 < (cliprect .y + cliprect .h ))) {
1825
1836
free (frontier );
1826
1837
free (mask );
1827
1838
return 0 ;
@@ -1830,99 +1841,99 @@ flood_fill_inner(SDL_Surface *surf, int x1, int y1, Uint32 new_color, SDL_Surfac
1830
1841
SURF_GET_AT (old_color , surf , x1 , y1 , (Uint8 * )surf -> pixels , surf -> format ,
1831
1842
pix );
1832
1843
1833
- if (pattern == NULL && old_color == new_color ){
1844
+ if (pattern == NULL && old_color == new_color ) {
1834
1845
free (frontier );
1835
1846
free (mask );
1836
1847
free (frontier_next );
1837
1848
return 0 ;
1838
1849
}
1839
1850
1840
- frontier [0 ].x = x1 ;
1841
- frontier [0 ].y = y1 ;
1851
+ frontier [0 ].x = x1 ;
1852
+ frontier [0 ].y = y1 ;
1842
1853
1843
- while (frontier_size != 0 ) {
1844
- next_frontier_size = 0 ;
1854
+ while (frontier_size != 0 ) {
1855
+ next_frontier_size = 0 ;
1845
1856
1846
- for (size_t i = 0 ; i < frontier_size ; i ++ ){
1847
- unsigned int x = frontier [i ].x ;
1848
- unsigned int y = frontier [i ].y ;
1849
- size_t mask_idx = (y - cliprect .y )* cliprect .w + ( x - cliprect .x );
1857
+ for (size_t i = 0 ; i < frontier_size ; i ++ ) {
1858
+ unsigned int x = frontier [i ].x ;
1859
+ unsigned int y = frontier [i ].y ;
1860
+ size_t mask_idx = (y - cliprect .y ) * cliprect .w + ( x - cliprect .x );
1850
1861
1851
- Uint32 current_color = 0 ;
1862
+ Uint32 current_color = 0 ;
1852
1863
1853
1864
_bitarray_set (mask , mask_idx , SDL_TRUE );
1854
1865
1855
- SURF_GET_AT (current_color , surf , x , y ,
1856
- (Uint8 * )surf -> pixels , surf -> format ,
1857
- pix );
1866
+ SURF_GET_AT (current_color , surf , x , y , (Uint8 * )surf -> pixels ,
1867
+ surf -> format , pix );
1858
1868
1859
- if (current_color != old_color ){
1869
+ if (current_color != old_color ) {
1860
1870
continue ;
1861
1871
}
1862
1872
1863
- if (pattern != NULL ){
1864
- SURF_GET_AT (new_color , pattern , x %pattern -> w , y %pattern -> h ,
1865
- (Uint8 * )pattern -> pixels , pattern -> format ,
1866
- pix );
1873
+ if (pattern != NULL ) {
1874
+ SURF_GET_AT (new_color , pattern , x % pattern -> w , y % pattern -> h ,
1875
+ (Uint8 * )pattern -> pixels , pattern -> format , pix );
1867
1876
}
1868
1877
1869
1878
unsafe_set_at (surf , x , y , new_color );
1870
1879
add_pixel_to_drawn_list (x , y , drawn_area );
1871
1880
1872
- for (int n = 0 ; n < 4 ; n ++ ){
1873
- long nx = x + VN_X [n ];
1874
- long ny = y + VN_Y [n ];
1881
+ for (int n = 0 ; n < 4 ; n ++ ) {
1882
+ long nx = x + VN_X [n ];
1883
+ long ny = y + VN_Y [n ];
1875
1884
SDL_bool found_in_frontier = SDL_FALSE ;
1876
1885
1877
- if (!(nx >= cliprect .x && nx < cliprect .x + cliprect .w
1878
- && ny >= cliprect .y && ny < cliprect .y + cliprect .h )) {
1886
+ if (!(nx >= cliprect .x && nx < cliprect .x + cliprect .w &&
1887
+ ny >= cliprect .y && ny < cliprect .y + cliprect .h )) {
1879
1888
continue ;
1880
1889
}
1881
1890
1882
- mask_idx = (ny - cliprect .y )* cliprect .w + (nx - cliprect .x );
1891
+ mask_idx = (ny - cliprect .y ) * cliprect .w + (nx - cliprect .x );
1883
1892
if (_bitarray_get (mask , mask_idx ))
1884
1893
continue ;
1885
1894
1886
1895
_bitarray_set (mask , mask_idx , SDL_TRUE );
1887
- if (found_in_frontier ){
1896
+ if (found_in_frontier ) {
1888
1897
continue ;
1889
1898
}
1890
1899
1891
- if (next_frontier_size == frontier_bufsize ){
1900
+ if (next_frontier_size == frontier_bufsize ) {
1892
1901
struct point2d * old_buf = frontier_next ;
1893
1902
1894
- frontier_bufsize *= 4 ;
1903
+ frontier_bufsize *= 4 ;
1895
1904
1896
- frontier_next = realloc (frontier_next , frontier_bufsize * sizeof (struct point2d ));
1897
- if (frontier_next == NULL ){
1905
+ frontier_next =
1906
+ realloc (frontier_next ,
1907
+ frontier_bufsize * sizeof (struct point2d ));
1908
+ if (frontier_next == NULL ) {
1898
1909
free (mask );
1899
1910
free (frontier );
1900
1911
free (old_buf );
1901
1912
return -1 ;
1902
1913
}
1903
1914
1904
1915
old_buf = frontier ;
1905
- frontier = realloc (frontier , frontier_bufsize * sizeof (struct point2d ));
1906
- if (frontier == NULL ){
1916
+ frontier = realloc (
1917
+ frontier , frontier_bufsize * sizeof (struct point2d ));
1918
+ if (frontier == NULL ) {
1907
1919
free (old_buf );
1908
1920
free (mask );
1909
1921
free (frontier_next );
1910
1922
return -1 ;
1911
1923
}
1912
1924
}
1913
1925
1914
- frontier_next [next_frontier_size ].x = nx ;
1915
- frontier_next [next_frontier_size ].y = ny ;
1926
+ frontier_next [next_frontier_size ].x = nx ;
1927
+ frontier_next [next_frontier_size ].y = ny ;
1916
1928
next_frontier_size ++ ;
1917
-
1918
1929
}
1919
1930
}
1920
1931
struct point2d * temp_buf ;
1921
- temp_buf = frontier ;
1922
- frontier = frontier_next ;
1923
- frontier_next = temp_buf ;
1932
+ temp_buf = frontier ;
1933
+ frontier = frontier_next ;
1934
+ frontier_next = temp_buf ;
1924
1935
1925
- frontier_size = next_frontier_size ;
1936
+ frontier_size = next_frontier_size ;
1926
1937
}
1927
1938
free (frontier );
1928
1939
free (mask );
@@ -3134,7 +3145,7 @@ static PyMethodDef _draw_methods[] = {
3134
3145
{"ellipse" , (PyCFunction )ellipse , METH_VARARGS | METH_KEYWORDS ,
3135
3146
DOC_DRAW_ELLIPSE },
3136
3147
{"flood_fill" , (PyCFunction )flood_fill , METH_VARARGS | METH_KEYWORDS ,
3137
- "" },
3148
+ DOC_DRAW_FLOODFILL },
3138
3149
{"arc" , (PyCFunction )arc , METH_VARARGS | METH_KEYWORDS , DOC_DRAW_ARC },
3139
3150
{"circle" , (PyCFunction )circle , METH_VARARGS | METH_KEYWORDS ,
3140
3151
DOC_DRAW_CIRCLE },
0 commit comments