2 * Fast C2P (Chunky-to-Planar) Conversion
4 * Copyright (C) 2003 Geert Uytterhoeven
7 * - This code was inspired by Scout's C2P tutorial
8 * - It assumes to run on a big endian system
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of this archive
15 #include <linux/string.h>
20 * Basic transpose step
23 #define _transp(d, i1, i2, shift, mask) \
25 u32 t = (d[i1] ^ (d[i2] >> shift)) & mask; \
27 d[i2] ^= t << shift; \
30 static inline u32
get_mask(int n
)
56 #define transp_nx1(d, n) \
58 u32 mask = get_mask(n); \
60 _transp(d, 0, 1, n, mask); \
62 _transp(d, 2, 3, n, mask); \
64 _transp(d, 4, 5, n, mask); \
66 _transp(d, 6, 7, n, mask); \
69 #define transp_nx2(d, n) \
71 u32 mask = get_mask(n); \
73 _transp(d, 0, 2, n, mask); \
74 _transp(d, 1, 3, n, mask); \
76 _transp(d, 4, 6, n, mask); \
77 _transp(d, 5, 7, n, mask); \
80 #define transp_nx4(d, n) \
82 u32 mask = get_mask(n); \
83 _transp(d, 0, 4, n, mask); \
84 _transp(d, 1, 5, n, mask); \
85 _transp(d, 2, 6, n, mask); \
86 _transp(d, 3, 7, n, mask); \
89 #define transp(d, n, m) transp_nx ## m(d, n)
93 * Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
95 * - 32 8-bit chunky pixels on input
96 * - permuted planar data on output
99 static void c2p_8bpp(u32 d
[8])
110 * Array containing the permution indices of the planar data after c2p
113 static const int perm_c2p_8bpp
[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
117 * Compose two values, using a bitmask as decision value
118 * This is equivalent to (a & mask) | (b & ~mask)
121 static inline unsigned long comp(unsigned long a
, unsigned long b
,
124 return ((a
^ b
) & mask
) ^ b
;
129 * Store a full block of planar data after c2p conversion
132 static inline void store_planar(char *dst
, u32 dst_inc
, u32 bpp
, u32 d
[8])
136 for (i
= 0; i
< bpp
; i
++, dst
+= dst_inc
)
137 *(u32
*)dst
= d
[perm_c2p_8bpp
[i
]];
142 * Store a partial block of planar data after c2p conversion
145 static inline void store_planar_masked(char *dst
, u32 dst_inc
, u32 bpp
,
150 for (i
= 0; i
< bpp
; i
++, dst
+= dst_inc
)
151 *(u32
*)dst
= comp(d
[perm_c2p_8bpp
[i
]], *(u32
*)dst
, mask
);
156 * c2p - Copy 8-bit chunky image data to a planar frame buffer
157 * @dst: Starting address of the planar frame buffer
158 * @dx: Horizontal destination offset (in pixels)
159 * @dy: Vertical destination offset (in pixels)
160 * @width: Image width (in pixels)
161 * @height: Image height (in pixels)
162 * @dst_nextline: Frame buffer offset to the next line (in bytes)
163 * @dst_nextplane: Frame buffer offset to the next plane (in bytes)
164 * @src_nextline: Image offset to the next line (in bytes)
165 * @bpp: Bits per pixel of the planar frame buffer (1-8)
168 void c2p(u8
*dst
, const u8
*src
, u32 dx
, u32 dy
, u32 width
, u32 height
,
169 u32 dst_nextline
, u32 dst_nextplane
, u32 src_nextline
, u32 bpp
)
172 u32 d
[8], first
, last
, w
;
176 dst
+= dy
*dst_nextline
+(dx
& ~31);
178 first
= ~0UL >> dst_idx
;
179 last
= ~(~0UL >> ((dst_idx
+width
) % 32));
184 if (dst_idx
+width
<= 32) {
185 /* Single destination word */
187 memset(d
, 0, sizeof(d
));
188 memcpy((u8
*)d
+dst_idx
, c
, width
);
191 store_planar_masked(p
, dst_nextplane
, bpp
, d
, first
);
194 /* Multiple destination words */
199 memset(d
, 0, dst_idx
);
200 memcpy((u8
*)d
+dst_idx
, c
, w
);
203 store_planar_masked(p
, dst_nextplane
, bpp
, d
, first
);
212 store_planar(p
, dst_nextplane
, bpp
, d
);
220 memset((u8
*)d
+w
, 0, 32-w
);
222 store_planar_masked(p
, dst_nextplane
, bpp
, d
, last
);