Skip to content

Commit

Permalink
Optimization of ImageDrawRectangleRec() (raysan5#3185)
Browse files Browse the repository at this point in the history
A significant performance increase can be had by copying the first row to all other rows.
  • Loading branch information
smalltimewizard authored and Rodrigo Escar committed Oct 4, 2023
1 parent 4660432 commit bb57c88
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/rtextures.c
Original file line number Diff line number Diff line change
Expand Up @@ -3135,26 +3135,28 @@ void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color)
if (rec.height < 0) rec.height = 0;

int sy = (int)rec.y;
int ey = sy + (int)rec.height;

int sx = (int)rec.x;

int bytesPerPixel = GetPixelDataSize(1, 1, dst->format);

for (int y = sy; y < ey; y++)
{
// Fill in the first pixel of the row based on image format
ImageDrawPixel(dst, sx, y, color);
// Fill in the first pixel of the first row based on image format
ImageDrawPixel(dst, sx, sy, color);

int bytesOffset = ((y*dst->width) + sx)*bytesPerPixel;
unsigned char *pSrcPixel = (unsigned char *)dst->data + bytesOffset;
int bytesOffset = ((sy*dst->width) + sx)*bytesPerPixel;
unsigned char *pSrcPixel = (unsigned char *)dst->data + bytesOffset;

// Repeat the first pixel data throughout the row
for (int x = 1; x < (int)rec.width; x++)
{
memcpy(pSrcPixel + x*bytesPerPixel, pSrcPixel, bytesPerPixel);
}
// Repeat the first pixel data throughout the row
for (int x = 1; x < (int)rec.width; x++)
{
memcpy(pSrcPixel + x*bytesPerPixel, pSrcPixel, bytesPerPixel);
}

// Repeat the first row data for all other rows
int bytesPerRow = bytesPerPixel * (int)rec.width;
for (int y = 1; y < (int)rec.height; y++)
{
memcpy(pSrcPixel + (y*dst->width)*bytesPerPixel, pSrcPixel, bytesPerRow);
}
}

// Draw rectangle lines within an image
Expand Down

0 comments on commit bb57c88

Please sign in to comment.