aboutsummaryrefslogtreecommitdiff
path: root/frontend/cspace.c
blob: 33a981df27eb9cde28463d40d6124e8f83786e35 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * (C) Gražvydas "notaz" Ignotas, 2011,2012
 *
 * This work is licensed under the terms of any of these licenses
 * (at your option):
 *  - GNU GPL, version 2 or later.
 *  - GNU LGPL, version 2.1 or later.
 * See the COPYING file in the top-level directory.
 */

#include "cspace.h"

/*
 * note: these are intended for testing and should be avoided
 * in favor of NEON version or platform-specific conversion
 */

#ifndef __arm__

void bgr555_to_rgb565(void *dst_, const void *src_, int bytes)
{
	const unsigned int *src = src_;
	unsigned int *dst = dst_;
	unsigned int p;
	int x;

	for (x = 0; x < bytes / 4; x++) {
		p = src[x];
		p = ((p & 0x7c007c00) >> 10) | ((p & 0x03e003e0) << 1)
			| ((p & 0x001f001f) << 11);
		dst[x] = p;
	}
}

#endif

#ifndef __ARM_NEON__

void bgr888_to_rgb565(void *dst_, const void *src_, int bytes)
{
	const unsigned char *src = src_;
	unsigned int *dst = dst_;
	unsigned int r1, g1, b1, r2, g2, b2;

	for (; bytes >= 6; bytes -= 6, src += 6, dst++) {
		r1 = src[0] & 0xf8;
		g1 = src[1] & 0xfc;
		b1 = src[2] & 0xf8;
		r2 = src[3] & 0xf8;
		g2 = src[4] & 0xfc;
		b2 = src[5] & 0xf8;
		*dst = (r2 << 24) | (g2 << 19) | (b2 << 13) |
			(r1 << 8) | (g1 << 3) | (b1 >> 3);
	}
}

// TODO?
void rgb888_to_rgb565(void *dst, const void *src, int bytes) {}
void bgr888_to_rgb888(void *dst, const void *src, int bytes) {}

#endif // __ARM_NEON__

/* YUV stuff */
static int yuv_ry[32], yuv_gy[32], yuv_by[32];
static unsigned char yuv_u[32 * 2], yuv_v[32 * 2];

void bgr_to_uyvy_init(void)
{
  int i, v;

  /* init yuv converter:
    y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0));
    y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1));
    u = (int)(8 * 0.565f * (b0 - y0)) + 128;
    v = (int)(8 * 0.713f * (r0 - y0)) + 128;
  */
  for (i = 0; i < 32; i++) {
    yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f);
    yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f);
    yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f);
  }
  for (i = -32; i < 32; i++) {
    v = (int)(8 * 0.565f * i) + 128;
    if (v < 0)
      v = 0;
    if (v > 255)
      v = 255;
    yuv_u[i + 32] = v;
    v = (int)(8 * 0.713f * i) + 128;
    if (v < 0)
      v = 0;
    if (v > 255)
      v = 255;
    yuv_v[i + 32] = v;
  }
}

void rgb565_to_uyvy(void *d, const void *s, int pixels)
{
  unsigned int *dst = d;
  const unsigned short *src = s;
  const unsigned char *yu = yuv_u + 32;
  const unsigned char *yv = yuv_v + 32;
  int r0, g0, b0, r1, g1, b1;
  int y0, y1, u, v;

  for (; pixels > 0; src += 2, dst++, pixels -= 2)
  {
    r0 = (src[0] >> 11) & 0x1f;
    g0 = (src[0] >> 6) & 0x1f;
    b0 =  src[0] & 0x1f;
    r1 = (src[1] >> 11) & 0x1f;
    g1 = (src[1] >> 6) & 0x1f;
    b1 =  src[1] & 0x1f;
    y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16;
    y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16;
    u = yu[b0 - y0];
    v = yv[r0 - y0];
    // valid Y range seems to be 16..235
    y0 = 16 + 219 * y0 / 31;
    y1 = 16 + 219 * y1 / 31;

    *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
  }
}

void bgr555_to_uyvy(void *d, const void *s, int pixels)
{
  unsigned int *dst = d;
  const unsigned short *src = s;
  const unsigned char *yu = yuv_u + 32;
  const unsigned char *yv = yuv_v + 32;
  int r0, g0, b0, r1, g1, b1;
  int y0, y1, u, v;

  for (; pixels > 0; src += 2, dst++, pixels -= 2)
  {
    b0 = (src[0] >> 10) & 0x1f;
    g0 = (src[0] >> 5) & 0x1f;
    r0 =  src[0] & 0x1f;
    b1 = (src[1] >> 10) & 0x1f;
    g1 = (src[1] >> 5) & 0x1f;
    r1 =  src[1] & 0x1f;
    y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16;
    y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16;
    u = yu[b0 - y0];
    v = yv[r0 - y0];
    y0 = 16 + 219 * y0 / 31;
    y1 = 16 + 219 * y1 / 31;

    *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
  }
}

void bgr888_to_uyvy(void *d, const void *s, int pixels)
{
  unsigned int *dst = d;
  const unsigned char *src8 = s;
  const unsigned char *yu = yuv_u + 32;
  const unsigned char *yv = yuv_v + 32;
  int r0, g0, b0, r1, g1, b1;
  int y0, y1, u, v;

  for (; pixels > 0; src8 += 3*2, dst++, pixels -= 2)
  {
    r0 = src8[0], g0 = src8[1], b0 = src8[2];
    r1 = src8[3], g1 = src8[4], b1 = src8[5];
    y0 = (r0 * 19595 + g0 * 38470 + b0 * 7471) >> 16;
    y1 = (r1 * 19595 + g1 * 38470 + b1 * 7471) >> 16;
    u = yu[(b0 - y0) / 8];
    v = yv[(r0 - y0) / 8];
    y0 = 16 + 219 * y0 / 255;
    y1 = 16 + 219 * y1 / 255;

    *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
  }
}