|
| 1 | +/* calibration.h |
| 2 | +* |
| 3 | +* MIT License |
| 4 | +* |
| 5 | +* Copyright (c) 2022 awawa-dev |
| 6 | +* |
| 7 | +* https://github.com/awawa-dev/HyperSerialEsp8266 |
| 8 | +* |
| 9 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +* of this software and associated documentation files (the "Software"), to deal |
| 11 | +* in the Software without restriction, including without limitation the rights |
| 12 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +* copies of the Software, and to permit persons to whom the Software is |
| 14 | +* furnished to do so, subject to the following conditions: |
| 15 | +* |
| 16 | +* The above copyright notice and this permission notice shall be included in all |
| 17 | +* copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +* SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#ifndef CALIBRATION_H |
| 29 | +#define CALIBRATION_H |
| 30 | + |
| 31 | +#include <stdint.h> |
| 32 | +#include <algorithm> |
| 33 | + |
| 34 | +#define ROUND_DIVIDE(numer, denom) (((numer) + (denom) / 2) / (denom)) |
| 35 | + |
| 36 | +struct { |
| 37 | + uint8_t white[256]; |
| 38 | + uint8_t red[256]; |
| 39 | + uint8_t green[256]; |
| 40 | + uint8_t blue[256]; |
| 41 | +} channelCorrection; |
| 42 | + |
| 43 | +struct { |
| 44 | + uint8_t gain = 0xFF; |
| 45 | + #ifdef COLD_WHITE |
| 46 | + uint8_t red = 0xA0; // adjust red -> white in 0-0xFF range |
| 47 | + uint8_t green = 0xA0; // adjust green -> white in 0-0xFF range |
| 48 | + uint8_t blue = 0xA0; // adjust blue -> white in 0-0xFF range |
| 49 | + #else |
| 50 | + uint8_t red = 0xB0; // adjust red -> white in 0-0xFF range |
| 51 | + uint8_t green = 0xB0; // adjust green -> white in 0-0xFF range |
| 52 | + uint8_t blue = 0x70; // adjust blue -> white in 0-0xFF range |
| 53 | + #endif |
| 54 | + |
| 55 | + void setParams(uint8_t _gain, uint8_t _red, uint8_t _green, uint8_t _blue) |
| 56 | + { |
| 57 | + gain = _gain; |
| 58 | + red = _red; |
| 59 | + green = _green; |
| 60 | + blue = _blue; |
| 61 | + } |
| 62 | + |
| 63 | + void prepareCalibration() |
| 64 | + { |
| 65 | + // prepare LUT calibration table, cold white is much better than "neutral" white |
| 66 | + for (uint32_t i = 0; i < 256; i++) |
| 67 | + { |
| 68 | + // color calibration |
| 69 | + uint32_t _gain = uint32_t(gain) * i; // adjust gain |
| 70 | + uint32_t _red = red * i; // adjust red |
| 71 | + uint32_t _green = green * i; // adjust green |
| 72 | + uint32_t _blue = blue * i; // adjust blue |
| 73 | + |
| 74 | + channelCorrection.white[i] = (uint8_t)std::min(ROUND_DIVIDE(_gain, 0xFF), (uint32_t)0xFF); |
| 75 | + channelCorrection.red[i] = (uint8_t)std::min(ROUND_DIVIDE(_red, 0xFF), (uint32_t)0xFF); |
| 76 | + channelCorrection.green[i] = (uint8_t)std::min(ROUND_DIVIDE(_green,0xFF), (uint32_t)0xFF); |
| 77 | + channelCorrection.blue[i] = (uint8_t)std::min(ROUND_DIVIDE(_blue, 0xFF), (uint32_t)0xFF); |
| 78 | + } |
| 79 | + } |
| 80 | +} calibrationConfig; |
| 81 | +#endif |
| 82 | + |
0 commit comments