From 8448764aae2315fe93d5d0b74d5077d21b2e35bd Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Sun, 15 Dec 2013 14:28:08 +0100 Subject: [PATCH] added linear_srgb and srgb_linear --- yuv.c | 23 +++++++++++++++++++++++ yuv.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/yuv.c b/yuv.c index 7486ffa..6688245 100644 --- a/yuv.c +++ b/yuv.c @@ -7,6 +7,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include "yuv.h" +#include uint8_t yuv_clamp(float x) { @@ -14,6 +15,28 @@ uint8_t yuv_clamp(float x) return tmp > 255.0 ? 255.0 : tmp; } +uint8_t linear_srgb(float linear) +{ + float v = fminf(fmaxf(linear, 0.0f), 1.0f); + float K0 = 0.03928f; + float a = 0.055f; + float phi = 12.92f; + float gamma = 2.4f; + float srgb = v <= K0 / phi ? v * phi : (1.0f + a) * powf(v, 1.0f / gamma) - a; + return 255.0f * srgb; +} + +float srgb_linear(uint8_t srgb) +{ + float v = srgb / 255.0f; + float K0 = 0.03928f; + float a = 0.055f; + float phi = 12.92f; + float gamma = 2.4f; + float linear = v <= K0 ? v / phi : powf((v + a) / (1.0f + a), gamma); + return linear; +} + uint8_t R_YUV(uint8_t Y, uint8_t U, uint8_t V) { (void)U; diff --git a/yuv.h b/yuv.h index a068e02..7c251e5 100644 --- a/yuv.h +++ b/yuv.h @@ -10,6 +10,8 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #define YUV_H #include +uint8_t linear_srgb(float linear); +float srgb_linear(uint8_t srgb); uint8_t R_YUV(uint8_t, uint8_t, uint8_t); uint8_t G_YUV(uint8_t, uint8_t, uint8_t); uint8_t B_YUV(uint8_t, uint8_t, uint8_t);