From 797266ea930ca103e7fa7a2e683158d935683f8f Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sun, 27 Oct 2013 12:05:39 +0000 Subject: Avoid repeating some calculations. --- include/palette.h | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/include/palette.h b/include/palette.h index b48d5f3..a4b9f77 100644 --- a/include/palette.h +++ b/include/palette.h @@ -66,18 +66,22 @@ static inline uint8_t nsfb_palette_best_match(struct nsfb_palette_s *palette, int cur_distance; int best_distance = INT_MAX; + int r = ( c & 0xFF); + int g = ((c >> 8) & 0xFF); + int b = ((c >> 16) & 0xFF); + switch (palette->type) { case NSFB_PALETTE_NSFB_8BPP: /* Index into colour cube part */ - dr = ((( c & 0xFF) * 5) + 128) / 256; - dg = ((((c >> 8) & 0xFF) * 7) + 128) / 256; - db = ((((c >> 16) & 0xFF) * 4) + 128) / 256; + dr = ((r * 5) + 128) / 256; + dg = ((g * 7) + 128) / 256; + db = ((b * 4) + 128) / 256; col = 40 * dr + 5 * dg + db; palent = palette->data[col]; - dr = ( c & 0xFF) - ( palent & 0xFF); - dg = ((c >> 8) & 0xFF) - ((palent >> 8 ) & 0xFF); - db = ((c >> 16) & 0xFF) - ((palent >> 16) & 0xFF); + dr = r - ( palent & 0xFF); + dg = g - ((palent >> 8) & 0xFF); + db = b - ((palent >> 16) & 0xFF); cur_distance = (dr * dr) + (dg * dg) + (db * db); best_col = col; @@ -87,14 +91,12 @@ static inline uint8_t nsfb_palette_best_match(struct nsfb_palette_s *palette, *b_error = db; /* Index into grayscale part */ - col = (( c & 0xFF) + - ((c >> 8) & 0xFF) + - ((c >> 16) & 0xFF) + (45 / 2)) / (15 * 3) - 1 + 240; + col = (r + g + b + (45 / 2)) / (15 * 3) - 1 + 240; palent = palette->data[col]; - dr = ( c & 0xFF) - ( palent & 0xFF); - dg = ((c >> 8) & 0xFF) - ((palent >> 8) & 0xFF); - db = ((c >> 16) & 0xFF) - ((palent >> 16) & 0xFF); + dr = r - (palent & 0xFF); + dg = g - (palent & 0xFF); + db = b - (palent & 0xFF); cur_distance = (dr * dr) + (dg * dg) + (db * db); if (cur_distance < best_distance) { best_col = col; @@ -109,9 +111,9 @@ static inline uint8_t nsfb_palette_best_match(struct nsfb_palette_s *palette, for (col = 0; col <= palette->last; col++) { palent = palette->data[col]; - dr = ( c & 0xFF) - ( palent & 0xFF); - dg = ((c >> 8) & 0xFF) - ((palent >> 8) & 0xFF); - db = ((c >> 16) & 0xFF) - ((palent >> 16) & 0xFF); + dr = r - ( palent & 0xFF); + dg = g - ((palent >> 8) & 0xFF); + db = b - ((palent >> 16) & 0xFF); cur_distance = (dr * dr) + (dg * dg) + (db * db); if (cur_distance < best_distance) { best_distance = cur_distance; -- cgit v1.2.3