From d6ccd6da61dbad24706e4c3fbff1bd7649061d10 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 9 Feb 2023 16:33:34 -0800 Subject: [PATCH] 16 Bit Gamma improvements (#645) * Gamma for 16bit Table Method --- examples/NeoPixelBusLg/NeoPixelBusLg.ino | 4 +- src/NeoPixelBusLg.h | 2 +- src/internal/NeoGamma.cpp | 32 +++++----- src/internal/NeoGamma.h | 75 ++++++++++++++++++++++-- 4 files changed, 89 insertions(+), 24 deletions(-) diff --git a/examples/NeoPixelBusLg/NeoPixelBusLg.ino b/examples/NeoPixelBusLg/NeoPixelBusLg.ino index 10b9b1ae..c69a8048 100644 --- a/examples/NeoPixelBusLg/NeoPixelBusLg.ino +++ b/examples/NeoPixelBusLg/NeoPixelBusLg.ino @@ -22,11 +22,11 @@ RgbColor white(255, 255, 255); // for your NeoPixels NeoPixelBusLg strip(PixelCount, PixelPin); -// If speed is an issue and memory is not, then you can use the gama table variant +// If speed is an issue and memory is not, then you can use the gamma table variant // which is much faster but uses 256 bytes of RAM // NeoPixelBusLg strip(PixelCount, PixelPin); -// If you want to turn gama correction off, then you can use the null gama method +// If you want to turn gamma correction off, then you can use the null gamma method // NeoPixelBusLg strip(PixelCount, PixelPin); void setup() diff --git a/src/NeoPixelBusLg.h b/src/NeoPixelBusLg.h index 29b46750..62eb9ae9 100644 --- a/src/NeoPixelBusLg.h +++ b/src/NeoPixelBusLg.h @@ -74,7 +74,7 @@ template> 8); + uint16_t lo = (value & 0x00ff); + uint8_t hiResult = _table[hi]; + uint16_t lowResult = 0; + + if (hi < countof(_hints)) + { + // use _hints table to calculate a reasonable lowbyte + lowResult = (lo + _hints[hi].pos * 256) / _hints[hi].count; + } + else if (hi == 255) + { + // last entry is always linear + lowResult = lo; + } + else + { + // check the _table for duplicate or jumps to adjust the range of lowbyte + if (hiResult == _table[hi - 1]) + { + // this result is an upper duplicate + lowResult = (lo >> 1) | 0x80; // lo / 2 + 128 + } + else + { + uint8_t delta = _table[hi + 1] - hiResult; + + if (delta == 0) + { + // this result is a lower duplicate + lowResult = (lo >> 1); // lo / 2 + } + else if (delta == 1) + { + // this result is incremental and thus linear + lowResult = lo; + } + else + { + // this result jumps by more than one, so need to spread across + lowResult = delta * lo; + } + } + + } + + return (static_cast(hiResult) << 8) + lowResult; } - */ + private: static const uint8_t _table[256]; + }; // no gamma correction at all