-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsc.c
149 lines (114 loc) · 2.96 KB
/
csc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2020 Bootlin
*/
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <math.h>
#include <draw.h>
#include <csc.h>
int rgb2yuv420(struct draw_buffer *buffer, void *buffer_y, void *buffer_u,
void *buffer_v)
{
unsigned int width, height, stride;
unsigned int x, y;
void *data;
if (!buffer)
return -EINVAL;
data = buffer->data;
width = buffer->width;
height = buffer->height;
stride = buffer->stride;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
uint8_t *crgb;
uint8_t *cy;
float value;
crgb = data + stride * y + x * 4;
value = crgb[2] * 0.299 + crgb[1] * 0.587 + crgb[0] * 0.114;
cy = buffer_y + width * y + x;
*cy = byte_range(value);
if ((x % 2) == 0 && (y % 2) == 0) {
uint8_t *cu;
uint8_t *cv;
value = crgb[2] * -0.14713 + crgb[1] * -0.28886 + crgb[0] * 0.436 + 128.;
cu = buffer_u + width / 2 * y / 2 + x / 2;
*cu = byte_range(value);
value = crgb[2] * 0.615 + crgb[1] * -0.51499 + crgb[0] * -0.10001 + 128.;
cv = buffer_v + width / 2 * y / 2 + x / 2;
*cv = byte_range(value);
}
}
}
return 0;
}
int rgb2nv12(struct draw_buffer *buffer, void *buffer_y, void *buffer_uv)
{
unsigned int width, height, stride;
unsigned int x, y;
void *data;
if (!buffer)
return -EINVAL;
data = buffer->data;
width = buffer->width;
height = buffer->height;
stride = buffer->stride;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
uint8_t *crgb;
uint8_t *cy;
float value;
crgb = data + stride * y + x * 4;
value = crgb[2] * 0.299 + crgb[1] * 0.587 + crgb[0] * 0.114;
cy = buffer_y + width * y + x;
*cy = byte_range(value);
if ((x % 2) == 0 && (y % 2) == 0) {
uint8_t *cu;
uint8_t *cv;
value = crgb[2] * -0.14713 + crgb[1] * -0.28886 + crgb[0] * 0.436 + 128.;
cu = buffer_uv + width * y / 2 + x;
*cu = byte_range(value);
value = crgb[2] * 0.615 + crgb[1] * -0.51499 + crgb[0] * -0.10001 + 128.;
cv = buffer_uv + width * y / 2 + x + 1;
*cv = byte_range(value);
}
}
}
return 0;
}
unsigned int rgb_pixel(unsigned int r, unsigned int g, unsigned int b)
{
return (255 << 24) | (r << 16) | (g << 8) | (b << 0);
}
unsigned int hsv2rgb_pixel(float hi, float si, float vi)
{
unsigned int pixel;
unsigned int ro, go, bo;
float r, g, b;
float h = hi / 360.0;
float s = si / 100.0;
float v = vi / 100.0;
int i = floor(h * 6.0);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
ro = (unsigned int)(r * 255.0);
if (ro > 255)
ro = 255;
go = (unsigned int)(g * 255.0);
if (go > 255)
go = 255;
bo = (unsigned int)(b * 255.0);
if (bo > 255)
bo = 255;
return rgb_pixel(ro, go, bo);
}