Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized oslGetImagePixel function for better readability and performance #50

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}