c2p.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Fast C2P (Chunky-to-Planar) Conversion
  3. *
  4. * Copyright (C) 2003 Geert Uytterhoeven
  5. *
  6. * NOTES:
  7. * - This code was inspired by Scout's C2P tutorial
  8. * - It assumes to run on a big endian system
  9. *
  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
  12. * for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include "c2p.h"
  17. /*
  18. * Basic transpose step
  19. */
  20. #define _transp(d, i1, i2, shift, mask) \
  21. do { \
  22. u32 t = (d[i1] ^ (d[i2] >> shift)) & mask; \
  23. d[i1] ^= t; \
  24. d[i2] ^= t << shift; \
  25. } while (0)
  26. static inline u32 get_mask(int n)
  27. {
  28. switch (n) {
  29. case 1:
  30. return 0x55555555;
  31. break;
  32. case 2:
  33. return 0x33333333;
  34. break;
  35. case 4:
  36. return 0x0f0f0f0f;
  37. break;
  38. case 8:
  39. return 0x00ff00ff;
  40. break;
  41. case 16:
  42. return 0x0000ffff;
  43. break;
  44. }
  45. return 0;
  46. }
  47. #define transp_nx1(d, n) \
  48. do { \
  49. u32 mask = get_mask(n); \
  50. /* First block */ \
  51. _transp(d, 0, 1, n, mask); \
  52. /* Second block */ \
  53. _transp(d, 2, 3, n, mask); \
  54. /* Third block */ \
  55. _transp(d, 4, 5, n, mask); \
  56. /* Fourth block */ \
  57. _transp(d, 6, 7, n, mask); \
  58. } while (0)
  59. #define transp_nx2(d, n) \
  60. do { \
  61. u32 mask = get_mask(n); \
  62. /* First block */ \
  63. _transp(d, 0, 2, n, mask); \
  64. _transp(d, 1, 3, n, mask); \
  65. /* Second block */ \
  66. _transp(d, 4, 6, n, mask); \
  67. _transp(d, 5, 7, n, mask); \
  68. } while (0)
  69. #define transp_nx4(d, n) \
  70. do { \
  71. u32 mask = get_mask(n); \
  72. _transp(d, 0, 4, n, mask); \
  73. _transp(d, 1, 5, n, mask); \
  74. _transp(d, 2, 6, n, mask); \
  75. _transp(d, 3, 7, n, mask); \
  76. } while (0)
  77. #define transp(d, n, m) transp_nx ## m(d, n)
  78. /*
  79. * Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
  80. * containing
  81. * - 32 8-bit chunky pixels on input
  82. * - permuted planar data on output
  83. */
  84. static void c2p_8bpp(u32 d[8])
  85. {
  86. transp(d, 16, 4);
  87. transp(d, 8, 2);
  88. transp(d, 4, 1);
  89. transp(d, 2, 4);
  90. transp(d, 1, 2);
  91. }
  92. /*
  93. * Array containing the permution indices of the planar data after c2p
  94. */
  95. static const int perm_c2p_8bpp[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
  96. /*
  97. * Compose two values, using a bitmask as decision value
  98. * This is equivalent to (a & mask) | (b & ~mask)
  99. */
  100. static inline unsigned long comp(unsigned long a, unsigned long b,
  101. unsigned long mask)
  102. {
  103. return ((a ^ b) & mask) ^ b;
  104. }
  105. /*
  106. * Store a full block of planar data after c2p conversion
  107. */
  108. static inline void store_planar(char *dst, u32 dst_inc, u32 bpp, u32 d[8])
  109. {
  110. int i;
  111. for (i = 0; i < bpp; i++, dst += dst_inc)
  112. *(u32 *)dst = d[perm_c2p_8bpp[i]];
  113. }
  114. /*
  115. * Store a partial block of planar data after c2p conversion
  116. */
  117. static inline void store_planar_masked(char *dst, u32 dst_inc, u32 bpp,
  118. u32 d[8], u32 mask)
  119. {
  120. int i;
  121. for (i = 0; i < bpp; i++, dst += dst_inc)
  122. *(u32 *)dst = comp(d[perm_c2p_8bpp[i]], *(u32 *)dst, mask);
  123. }
  124. /*
  125. * c2p - Copy 8-bit chunky image data to a planar frame buffer
  126. * @dst: Starting address of the planar frame buffer
  127. * @dx: Horizontal destination offset (in pixels)
  128. * @dy: Vertical destination offset (in pixels)
  129. * @width: Image width (in pixels)
  130. * @height: Image height (in pixels)
  131. * @dst_nextline: Frame buffer offset to the next line (in bytes)
  132. * @dst_nextplane: Frame buffer offset to the next plane (in bytes)
  133. * @src_nextline: Image offset to the next line (in bytes)
  134. * @bpp: Bits per pixel of the planar frame buffer (1-8)
  135. */
  136. void c2p(u8 *dst, const u8 *src, u32 dx, u32 dy, u32 width, u32 height,
  137. u32 dst_nextline, u32 dst_nextplane, u32 src_nextline, u32 bpp)
  138. {
  139. int dst_idx;
  140. u32 d[8], first, last, w;
  141. const u8 *c;
  142. u8 *p;
  143. dst += dy*dst_nextline+(dx & ~31);
  144. dst_idx = dx % 32;
  145. first = ~0UL >> dst_idx;
  146. last = ~(~0UL >> ((dst_idx+width) % 32));
  147. while (height--) {
  148. c = src;
  149. p = dst;
  150. w = width;
  151. if (dst_idx+width <= 32) {
  152. /* Single destination word */
  153. first &= last;
  154. memset(d, 0, sizeof(d));
  155. memcpy((u8 *)d+dst_idx, c, width);
  156. c += width;
  157. c2p_8bpp(d);
  158. store_planar_masked(p, dst_nextplane, bpp, d, first);
  159. p += 4;
  160. } else {
  161. /* Multiple destination words */
  162. w = width;
  163. /* Leading bits */
  164. if (dst_idx) {
  165. w = 32 - dst_idx;
  166. memset(d, 0, dst_idx);
  167. memcpy((u8 *)d+dst_idx, c, w);
  168. c += w;
  169. c2p_8bpp(d);
  170. store_planar_masked(p, dst_nextplane, bpp, d, first);
  171. p += 4;
  172. w = width-w;
  173. }
  174. /* Main chunk */
  175. while (w >= 32) {
  176. memcpy(d, c, 32);
  177. c += 32;
  178. c2p_8bpp(d);
  179. store_planar(p, dst_nextplane, bpp, d);
  180. p += 4;
  181. w -= 32;
  182. }
  183. /* Trailing bits */
  184. w %= 32;
  185. if (w > 0) {
  186. memcpy(d, c, w);
  187. memset((u8 *)d+w, 0, 32-w);
  188. c2p_8bpp(d);
  189. store_planar_masked(p, dst_nextplane, bpp, d, last);
  190. }
  191. }
  192. src += src_nextline;
  193. dst += dst_nextline;
  194. }
  195. }
  196. EXPORT_SYMBOL_GPL(c2p);
  197. MODULE_LICENSE("GPL");