
< /* Add this function before the main function */
< static JSAMPLE safe_colormap_access(JSAMPARRAY colormap, unsigned int index, unsigned int max_index) {
<     return (index < max_index) ? colormap[0][index] : 0;
< }
< 
<     *outptr++ = colormap[0][t]; /* colormap[x] has size cmaplen */
---
>     *outptr++ = safe_colormap_access(colormap, t, source->cmaplen);
