1 #include "lib_ccx.h"
2 #include "utility.h"
3
4 #ifdef ENABLE_HARDSUBX
5 //TODO: Correct FFMpeg integration
6 #include <libavcodec/avcodec.h>
7 #include <libavformat/avformat.h>
8 #include <libavutil/imgutils.h>
9 #include <libswscale/swscale.h>
10 #include "allheaders.h"
11 #include "hardsubx.h"
12
13 #define BLACK 20.0
14 #define YELLOW 70.0
15
16 #define min_f(a, b, c) (fminf(a, fminf(b, c)))
17 #define max_f(a, b, c) (fmaxf(a, fmaxf(b, c)))
18
rgb_to_hsv(float R,float G,float B,float * H,float * S,float * V)19 void rgb_to_hsv(float R, float G, float B,float *H, float *S, float *V)
20 {
21 //Conversion into HSV color space to get Hue
22 float r = R / 255.0f;
23 float g = G / 255.0f;
24 float b = B / 255.0f;
25
26 float h, s, v; // h:0-360.0, s:0.0-1.0, v:0.0-1.0
27
28 float max = max_f(r, g, b);
29 float min = min_f(r, g, b);
30
31 v = max;
32
33 if (max == 0.0f) {
34 s = 0;
35 h = 0;
36 }
37 else if (max - min == 0.0f) {
38 s = 0;
39 h = 0;
40 }
41 else {
42 s = (max - min) / max;
43
44 if (max == r) {
45 h = 60 * ((g - b) / (max - min)) + 0;
46 }
47 else if (max == g) {
48 h = 60 * ((b - r) / (max - min)) + 120;
49 }
50 else {
51 h = 60 * ((r - g) / (max - min)) + 240;
52 }
53 }
54
55 if (h < 0) h += 360.0f;
56
57 *H = (unsigned char)(h); // dst_h : 0-360
58 *S = (unsigned char)(s * 255); // dst_s : 0-255
59 *V = (unsigned char)(v * 255); // dst_v : 0-255
60 }
61
rgb_to_lab(float R,float G,float B,float * L,float * a,float * b)62 void rgb_to_lab(float R, float G, float B,float *L, float *a, float *b)
63 {
64 //Conversion to the CIE-LAB color space to get the Luminance
65 float X, Y, Z, fX, fY, fZ;
66
67 X = 0.412453*R + 0.357580*G + 0.180423*B;
68 Y = 0.212671*R + 0.715160*G + 0.072169*B;
69 Z = 0.019334*R + 0.119193*G + 0.950227*B;
70
71 X /= (255 * 0.950456);
72 Y /= 255;
73 Z /= (255 * 1.088754);
74
75 if (Y > 0.008856)
76 {
77 fY = pow(Y, 1.0/3.0);
78 *L = 116.0*fY - 16.0;
79 }
80 else
81 {
82 fY = 7.787*Y + 16.0/116.0;
83 *L = 903.3*Y;
84 }
85
86 if (X > 0.008856)
87 fX = pow(X, 1.0/3.0);
88 else
89 fX = 7.787*X + 16.0/116.0;
90
91 if (Z > 0.008856)
92 fZ = pow(Z, 1.0/3.0);
93 else
94 fZ = 7.787*Z + 16.0/116.0;
95
96 *a = 500.0*(fX - fY);
97 *b = 200.0*(fY - fZ);
98
99 if (*L < BLACK) {
100 *a *= exp((*L - BLACK) / (BLACK / 4));
101 *b *= exp((*L - BLACK) / (BLACK / 4));
102 *L = BLACK;
103 }
104 if (*b > YELLOW)
105 *b = YELLOW;
106 }
107
108 #endif
109