Skip to content

Commit

Permalink
Optimized oslGetImagePixel function for better readability and perfor…
Browse files Browse the repository at this point in the history
…mance

- Added early boundary checks for x and y coordinates to prevent unnecessary processing.
- Simplified switch-case structure and improved 4-bit pixel format handling.
- Enhanced error handling for unsupported pixel formats.
- Ensured the function returns -1 for out-of-bounds or unsupported cases.
  • Loading branch information
dogo committed Sep 1, 2024
1 parent ae428f5 commit 8ace700
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/image/oslGetImagePixel.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
#include "oslib.h"

//Retourne la valeur d'un pixel sur une image - lent
int oslGetImagePixel(OSL_IMAGE *img, unsigned int x, unsigned int y) {
void *pPixel = oslGetUncachedPtr(oslGetSwizzledPixelAddr(img, x, y));
// Retrieves the value of a pixel on an image - optimized
int oslGetImagePixel(OSL_IMAGE *img, unsigned int x, unsigned int y) {
if (x >= img->sizeX || y >= img->sizeY) {
return -1; // Return error for out-of-bounds coordinates
}

//Because of unsigned, we don't have to check if they're greater than 0!
if (/*x >= 0 &&*/ x < img->sizeX /*&& y >= 0*/ && y < img->sizeY) {
switch (img->pixelFormat) {
case OSL_PF_8888:
return *(u32*)pPixel;
void *pPixel = oslGetUncachedPtr(oslGetSwizzledPixelAddr(img, x, y));

case OSL_PF_5650:
case OSL_PF_5551:
case OSL_PF_4444:
return *(u16*)pPixel;
switch (img->pixelFormat) {
case OSL_PF_8888:
return *(u32*)pPixel;

case OSL_PF_8BIT:
return *(u8*)pPixel;
case OSL_PF_5650:
case OSL_PF_5551:
case OSL_PF_4444:
return *(u16*)pPixel;

case OSL_PF_4BIT:
return *(u8*)pPixel & (15 << ((x & 1) << 2));
}
}
return -1;
case OSL_PF_8BIT:
return *(u8*)pPixel;

case OSL_PF_4BIT:
return (*(u8*)pPixel >> ((x & 1) * 4)) & 0xF; // Extract the correct 4-bit value

default:
return -1; // Return error for unsupported formats
}
}

0 comments on commit 8ace700

Please sign in to comment.