Turn min into macro
[pcsx_rearmed.git] / frontend / cspace.c
... / ...
CommitLineData
1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2011,2012
3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
11#include "cspace.h"
12
13/*
14 * note: these are intended for testing and should be avoided
15 * in favor of NEON version or platform-specific conversion
16 */
17
18#ifndef __arm__
19
20void bgr555_to_rgb565(void *dst_, const void *src_, int bytes)
21{
22 const unsigned int *src = src_;
23 unsigned int *dst = dst_;
24 unsigned int p;
25 int x;
26
27 for (x = 0; x < bytes / 4; x++) {
28 p = src[x];
29 p = ((p & 0x7c007c00) >> 10) | ((p & 0x03e003e0) << 1)
30 | ((p & 0x001f001f) << 11);
31 dst[x] = p;
32 }
33}
34
35#endif
36
37#ifndef __ARM_NEON__
38
39void bgr888_to_rgb565(void *dst_, const void *src_, int bytes)
40{
41 const unsigned char *src = src_;
42 unsigned int *dst = dst_;
43 unsigned int r1, g1, b1, r2, g2, b2;
44
45 for (; bytes >= 6; bytes -= 6, src += 6, dst++) {
46 r1 = src[0] & 0xf8;
47 g1 = src[1] & 0xfc;
48 b1 = src[2] & 0xf8;
49 r2 = src[3] & 0xf8;
50 g2 = src[4] & 0xfc;
51 b2 = src[5] & 0xf8;
52 *dst = (r2 << 24) | (g2 << 19) | (b2 << 13) |
53 (r1 << 8) | (g1 << 3) | (b1 >> 3);
54 }
55}
56
57// TODO?
58void rgb888_to_rgb565(void *dst, const void *src, int bytes) {}
59void bgr888_to_rgb888(void *dst, const void *src, int bytes) {}
60
61#endif // __ARM_NEON__
62
63/* YUV stuff */
64static int yuv_ry[32], yuv_gy[32], yuv_by[32];
65static unsigned char yuv_u[32 * 2], yuv_v[32 * 2];
66
67void bgr_to_uyvy_init(void)
68{
69 int i, v;
70
71 /* init yuv converter:
72 y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0));
73 y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1));
74 u = (int)(8 * 0.565f * (b0 - y0)) + 128;
75 v = (int)(8 * 0.713f * (r0 - y0)) + 128;
76 */
77 for (i = 0; i < 32; i++) {
78 yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f);
79 yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f);
80 yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f);
81 }
82 for (i = -32; i < 32; i++) {
83 v = (int)(8 * 0.565f * i) + 128;
84 if (v < 0)
85 v = 0;
86 if (v > 255)
87 v = 255;
88 yuv_u[i + 32] = v;
89 v = (int)(8 * 0.713f * i) + 128;
90 if (v < 0)
91 v = 0;
92 if (v > 255)
93 v = 255;
94 yuv_v[i + 32] = v;
95 }
96}
97
98void rgb565_to_uyvy(void *d, const void *s, int pixels)
99{
100 unsigned int *dst = d;
101 const unsigned short *src = s;
102 const unsigned char *yu = yuv_u + 32;
103 const unsigned char *yv = yuv_v + 32;
104 int r0, g0, b0, r1, g1, b1;
105 int y0, y1, u, v;
106
107 for (; pixels > 0; src += 2, dst++, pixels -= 2)
108 {
109 r0 = (src[0] >> 11) & 0x1f;
110 g0 = (src[0] >> 6) & 0x1f;
111 b0 = src[0] & 0x1f;
112 r1 = (src[1] >> 11) & 0x1f;
113 g1 = (src[1] >> 6) & 0x1f;
114 b1 = src[1] & 0x1f;
115 y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16;
116 y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16;
117 u = yu[b0 - y0];
118 v = yv[r0 - y0];
119 // valid Y range seems to be 16..235
120 y0 = 16 + 219 * y0 / 31;
121 y1 = 16 + 219 * y1 / 31;
122
123 *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
124 }
125}
126
127void bgr555_to_uyvy(void *d, const void *s, int pixels)
128{
129 unsigned int *dst = d;
130 const unsigned short *src = s;
131 const unsigned char *yu = yuv_u + 32;
132 const unsigned char *yv = yuv_v + 32;
133 int r0, g0, b0, r1, g1, b1;
134 int y0, y1, u, v;
135
136 for (; pixels > 0; src += 2, dst++, pixels -= 2)
137 {
138 b0 = (src[0] >> 10) & 0x1f;
139 g0 = (src[0] >> 5) & 0x1f;
140 r0 = src[0] & 0x1f;
141 b1 = (src[1] >> 10) & 0x1f;
142 g1 = (src[1] >> 5) & 0x1f;
143 r1 = src[1] & 0x1f;
144 y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16;
145 y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16;
146 u = yu[b0 - y0];
147 v = yv[r0 - y0];
148 y0 = 16 + 219 * y0 / 31;
149 y1 = 16 + 219 * y1 / 31;
150
151 *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
152 }
153}
154
155void bgr888_to_uyvy(void *d, const void *s, int pixels)
156{
157 unsigned int *dst = d;
158 const unsigned char *src8 = s;
159 const unsigned char *yu = yuv_u + 32;
160 const unsigned char *yv = yuv_v + 32;
161 int r0, g0, b0, r1, g1, b1;
162 int y0, y1, u, v;
163
164 for (; pixels > 0; src8 += 3*2, dst++, pixels -= 2)
165 {
166 r0 = src8[0], g0 = src8[1], b0 = src8[2];
167 r1 = src8[3], g1 = src8[4], b1 = src8[5];
168 y0 = (r0 * 19595 + g0 * 38470 + b0 * 7471) >> 16;
169 y1 = (r1 * 19595 + g1 * 38470 + b1 * 7471) >> 16;
170 u = yu[(b0 - y0) / 8];
171 v = yv[(r0 - y0) / 8];
172 y0 = 16 + 219 * y0 / 255;
173 y1 = 16 + 219 * y1 / 255;
174
175 *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
176 }
177}