Skip to content

Commit e2233ac

Browse files
feat: Optimize ImageClearBackground and ImageDrawRectangleRec with doubling strategy (#5363)
1 parent 3f92c39 commit e2233ac

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/rtextures.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,11 +3341,14 @@ void ImageClearBackground(Image *dst, Color color)
33413341

33423342
unsigned char *pSrcPixel = (unsigned char *)dst->data;
33433343
int bytesPerPixel = GetPixelDataSize(1, 1, dst->format);
3344+
int totalPixels = dst->width * dst->height;
33443345

3345-
// Repeat the first pixel data throughout the image
3346-
for (int i = 1; i < dst->width*dst->height; i++)
3346+
// Repeat the first pixel data throughout the image,
3347+
// doubling the pixels copied on each iteration
3348+
for (int i = 1; i < totalPixels; i *= 2)
33473349
{
3348-
memcpy(pSrcPixel + i*bytesPerPixel, pSrcPixel, bytesPerPixel);
3350+
int pixelsToCopy = MIN(i, totalPixels - i);
3351+
memcpy(pSrcPixel + i * bytesPerPixel, pSrcPixel, pixelsToCopy * bytesPerPixel);
33493352
}
33503353
}
33513354

@@ -3724,9 +3727,10 @@ void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color)
37243727
unsigned char *pSrcPixel = (unsigned char *)dst->data + bytesOffset;
37253728

37263729
// Repeat the first pixel data throughout the row
3727-
for (int x = 1; x < (int)rec.width; x++)
3730+
for (int x = 1; x < (int)rec.width; x *= 2)
37283731
{
3729-
memcpy(pSrcPixel + x*bytesPerPixel, pSrcPixel, bytesPerPixel);
3732+
int pixelsToCopy = MIN(x, (int)rec.width - x);
3733+
memcpy(pSrcPixel + x*bytesPerPixel, pSrcPixel, pixelsToCopy * bytesPerPixel);
37303734
}
37313735

37323736
// Repeat the first row data for all other rows

0 commit comments

Comments
 (0)