blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

colour.h (5369B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <math.h>
      3 
      4 #define D65_XYY_X 0.312726871026564878786047074755
      5 #define D65_XYY_Y 0.329023206641284038376227272238
      6 
      7 #define D65_XYZ_X (D65_XYY_X / D65_XYY_Y)
      8 #define D65_XYZ_Z (1 / D65_XYY_Y - 1 - D65_XYZ_X)
      9 
     10 #define SRGB_ENCODE(TYPE, NAME_SUFFIX, MATH_SUFFIX)\
     11 	static inline TYPE\
     12 	srgb_encode##NAME_SUFFIX(TYPE t)\
     13 	{\
     14 		TYPE sign = 1;\
     15 		if (t < 0) {\
     16 			t = -t;\
     17 			sign = -1;\
     18 		}\
     19 		t = t <= (TYPE)0.0031306684425217108\
     20 			? (TYPE)12.92 * t\
     21 			: (TYPE)1.055 * pow##MATH_SUFFIX(t, (TYPE)1.0 / (TYPE)2.4) - (TYPE)0.055;\
     22 		return t * sign;\
     23 	}
     24 SRGB_ENCODE(double, _d,)
     25 SRGB_ENCODE(float, _f, f)
     26 #undef SRGB_ENCODE
     27 
     28 #define SRGB_DECODE(TYPE, NAME_SUFFIX, MATH_SUFFIX)\
     29 	static inline TYPE\
     30 	srgb_decode##NAME_SUFFIX(TYPE t)\
     31 	{\
     32 		TYPE sign = 1;\
     33 		if (t < 0) {\
     34 			t = -t;\
     35 			sign = -1;\
     36 		}\
     37 		t = t <= (TYPE)0.0031306684425217108 * (TYPE)12.92\
     38 			? t / (TYPE)12.92\
     39 			: pow##MATH_SUFFIX((t + (TYPE)0.055) / (TYPE)1.055, (TYPE)2.4);\
     40 		return t * sign;\
     41 	}
     42 SRGB_DECODE(double, _d,)
     43 SRGB_DECODE(float, _f, f)
     44 #undef SRGB_DECODE
     45 
     46 
     47 #define MATRIX_MULTIPLY_FUNCTION(FUNCTION, TYPE, R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3)\
     48 	static inline void\
     49 	FUNCTION(TYPE ia, TYPE ib, TYPE ic, TYPE *oa, TYPE *ob, TYPE *oc)\
     50 	{\
     51 		*oa = (TYPE)(R1C1) * ia + (TYPE)(R1C2) * ib + (TYPE)(R1C3) * ic;\
     52 		*ob = (TYPE)(R2C1) * ia + (TYPE)(R2C2) * ib + (TYPE)(R2C3) * ic;\
     53 		*oc = (TYPE)(R3C1) * ia + (TYPE)(R3C2) * ib + (TYPE)(R3C3) * ic;\
     54 	}
     55 #define MATRIX_MULTIPLY_FUNCTIONS(FUNCTION_BASE, ...)\
     56 	MATRIX_MULTIPLY_FUNCTION(FUNCTION_BASE##_d, double, __VA_ARGS__)\
     57 	MATRIX_MULTIPLY_FUNCTION(FUNCTION_BASE##_f, float,  __VA_ARGS__)
     58 
     59 MATRIX_MULTIPLY_FUNCTIONS(yuv_to_srgb,
     60 			   0.99983946058747896756813133833929896354675292968750,
     61 			  -0.00111314629257774910797706979792565107345581054688,
     62 			   1.59561960249794831945280293439282104372978210449219,
     63 			   0.99983946058762285247212275862693786621093750000000,
     64 			  -0.39124374513994553126394748687744140625000000000000,
     65 			  -0.81304201036561862814266987697919830679893493652344,
     66 			   0.99983946058757433572594663928612135350704193115234,
     67 			   2.01741786772377817982260239659808576107025146484375,
     68 			   0.00115819721926166407932612401054939255118370056152)
     69 
     70 MATRIX_MULTIPLY_FUNCTIONS(srgb_to_yuv,
     71 			   0.29906158567044888174635275390755850821733474731445,
     72 			   0.58707985442089483107963587826816365122795104980469,
     73 			   0.11401912509812317519930502385250292718410491943359,
     74 			  -0.14846813725490196067546833091910229995846748352051,
     75 			  -0.29074754901960786490633381617954000830650329589844,
     76 			   0.43921568627450979782622653146972879767417907714844,
     77 			   0.43921568627450979782622653146972879767417907714844,
     78 			  -0.36807598039215688734415721228288020938634872436523,
     79 			  -0.07113970588235293823764493481576209887862205505371)
     80 
     81 MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_srgb,
     82                            3.240446254647737500675930277794,
     83                           -1.537134761820080575134284117667,
     84                           -0.498530193022728718155178739835,
     85                           -0.969266606244679751469561779231,
     86                            1.876011959788370209167851498933,
     87                            0.041556042214430065351304932619,
     88                            0.055643503564352832235773149705,
     89                           -0.204026179735960239147729566866,
     90                            1.057226567722703292062647051353)
     91 
     92 MATRIX_MULTIPLY_FUNCTIONS(srgb_to_ciexyz,
     93                           0.412457445582367576708548995157,
     94                           0.357575865245515878143578447634,
     95                           0.180437247826399665973085006954,
     96                           0.212673370378408277403536885686,
     97                           0.715151730491031756287156895269,
     98                           0.072174899130559869164791564344,
     99                           0.019333942761673460208893260415,
    100                           0.119191955081838593666354597644,
    101                           0.950302838552371742508739771438)
    102 
    103 MATRIX_MULTIPLY_FUNCTIONS(scaled_yuv_to_ciexyz,
    104                            0.00001450092272326647052454150210731853576362482272 / 0.8552986953536278,
    105 			   0.00000341281577174185336742326911385614351956974133,
    106 			   0.00000560936853511406815494611324823814868523186306,
    107 			   0.00001525657222228693664302541510569000138275441714 / 0.8552986953536278,
    108 			  -0.00000205124661398017884712786061685019234346327721,
    109 			  -0.00000369297336083484072815404791745308443751127925,
    110 			   0.00001661179425452522781048517175328527173405745998 / 0.8552986953536278,
    111 			   0.00002854205077959434113873959937279778387164697051,
    112 			  -0.00000099119265445913120730111588868593841539222922)
    113 
    114 MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_scaled_yuv,
    115                            26633.45812403910895227454602718353271484375   * 0.8552986953536278,
    116 			   40527.367131481194519437849521636962890625     * 0.8552986953536278,
    117 			    -272.0008243176007454167120158672332763671875 * 0.8552986953536278,
    118 			  -11458.90154486896426533348858356475830078125,
    119 			  -26662.3801805758339469321072101593017578125,
    120 			   34490.0235254608633113093674182891845703125,
    121 			  116394.099928013441967777907848358154296875,
    122 			  -88546.58492295184987597167491912841796875,
    123 			  -20281.0410136536011123098433017730712890625)
    124 
    125 #undef MATRIX_MULTIPLY_FUNCTIONS
    126 #undef MATRIX_MULTIPLY_FUNCTION