Skip to content

Commit

Permalink
Refactor oslSetImagePixel function for clarity and robustness
Browse files Browse the repository at this point in the history
- Simplified bounds checking with early return for out-of-bounds pixels.
- Added explicit type casts for pixel value assignments to ensure type safety.
- Improved consistency in code formatting and alignment for better readability.
  • Loading branch information
dogo committed Sep 2, 2024
1 parent dc6df8f commit a5f0e09
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/image/oslSetImagePixel.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
#include "oslib.h"

//Dessine un pixel sur une image - lent
void oslSetImagePixel(OSL_IMAGE *img, unsigned int x, unsigned int y, int pixelValue) {
void *pPixel = oslGetUncachedPtr(oslGetSwizzledPixelAddr(img, x, y));
// Draws a pixel on an image - slow
void oslSetImagePixel(OSL_IMAGE *img, unsigned int x, unsigned int y, int pixelValue) {
// Early exit if the pixel is out of bounds (unsigned check covers non-negative range)
if (x >= img->sizeX || y >= img->sizeY) {
return;
}

//Clipping - use of unsigned avoids checking if it's greater than 0.
if (/*x >= 0 &&*/ x < img->sizeX && /*y >= 0 &&*/ y < img->sizeY) {
switch (img->pixelFormat) {
case OSL_PF_8888:
*(u32*)pPixel = pixelValue;
break;
// Get the pointer to the pixel's address in the image, avoiding cache issues
void *pPixel = oslGetUncachedPtr(oslGetSwizzledPixelAddr(img, x, y));

case OSL_PF_5650:
case OSL_PF_5551:
case OSL_PF_4444:
*(u16*)pPixel = pixelValue;
break;
// Set the pixel value based on the image's pixel format
switch (img->pixelFormat) {
case OSL_PF_8888:
*(u32*)pPixel = (u32)pixelValue;
break;

case OSL_PF_8BIT:
*(u8*)pPixel = pixelValue;
break;
case OSL_PF_5650:
case OSL_PF_5551:
case OSL_PF_4444:
*(u16*)pPixel = (u16)pixelValue;
break;

case OSL_PF_4BIT:
*(u8*)pPixel &= ~(15 << ((x & 1) << 2));
*(u8*)pPixel |= (pixelValue & 15) << ((x & 1) << 2);
break;
}
}
case OSL_PF_8BIT:
*(u8*)pPixel = (u8)pixelValue;
break;

case OSL_PF_4BIT: {
*(u8*)pPixel &= ~(15 << ((x & 1) << 2));
*(u8*)pPixel |= (pixelValue & 15) << ((x & 1) << 2);
break;
}
}
}

0 comments on commit a5f0e09

Please sign in to comment.