Files
Upsilon/kandinsky/src/postprocess_gamma_context.cpp
circuit10 b44a95a9b3 Casio fx-CG series port (#324)
* Initial test - working on Linux

* Try to make it work with liba

* Stop using liba and the filesystem

* IT WORKS

* Key input, full res, fix some of the crashes

* Fix the hang when doing calculations

* Add some more key mappings

* Fix the square root issue

* Icons

* Better key mappings, brightness control, better gamma correction, more effficient framebuffer

* Cleanup stage 1

* Cleanup stage 2

* Make the build system build a g3a

* Make it not exit when you press the menu button

* Add Casio port to README

* Use omega-master instead of omega-dev

* Fix mistake with cherry-picking in the README

* Fix internal storage crash

* Fix compile error on Numworks calculators

* Upsilon branding

* Sharper icon

* Make the CI work

* Add power off and improve menu

* Map Alpha + up/down to the brightness shortcut

* Add missing file

* Fix web CI build

* Revert "Fix web CI build"

This reverts commit f19657d9fc.

* Change "prizm" to "fxcg"

* Add FASTLOAD option for Add-in Push

* Add some charatcers to the catalog on Casio and improve key mappings

* Build with -Os -flto

* Disable LTO for now as it's causing crashes

* Put back the fonts I accidently changed

I'd like to add an option for this though as I prefer the ones from Epsilon
2023-05-10 18:28:18 +02:00

105 lines
3.5 KiB
C++

#include <kandinsky/color.h>
#include <kandinsky/postprocess_gamma_context.h>
#include <ion.h>
#include <math.h>
#include <stdint.h>
constexpr int MaxGammaStates = 7;
constexpr float MaxGammaGamut = 0.75;
constexpr float toGamma(int gamma) {
return 1.f / (1 + (float(gamma) / MaxGammaStates * MaxGammaGamut));
}
constexpr int clampGamma(int gamma) {
return gamma < -MaxGammaStates ? -MaxGammaStates : (gamma > MaxGammaStates ? MaxGammaStates : gamma);
}
void KDPostProcessGammaContext::updateGammaTables() {
const float redGamma = toGamma(m_redGamma);
const float greenGamma = toGamma(m_greenGamma);
const float blueGamma = toGamma(m_blueGamma);
for (int i = 0; i < 32; i++) {
uint8_t r = (uint8_t)(powf((i << 3) / 255.f, redGamma) * 255);
m_redGammaTable[i] = r >> 3;
}
for (int i = 0; i < 64; i++) {
uint8_t g = (uint8_t)(powf((i << 2) / 255.f, greenGamma) * 255);
m_greenGammaTable[i] = g >> 2;
}
for (int i = 0; i < 32; i++) {
uint8_t b = (uint8_t)(powf((i << 3 )/ 255.f, blueGamma) * 255);
m_blueGammaTable[i] = b >> 3;
}
}
KDPostProcessGammaContext::KDPostProcessGammaContext() :
m_redGamma(0), m_greenGamma(0), m_blueGamma(0) {
updateGammaTables();
}
void KDPostProcessGammaContext::gamma(int& red, int& green, int& blue) {
red = m_redGamma;
green = m_greenGamma;
blue = m_blueGamma;
}
void KDPostProcessGammaContext::gamma(float& red, float& green, float& blue) {
red = (m_redGamma + MaxGammaStates) / float(MaxGammaStates*2);
green = (m_greenGamma + MaxGammaStates) / float(MaxGammaStates*2);
blue = (m_blueGamma + MaxGammaStates) / float(MaxGammaStates*2);
}
void KDPostProcessGammaContext::setGamma(int red, int green, int blue) {
m_redGamma = clampGamma(red);
m_greenGamma = clampGamma(green);
m_blueGamma = clampGamma(blue);
updateGammaTables();
}
KDColor KDPostProcessGammaContext::correctColor(KDColor color) {
uint8_t r5 = (((uint16_t )color)>>11)&0x1F;
r5 = m_redGammaTable[r5];
uint8_t g6 = (((uint16_t )color)>>5)&0x3F;
g6 = m_greenGammaTable[g6];
uint8_t b5 = ((uint16_t )color)&0x1F;
b5 = m_blueGammaTable[b5];
return KDColor::RGB16(r5<<11 | g6<<5 | b5);
}
void KDPostProcessGammaContext::pushRect(KDRect rect, const KDColor * pixels) {
KDColor workingBuffer[rect.width()];
for (KDCoordinate y = 0; y < rect.height(); y++) {
KDRect workingRect(rect.x(), rect.y()+y, rect.width(), 1);
for (KDCoordinate x = 0; x < rect.width(); x++) {
const KDColor color = pixels[y*rect.width()+x];
const KDColor result = correctColor(color);
workingBuffer[x] = result;
}
KDPostProcessContext::pushRect(workingRect, workingBuffer);
}
}
void KDPostProcessGammaContext::pushRectUniform(KDRect rect, KDColor color) {
KDPostProcessContext::pushRectUniform(rect, correctColor(color));
}
void KDPostProcessGammaContext::pullRect(KDRect rect, KDColor * pixels) {
const float redGamma = 1.f/toGamma(m_redGamma);
const float greenGamma = 1.f/toGamma(m_greenGamma);
const float blueGamma = 1.f/toGamma(m_blueGamma);
KDPostProcessContext::pullRect(rect, pixels);
for (KDCoordinate y = 0; y < rect.height(); y++) {
for (KDCoordinate x = 0; x < rect.width(); x++) {
const KDColor color = pixels[y*rect.width()+x];
const KDColor result = KDColor::RGB888(
(uint8_t)(powf(color.red()/255.f, redGamma)*255),
(uint8_t)(powf(color.green()/255.f,greenGamma)*255),
(uint8_t)(powf(color.blue()/255.f, blueGamma)*255));
pixels[y*rect.width()+x] = result;
}
}
}