From 35958c8c6b600482f68fef08ac26547b5655e987 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Wed, 7 Feb 2024 11:36:21 +0100 Subject: A lot of small changes --- userland/libppm/ppm.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'userland/libppm') diff --git a/userland/libppm/ppm.c b/userland/libppm/ppm.c index 030c68d..9013e27 100644 --- a/userland/libppm/ppm.c +++ b/userland/libppm/ppm.c @@ -64,36 +64,47 @@ int load_ppm6_file(FILE *fp, const struct PPM_IMAGE *img, uint32_t buf_width, printf("end malloc\n"); printf("fread"); - int rc = fread(rgb, 3, n_pixels, fp); + const int rc = fread(rgb, 3, n_pixels, fp); if (0 == rc) return 0; printf("end fread"); uint32_t *p = rgb; + u32 buf_size = buf_height * buf_width; if (1 == modifier) { - for (; rc--; p = ((uint8_t *)p) + 3) { - uint32_t v = *p; - buffer[cy * buf_width + cx] = - ((v & 0xFF) << 16) | ((v & 0xFF00)) | ((v & 0xFF0000) >> 16); + for (int i = 0; i < rc && i < buf_size; i++, p = ((uint8_t *)p) + 3) { + if (cx > buf_width) { + i--; + } else { + uint32_t v = *p; + buffer[cy * buf_width + cx] = + ((v & 0xFF) << 16) | ((v & 0xFF00)) | ((v & 0xFF0000) >> 16); + } cx++; if (cx == img->width) { cx = 0; cy++; + i--; continue; } } } else { - for (; rc--; p = ((uint8_t *)p) + 3) { + for (int i = 0; i < rc && i < buf_size; i++, p = ((uint8_t *)p) + 3) { ((uint8_t *)p)[0] *= modifier; ((uint8_t *)p)[1] *= modifier; ((uint8_t *)p)[2] *= modifier; - uint32_t v = *p; - buffer[cy * buf_width + cx] = - ((v & 0xFF) << 16) | ((v & 0xFF00)) | ((v & 0xFF0000) >> 16); + if (cx > buf_width) { + i--; + } else { + uint32_t v = *p; + buffer[cy * buf_width + cx] = + ((v & 0xFF) << 16) | ((v & 0xFF00)) | ((v & 0xFF0000) >> 16); + } cx++; if (cx == img->width) { cx = 0; cy++; + i--; continue; } } -- cgit v1.2.3