| 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 | |
| 20 | void 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 __ARM64__ |
| 38 | |
| 39 | void 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 | #endif |
| 58 | |
| 59 | #ifndef __ARM_NEON__ |
| 60 | |
| 61 | void bgr888_to_rgb565(void *dst_, const void *src_, int bytes) |
| 62 | { |
| 63 | const unsigned char *src = src_; |
| 64 | unsigned int *dst = dst_; |
| 65 | unsigned int r1, g1, b1, r2, g2, b2; |
| 66 | |
| 67 | for (; bytes >= 6; bytes -= 6, src += 6, dst++) { |
| 68 | r1 = src[0] & 0xf8; |
| 69 | g1 = src[1] & 0xfc; |
| 70 | b1 = src[2] & 0xf8; |
| 71 | r2 = src[3] & 0xf8; |
| 72 | g2 = src[4] & 0xfc; |
| 73 | b2 = src[5] & 0xf8; |
| 74 | *dst = (r2 << 24) | (g2 << 19) | (b2 << 13) | |
| 75 | (r1 << 8) | (g1 << 3) | (b1 >> 3); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // TODO? |
| 80 | void rgb888_to_rgb565(void *dst, const void *src, int bytes) {} |
| 81 | void bgr888_to_rgb888(void *dst, const void *src, int bytes) {} |
| 82 | |
| 83 | #endif // __ARM_NEON__ |
| 84 | |
| 85 | /* YUV stuff */ |
| 86 | static int yuv_ry[32], yuv_gy[32], yuv_by[32]; |
| 87 | static unsigned char yuv_u[32 * 2], yuv_v[32 * 2]; |
| 88 | |
| 89 | void bgr_to_uyvy_init(void) |
| 90 | { |
| 91 | int i, v; |
| 92 | |
| 93 | /* init yuv converter: |
| 94 | y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0)); |
| 95 | y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1)); |
| 96 | u = (int)(8 * 0.565f * (b0 - y0)) + 128; |
| 97 | v = (int)(8 * 0.713f * (r0 - y0)) + 128; |
| 98 | */ |
| 99 | for (i = 0; i < 32; i++) { |
| 100 | yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f); |
| 101 | yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f); |
| 102 | yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f); |
| 103 | } |
| 104 | for (i = -32; i < 32; i++) { |
| 105 | v = (int)(8 * 0.565f * i) + 128; |
| 106 | if (v < 0) |
| 107 | v = 0; |
| 108 | if (v > 255) |
| 109 | v = 255; |
| 110 | yuv_u[i + 32] = v; |
| 111 | v = (int)(8 * 0.713f * i) + 128; |
| 112 | if (v < 0) |
| 113 | v = 0; |
| 114 | if (v > 255) |
| 115 | v = 255; |
| 116 | yuv_v[i + 32] = v; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void rgb565_to_uyvy(void *d, const void *s, int pixels) |
| 121 | { |
| 122 | unsigned int *dst = d; |
| 123 | const unsigned short *src = s; |
| 124 | const unsigned char *yu = yuv_u + 32; |
| 125 | const unsigned char *yv = yuv_v + 32; |
| 126 | int r0, g0, b0, r1, g1, b1; |
| 127 | int y0, y1, u, v; |
| 128 | |
| 129 | for (; pixels > 0; src += 2, dst++, pixels -= 2) |
| 130 | { |
| 131 | r0 = (src[0] >> 11) & 0x1f; |
| 132 | g0 = (src[0] >> 6) & 0x1f; |
| 133 | b0 = src[0] & 0x1f; |
| 134 | r1 = (src[1] >> 11) & 0x1f; |
| 135 | g1 = (src[1] >> 6) & 0x1f; |
| 136 | b1 = src[1] & 0x1f; |
| 137 | y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16; |
| 138 | y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16; |
| 139 | u = yu[b0 - y0]; |
| 140 | v = yv[r0 - y0]; |
| 141 | // valid Y range seems to be 16..235 |
| 142 | y0 = 16 + 219 * y0 / 31; |
| 143 | y1 = 16 + 219 * y1 / 31; |
| 144 | |
| 145 | *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void bgr555_to_uyvy(void *d, const void *s, int pixels) |
| 150 | { |
| 151 | unsigned int *dst = d; |
| 152 | const unsigned short *src = s; |
| 153 | const unsigned char *yu = yuv_u + 32; |
| 154 | const unsigned char *yv = yuv_v + 32; |
| 155 | int r0, g0, b0, r1, g1, b1; |
| 156 | int y0, y1, u, v; |
| 157 | |
| 158 | for (; pixels > 0; src += 2, dst++, pixels -= 2) |
| 159 | { |
| 160 | b0 = (src[0] >> 10) & 0x1f; |
| 161 | g0 = (src[0] >> 5) & 0x1f; |
| 162 | r0 = src[0] & 0x1f; |
| 163 | b1 = (src[1] >> 10) & 0x1f; |
| 164 | g1 = (src[1] >> 5) & 0x1f; |
| 165 | r1 = src[1] & 0x1f; |
| 166 | y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16; |
| 167 | y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16; |
| 168 | u = yu[b0 - y0]; |
| 169 | v = yv[r0 - y0]; |
| 170 | y0 = 16 + 219 * y0 / 31; |
| 171 | y1 = 16 + 219 * y1 / 31; |
| 172 | |
| 173 | *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void bgr888_to_uyvy(void *d, const void *s, int pixels) |
| 178 | { |
| 179 | unsigned int *dst = d; |
| 180 | const unsigned char *src8 = s; |
| 181 | const unsigned char *yu = yuv_u + 32; |
| 182 | const unsigned char *yv = yuv_v + 32; |
| 183 | int r0, g0, b0, r1, g1, b1; |
| 184 | int y0, y1, u, v; |
| 185 | |
| 186 | for (; pixels > 0; src8 += 3*2, dst++, pixels -= 2) |
| 187 | { |
| 188 | r0 = src8[0], g0 = src8[1], b0 = src8[2]; |
| 189 | r1 = src8[3], g1 = src8[4], b1 = src8[5]; |
| 190 | y0 = (r0 * 19595 + g0 * 38470 + b0 * 7471) >> 16; |
| 191 | y1 = (r1 * 19595 + g1 * 38470 + b1 * 7471) >> 16; |
| 192 | u = yu[(b0 - y0) / 8]; |
| 193 | v = yv[(r0 - y0) / 8]; |
| 194 | y0 = 16 + 219 * y0 / 255; |
| 195 | y1 = 16 + 219 * y1 / 255; |
| 196 | |
| 197 | *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u; |
| 198 | } |
| 199 | } |