-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor oslSetImagePixel function for clarity and robustness
- 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
Showing
1 changed file
with
28 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |