From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Vladimir Serbinenko Date: Thu, 26 Feb 2015 18:27:43 +0100 Subject: [PATCH] png: Optimize by avoiding divisions. (cherry picked from commit 18125877eec4e6d7b0b6567b6c92eab1df946f47) --- grub-core/video/readers/png.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c index a9066658f2..71bb76f9c4 100644 --- a/grub-core/video/readers/png.c +++ b/grub-core/video/readers/png.c @@ -236,7 +236,7 @@ grub_png_decode_image_palette (struct grub_png_data *data, { unsigned i = 0, j; - if (len == 0 || len % 3 != 0) + if (len == 0) return GRUB_ERR_NONE; grub_errno = GRUB_ERR_NONE; @@ -900,15 +900,26 @@ grub_png_convert_image (struct grub_png_data *data) int mask = (1 << data->color_bits) - 1; unsigned j; if (data->is_gray) - for (i = 0; i < (1U << data->color_bits); i++) - { - grub_uint8_t col = (0xff * i) / ((1U << data->color_bits) - 1); - palette[i][0] = col; - palette[i][1] = col; - palette[i][2] = col; - } + { + /* Generic formula is + (0xff * i) / ((1U << data->color_bits) - 1) + but for allowed bit depth of 1, 2 and for it's + equivalent to + (0xff / ((1U << data->color_bits) - 1)) * i + Precompute the multipliers to avoid division. + */ + + const grub_uint8_t multipliers[5] = { 0xff, 0xff, 0x55, 0x24, 0x11 }; + for (i = 0; i < (1U << data->color_bits); i++) + { + grub_uint8_t col = multipliers[data->color_bits] * i; + palette[i][0] = col; + palette[i][1] = col; + palette[i][2] = col; + } + } else - grub_memcpy (palette, data->palette, 16 * 3); + grub_memcpy (palette, data->palette, 3 << data->color_bits); d1c = d1; d2c = d2; for (j = 0; j < data->image_height; j++, d1c += data->image_width * 3,