@@ -2169,6 +2169,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
21692169 int x_dest = 0 , y_dest = 0 ; /* Default destination coordinates. */
21702170 Uint8 dflt_setcolor [] = {255 , 255 , 255 , 255 }; /* Default set color. */
21712171 Uint8 dflt_unsetcolor [] = {0 , 0 , 0 , 255 }; /* Default unset color. */
2172+ int create_area_bitmask = 0 ;
21722173
21732174 static char * keywords [] = {"surface" , "setsurface" , "unsetsurface" ,
21742175 "setcolor" , "unsetcolor" , "dest" ,
@@ -2210,6 +2211,28 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
22102211 PyErr_SetString (PyExc_TypeError , "invalid rectstyle argument" );
22112212 goto to_surface_error ;
22122213 }
2214+
2215+ memcpy (& temp_rect , area_rect , sizeof (temp_rect ));
2216+ area_rect = & temp_rect ;
2217+
2218+ pgRect_Normalize (area_rect );
2219+
2220+ if (area_rect -> x < 0 ) {
2221+ // x_dest -= area_rect->x;
2222+ area_rect -> w += area_rect -> x ;
2223+ area_rect -> x = 0 ;
2224+ }
2225+ if (area_rect -> y < 0 ) {
2226+ // y_dest -= area_rect->y;
2227+ area_rect -> h += area_rect -> y ;
2228+ area_rect -> y = 0 ;
2229+ }
2230+
2231+ // clamp rect width and height to not stick out of the mask
2232+ area_rect -> w = MAX (MIN (area_rect -> w , bitmask -> w - area_rect -> x ), 0 );
2233+ area_rect -> h = MAX (MIN (area_rect -> h , bitmask -> h - area_rect -> y ), 0 );
2234+
2235+ create_area_bitmask = area_rect -> w > 0 && area_rect -> h > 0 ;
22132236 }
22142237 else {
22152238 temp_rect .x = temp_rect .y = 0 ;
@@ -2218,22 +2241,6 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
22182241 area_rect = & temp_rect ;
22192242 }
22202243
2221- if (area_rect -> x < 0 ) {
2222- // x_dest -= area_rect->x;
2223- area_rect -> w += area_rect -> x ;
2224- area_rect -> x = 0 ;
2225- }
2226- if (area_rect -> y < 0 ) {
2227- // y_dest -= area_rect->y;
2228- area_rect -> h += area_rect -> y ;
2229- area_rect -> y = 0 ;
2230- }
2231-
2232- // clamp rect width and height to not stick out of the mask
2233- area_rect -> w = MAX (MIN (area_rect -> w , bitmask -> w - area_rect -> x ), 0 );
2234- area_rect -> h = MAX (MIN (area_rect -> h , bitmask -> h - area_rect -> y ), 0 );
2235- // pgRect_Normalize(area_rect);
2236-
22372244 if (Py_None == surfobj ) {
22382245 surfobj = PyObject_CallFunction ((PyObject * )& pgSurface_Type , "(ii)ii" ,
22392246 area_rect -> w , area_rect -> h ,
@@ -2341,8 +2348,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
23412348 goto to_surface_error ;
23422349 }
23432350
2344- if (areaobj ) {
2345- assert (area_rect -> w >= 0 && area_rect -> w >= 0 );
2351+ if (create_area_bitmask ) {
23462352 area_bitmask = bitmask_create (area_rect -> w , area_rect -> h );
23472353 if (NULL == area_bitmask ) {
23482354 PyErr_Format (PyExc_MemoryError , "failed to allocate memory for a mask" );
@@ -2359,6 +2365,35 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
23592365
23602366 bitmask_overlap_mask (bitmask , overlap_bitmask , area_bitmask ,
23612367 area_rect -> x , area_rect -> y );
2368+
2369+ printf ("%i,%i\n" , area_rect -> x , area_rect -> y );
2370+ printf ("%ix%i\n" , bitmask -> w , bitmask -> h );
2371+ for (int y = 0 ; y < bitmask -> h ; ++ y ) {
2372+ for (int x = 0 ; x < bitmask -> w ; ++ x ) {
2373+ int bit_value = (bitmask -> bits [y ] >> x ) & 1 ;
2374+ printf ("%d " , bit_value );
2375+ }
2376+ printf ("\n" );
2377+ }
2378+ printf ("\n" );
2379+ printf ("overlap\n" );
2380+ for (int y = 0 ; y < overlap_bitmask -> h ; ++ y ) {
2381+ for (int x = 0 ; x < overlap_bitmask -> w ; ++ x ) {
2382+ int bit_value = (overlap_bitmask -> bits [y ] >> x ) & 1 ;
2383+ printf ("%d " , bit_value );
2384+ }
2385+ printf ("\n" );
2386+ }
2387+ printf ("\n%ix%i\n" , area_rect -> w , area_rect -> h );
2388+ for (int y = 0 ; y < area_bitmask -> h ; ++ y ) {
2389+ for (int x = 0 ; x < area_bitmask -> w ; ++ x ) {
2390+ int bit_value = (area_bitmask -> bits [y ] >> x ) & 1 ;
2391+ printf ("%d " , bit_value );
2392+ }
2393+ printf ("\n" );
2394+ }
2395+ printf ("\n" );
2396+
23622397 bitmask_free (overlap_bitmask );
23632398 }
23642399 else {
@@ -2373,7 +2408,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs)
23732408
23742409 Py_END_ALLOW_THREADS ; /* Obtain the GIL. */
23752410
2376- if (areaobj ) {
2411+ if (create_area_bitmask ) {
23772412 bitmask_free (area_bitmask );
23782413 }
23792414
0 commit comments