Skip to content

Commit

Permalink
Oops. Maths strike again.
Browse files Browse the repository at this point in the history
This *can* go negative, so, clamp it properly.

Thanks to @tymmej for noticing in
koreader/koreader#11756 ;).
  • Loading branch information
NiLuJe committed May 7, 2024
1 parent 9c61a1a commit c350106
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions fbink.c
Original file line number Diff line number Diff line change
Expand Up @@ -10730,6 +10730,13 @@ static __attribute__((hot)) uint8_t
return (q > UINT8_MAX ? UINT8_MAX : (uint8_t) q);
}

static uint8_t
hhuclampf(float d, float min, float max)
{
const float t = d < min ? min : d;
return t > max ? (uint8_t) max : (uint8_t) t;
}

static __attribute__((hot)) void
saturation_boost_hsp(FBInkPixelRGBA* restrict px, const float change)
{
Expand All @@ -10750,9 +10757,9 @@ static __attribute__((hot)) void
G = P + (G - P) * change;
B = P + (B - P) * change;

px->color.r = (R > UINT8_MAX ? UINT8_MAX : (uint8_t) R);
px->color.g = (G > UINT8_MAX ? UINT8_MAX : (uint8_t) G);
px->color.b = (B > UINT8_MAX ? UINT8_MAX : (uint8_t) B);
px->color.r = hhuclampf(R, 0, UINT8_MAX);
px->color.g = hhuclampf(G, 0, UINT8_MAX);
px->color.b = hhuclampf(B, 0, UINT8_MAX);
}

// Draw image data on screen (we inherit a few of the variable types/names from stbi ;))
Expand Down
1 change: 1 addition & 0 deletions fbink_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ unsigned char*
static unsigned char* img_load_from_file(const char*, int* restrict, int* restrict, int* restrict, int);
static unsigned char* img_convert_px_format(const unsigned char* restrict, int, int, int, int);
static __attribute__((hot)) uint8_t dither_o8x8(unsigned short int, unsigned short int, uint8_t);
static uint8_t hhuclampf(float d, float min, float max);
static __attribute__((hot)) void saturation_boost_hsp(FBInkPixelRGBA* restrict px, const float);
static int draw_image(int,
const unsigned char* restrict,
Expand Down

0 comments on commit c350106

Please sign in to comment.