c2p.c 5.0 KB

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