Skip to content

Commit 39c2de0

Browse files
committed
Fixed an underflow issue in the dithering process, as reported by @waugustus in #172.
1 parent fda7c55 commit 39c2de0

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/quant.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,13 @@ sixel_quant_apply_palette(
12831283
goto end;
12841284
}
12851285

1286+
/* NOTE: diffuse_jajuni, diffuse_stucki, and diffuse_burkes reference at
1287+
* minimum the position pos + width * 1 - 2, so width must be at least 2
1288+
* to avoid underflow.
1289+
* On the other hand, diffuse_fs and diffuse_atkinson
1290+
* reference pos + width * 1 - 1, but since these functions are only called
1291+
* when width >= 1, they do not cause underflow.
1292+
*/
12861293
if (depth != 3) {
12871294
f_diffuse = diffuse_none;
12881295
} else {
@@ -1297,13 +1304,16 @@ sixel_quant_apply_palette(
12971304
f_diffuse = diffuse_fs;
12981305
break;
12991306
case SIXEL_DIFFUSE_JAJUNI:
1300-
f_diffuse = diffuse_jajuni;
1307+
/* fallback to diffuse_none if width < 2 */
1308+
f_diffuse = width >= 2 ? diffuse_jajuni: diffuse_none;
13011309
break;
13021310
case SIXEL_DIFFUSE_STUCKI:
1303-
f_diffuse = diffuse_stucki;
1311+
/* fallback to diffuse_none if width < 2 */
1312+
f_diffuse = width >= 2 ? diffuse_stucki: diffuse_none;
13041313
break;
13051314
case SIXEL_DIFFUSE_BURKES:
1306-
f_diffuse = diffuse_burkes;
1315+
/* fallback to diffuse_none if width < 2 */
1316+
f_diffuse = width >= 2 ? diffuse_burkes: diffuse_none;
13071317
break;
13081318
case SIXEL_DIFFUSE_A_DITHER:
13091319
f_diffuse = diffuse_none;

0 commit comments

Comments
 (0)