CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Convert/ColorConv.h
Views: 1401
1
// Copyright (c) 2015- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include "ppsspp_config.h"
21
#include "Common/CommonTypes.h"
22
23
inline u8 Convert4To8(u8 v) {
24
// Swizzle bits: 00001234 -> 12341234
25
return (v << 4) | (v);
26
}
27
28
inline u8 Convert5To8(u8 v) {
29
// Swizzle bits: 00012345 -> 12345123
30
return (v << 3) | (v >> 2);
31
}
32
33
inline u8 Convert6To8(u8 v) {
34
// Swizzle bits: 00123456 -> 12345612
35
return (v << 2) | (v >> 4);
36
}
37
38
inline u16 RGBA8888toRGB565(u32 px) {
39
return ((px >> 3) & 0x001F) | ((px >> 5) & 0x07E0) | ((px >> 8) & 0xF800);
40
}
41
42
inline u16 RGBA8888toRGBA4444(u32 px) {
43
return ((px >> 4) & 0x000F) | ((px >> 8) & 0x00F0) | ((px >> 12) & 0x0F00) | ((px >> 16) & 0xF000);
44
}
45
46
inline u16 BGRA8888toRGB565(u32 px) {
47
return ((px >> 19) & 0x001F) | ((px >> 5) & 0x07E0) | ((px << 8) & 0xF800);
48
}
49
50
inline u16 BGRA8888toRGBA4444(u32 px) {
51
return ((px >> 20) & 0x000F) | ((px >> 8) & 0x00F0) | ((px << 4) & 0x0F00) | ((px >> 16) & 0xF000);
52
}
53
54
inline u16 BGRA8888toRGBA5551(u32 px) {
55
return ((px >> 19) & 0x001F) | ((px >> 6) & 0x03E0) | ((px << 7) & 0x7C00) | ((px >> 16) & 0x8000);
56
}
57
58
inline u16 RGBA8888toRGBA5551(u32 px) {
59
return ((px >> 3) & 0x001F) | ((px >> 6) & 0x03E0) | ((px >> 9) & 0x7C00) | ((px >> 16) & 0x8000);
60
}
61
62
inline u32 RGBA4444ToRGBA8888(u16 src) {
63
const u32 r = (src & 0x000F) << 0;
64
const u32 g = (src & 0x00F0) << 4;
65
const u32 b = (src & 0x0F00) << 8;
66
const u32 a = (src & 0xF000) << 12;
67
const u32 c = r | g | b | a;
68
return c | (c << 4);
69
}
70
71
inline u32 RGBA5551ToRGBA8888(u16 src) {
72
u32 dark = ((src & 0x1F) << 3) | ((src & 0x3E0) << 6) | ((src & 0x7C00) << 9);
73
// Replicate the top 3 upper bits into the missing lower bits.
74
u32 full = (dark | ((dark >> 5) & 0x070707));
75
if (src >> 15) {
76
full |= 0xFF000000;
77
}
78
return full;
79
}
80
81
inline u32 RGB565ToRGBA8888(u16 src) {
82
u32 dark_rb = ((src & 0x1F) << 3) | ((src & 0xF800) << 8);
83
// Replicate the top 3 upper bits into the missing lower bits.
84
u32 full_rb = (dark_rb | ((dark_rb >> 5) & 0x070007));
85
// Add in green (6 bits instead of 5).
86
u32 dark_g = ((src & 0x7E0) << 5);
87
u32 full_g = dark_g | ((dark_g >> 6) & 0x300);
88
return full_rb | full_g | 0xFF000000;
89
}
90
91
inline u16 RGBA8888ToRGB565(u32 value) {
92
u32 r = (value >> 3) & 0x1F;
93
u32 g = (value >> 5) & (0x3F << 5);
94
u32 b = (value >> 8) & (0x1F << 11);
95
return (u16)(r | g | b);
96
}
97
98
inline u16 RGBA8888ToRGBA5551(u32 value) {
99
u32 r = (value >> 3) & 0x1F;
100
u32 g = (value >> 6) & (0x1F << 5);
101
u32 b = (value >> 9) & (0x1F << 10);
102
u32 a = (value >> 16) & 0x8000;
103
return (u16)(r | g | b | a);
104
}
105
106
// Used in fast sprite path.
107
inline u16 RGBA8888ToRGBA555X(u32 value) {
108
u32 r = (value >> 3) & 0x1F;
109
u32 g = (value >> 6) & (0x1F << 5);
110
u32 b = (value >> 9) & (0x1F << 10);
111
return (u16)(r | g | b);
112
}
113
114
inline u16 RGBA8888ToRGBA4444(u32 value) {
115
const u32 c = value >> 4;
116
const u16 r = (c >> 0) & 0x000F;
117
const u16 g = (c >> 4) & 0x00F0;
118
const u16 b = (c >> 8) & 0x0F00;
119
const u16 a = (c >> 12) & 0xF000;
120
return r | g | b | a;
121
}
122
123
inline u16 RGBA8888ToRGBA444X(u32 value) {
124
const u32 c = value >> 4;
125
const u16 r = (c >> 0) & 0x000F;
126
const u16 g = (c >> 4) & 0x00F0;
127
const u16 b = (c >> 8) & 0x0F00;
128
return r | g | b;
129
}
130
131
// "Complete" set of color conversion functions between the usual formats.
132
133
// TODO: Need to revisit the naming convention of these. Seems totally backwards
134
// now that we've standardized on Draw::DataFormat.
135
//
136
// The functions that have the same bit width of input and output can generally
137
// tolerate being called with src == dst, which is used a lot for ReverseColors
138
// in the GLES backend.
139
140
void ConvertBGRA8888ToRGBA8888(u32 *dst, const u32 *src, u32 numPixels);
141
#define ConvertRGBA8888ToBGRA8888 ConvertBGRA8888ToRGBA8888
142
void ConvertBGRA8888ToRGB888(u8 *dst, const u32 *src, u32 numPixels);
143
144
void ConvertRGBA8888ToRGBA5551(u16 *dst, const u32 *src, u32 numPixels);
145
void ConvertRGBA8888ToRGB565(u16 *dst, const u32 *src, u32 numPixels);
146
void ConvertRGBA8888ToRGBA4444(u16 *dst, const u32 *src, u32 numPixels);
147
void ConvertRGBA8888ToRGB888(u8 *dst, const u32 *src, u32 numPixels);
148
149
void ConvertBGRA8888ToRGBA5551(u16 *dst, const u32 *src, u32 numPixels);
150
void ConvertBGRA8888ToRGB565(u16 *dst, const u32 *src, u32 numPixels);
151
void ConvertBGRA8888ToRGBA4444(u16 *dst, const u32 *src, u32 numPixels);
152
153
void ConvertRGB565ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
154
void ConvertRGBA5551ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
155
void ConvertRGBA4444ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
156
157
void ConvertBGR565ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
158
void ConvertABGR1555ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
159
void ConvertABGR4444ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
160
161
void ConvertRGBA4444ToBGRA8888(u32 *dst, const u16 *src, u32 numPixels);
162
void ConvertRGBA5551ToBGRA8888(u32 *dst, const u16 *src, u32 numPixels);
163
void ConvertRGB565ToBGRA8888(u32 *dst, const u16 *src, u32 numPixels);
164
165
void ConvertRGBA4444ToABGR4444(u16 *dst, const u16 *src, u32 numPixels);
166
void ConvertRGBA5551ToABGR1555(u16 *dst, const u16 *src, u32 numPixels);
167
void ConvertRGB565ToBGR565(u16 *dst, const u16 *src, u32 numPixels);
168
void ConvertBGRA5551ToABGR1555(u16 *dst, const u16 *src, u32 numPixels);
169
170