Skip to content

Commit 9f45a4a

Browse files
authored
fix clamping bug in jpeg decode (#18389)
fixes #6611
1 parent a6e7ff2 commit 9f45a4a

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

  • libretro-common/formats/jpeg

libretro-common/formats/jpeg/rjpeg.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ struct rjpeg
123123

124124
#endif
125125

126+
/* Auto-detect NEON support */
127+
#if !defined(RJPEG_NO_SIMD) && !defined(RJPEG_NEON) && (defined(__ARM_NEON__) || defined(HAVE_NEON))
128+
#define RJPEG_NEON
129+
#endif
130+
126131
/* ARM NEON */
127132
#if defined(RJPEG_NO_SIMD) && defined(RJPEG_NEON)
128133
#undef RJPEG_NEON
@@ -771,7 +776,7 @@ static INLINE uint8_t rjpeg_clamp(int x)
771776
{
772777
/* trick to use a single test to catch both cases */
773778
if ((unsigned int) x > 255)
774-
return 255;
779+
return (x < 0) ? 0 : 255;
775780
return (uint8_t) x;
776781
}
777782

@@ -2214,11 +2219,11 @@ static void rjpeg_YCbCr_to_RGB_row(uint8_t *out, const uint8_t *y,
22142219
g >>= 20;
22152220
b >>= 20;
22162221
if ((unsigned) r > 255)
2217-
r = 255;
2222+
r = (r < 0) ? 0 : 255;
22182223
if ((unsigned) g > 255)
2219-
g = 255;
2224+
g = (g < 0) ? 0 : 255;
22202225
if ((unsigned) b > 255)
2221-
b = 255;
2226+
b = (b < 0) ? 0 : 255;
22222227
out[0] = (uint8_t)r;
22232228
out[1] = (uint8_t)g;
22242229
out[2] = (uint8_t)b;
@@ -2358,11 +2363,11 @@ static void rjpeg_YCbCr_to_RGB_simd(uint8_t *out, const uint8_t *y,
23582363
g >>= 20;
23592364
b >>= 20;
23602365
if ((unsigned) r > 255)
2361-
r = 255;
2366+
r = (r < 0) ? 0 : 255;
23622367
if ((unsigned) g > 255)
2363-
g = 255;
2368+
g = (g < 0) ? 0 : 255;
23642369
if ((unsigned) b > 255)
2365-
b = 255;
2370+
b = (b < 0) ? 0 : 255;
23662371
out[0] = (uint8_t)r;
23672372
out[1] = (uint8_t)g;
23682373
out[2] = (uint8_t)b;

0 commit comments

Comments
 (0)