@@ -42,11 +42,17 @@ static bool check_bounds(MCImageBitmap *p_bitmap, int32_t x, int32_t y, uint32_t
4242
4343static void clamp_region (MCImageBitmap *p_bitmap, int32_t &x, int32_t &y, uint32_t &width, uint32_t &height)
4444{
45- x = MCMax (0 , x);
46- y = MCMax (0 , y);
45+ int32_t t_x1, t_y1, t_x2, t_y2;
4746
48- width = MCMin ((int32_t )p_bitmap->width - x, (int32_t )width);
49- height = MCMin ((int32_t )p_bitmap->height - y, (int32_t )height);
47+ t_x1 = MCMax (0 , MCMin ((int32_t )p_bitmap->width , x));
48+ t_y1 = MCMax (0 , MCMin ((int32_t )p_bitmap->height , y));
49+ t_x2 = MCMax (0 , MCMin ((int32_t )p_bitmap->width , x + (int32_t )width));
50+ t_y2 = MCMax (0 , MCMin ((int32_t )p_bitmap->height , y + (int32_t )height));
51+
52+ x = t_x1;
53+ y = t_y1;
54+ width = t_x2 - t_x1;
55+ height = t_y2 - t_y1;
5056}
5157
5258bool MCImageBitmapCreate (uindex_t p_width, uindex_t p_height, MCImageBitmap *&r_bitmap)
@@ -173,17 +179,20 @@ void MCImageBitmapCopyRegionToBitmap(MCImageBitmap *p_dst, MCImageBitmap *p_src,
173179 int32_t t_dst_x, t_dst_y;
174180 uint32_t t_width, t_height;
175181
176- t_src_x = MCMax (0 , p_src_rect.x );
177- t_src_y = MCMax (0 , p_src_rect.y );
182+ t_src_x = p_src_rect.x ;
183+ t_src_y = p_src_rect.y ;
184+
185+ t_width = p_src_rect.width ;
186+ t_height = p_src_rect.height ;
178187
179- t_dst_x = MCMax ( 0 , p_dst_offset.x ) ;
180- t_dst_y = MCMax ( 0 , p_dst_offset.y ) ;
188+ t_dst_x = p_dst_offset.x ;
189+ t_dst_y = p_dst_offset.y ;
181190
182- t_width = MCMin (( int32_t ) p_src-> width - t_src_x, ( int32_t )p_src_rect. width );
183- t_height = MCMin (( int32_t )p_src-> height - t_src_y, ( int32_t )p_src_rect. height );
191+ clamp_region ( p_src, t_src_x, t_src_y, t_width, t_height );
192+ clamp_region (p_dst, t_dst_x, t_dst_y, t_width, t_height );
184193
185- t_width = MCMin (( int32_t )p_dst-> width - t_dst_x, ( int32_t )t_width);
186- t_height = MCMin (( int32_t )p_dst-> height - t_dst_y, ( int32_t )t_height) ;
194+ if ( t_width == 0 || t_height == 0 )
195+ return ;
187196
188197 copy_bitmap_data ((uint8_t *)p_dst->data + p_dst->stride * t_dst_y + t_dst_x * sizeof (uint32_t ), p_dst->stride , (uint8_t *)p_src->data + p_src->stride * t_src_y + t_src_x * sizeof (uint32_t ), p_src->stride , t_width, t_height);
189198
0 commit comments