Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/theora/editor/rgb2yuv.h
10278 views
1
/**************************************************************************/
2
/* rgb2yuv.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/typedefs.h"
34
35
// For reference, see:
36
// - https://stackoverflow.com/a/9467305
37
// - https://en.wikipedia.org/wiki/YCbCr#Approximate_8-bit_matrices_for_BT.601
38
39
static void _rgb2yuv420(uint8_t *y, uint8_t *u, uint8_t *v, uint8_t *rgb, size_t width, size_t height, size_t pixel_size) {
40
size_t uvpos = 0;
41
size_t i = 0;
42
for (size_t line = 0; line < height; line += 2) {
43
for (size_t x = 0; x < width; x += 2) {
44
uint8_t r = rgb[pixel_size * i];
45
uint8_t g = rgb[pixel_size * i + 1];
46
uint8_t b = rgb[pixel_size * i + 2];
47
48
y[i++] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
49
50
u[uvpos] = ((-38 * r + -74 * g + 112 * b) >> 8) + 128;
51
v[uvpos] = ((112 * r + -94 * g + -18 * b) >> 8) + 128;
52
uvpos++;
53
54
r = rgb[pixel_size * i];
55
g = rgb[pixel_size * i + 1];
56
b = rgb[pixel_size * i + 2];
57
58
y[i++] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
59
}
60
for (size_t x = 0; x < width; x += 1) {
61
uint8_t r = rgb[pixel_size * i];
62
uint8_t g = rgb[pixel_size * i + 1];
63
uint8_t b = rgb[pixel_size * i + 2];
64
65
y[i++] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
66
}
67
}
68
}
69
70
static void rgb2yuv420(uint8_t *y, uint8_t *u, uint8_t *v, uint8_t *rgb, size_t width, size_t height) {
71
_rgb2yuv420(y, u, v, rgb, width, height, 3);
72
}
73
74
static void rgba2yuv420(uint8_t *y, uint8_t *u, uint8_t *v, uint8_t *rgba, size_t width, size_t height) {
75
_rgb2yuv420(y, u, v, rgba, width, height, 4);
76
}
77
78