c2p_iplan2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Fast C2P (Chunky-to-Planar) Conversion
  3. *
  4. * Copyright (C) 2003-2008 Geert Uytterhoeven
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/export.h>
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <linux/unaligned.h>
  14. #include "c2p.h"
  15. #include "c2p_core.h"
  16. /*
  17. * Perform a full C2P step on 16 8-bit pixels, stored in 4 32-bit words
  18. * containing
  19. * - 16 8-bit chunky pixels on input
  20. * - permutated planar data (2 planes per 32-bit word) on output
  21. */
  22. static void c2p_16x8(u32 d[4])
  23. {
  24. transp4(d, 8, 2);
  25. transp4(d, 1, 2);
  26. transp4x(d, 16, 2);
  27. transp4x(d, 2, 2);
  28. transp4(d, 4, 1);
  29. }
  30. /*
  31. * Array containing the permutation indices of the planar data after c2p
  32. */
  33. static const int perm_c2p_16x8[4] = { 1, 3, 0, 2 };
  34. /*
  35. * Store a full block of iplan2 data after c2p conversion
  36. */
  37. static inline void store_iplan2(void *dst, u32 bpp, u32 d[4])
  38. {
  39. int i;
  40. for (i = 0; i < bpp/2; i++, dst += 4)
  41. put_unaligned_be32(d[perm_c2p_16x8[i]], dst);
  42. }
  43. /*
  44. * Store a partial block of iplan2 data after c2p conversion
  45. */
  46. static inline void store_iplan2_masked(void *dst, u32 bpp, u32 d[4], u32 mask)
  47. {
  48. int i;
  49. for (i = 0; i < bpp/2; i++, dst += 4)
  50. put_unaligned_be32(comp(d[perm_c2p_16x8[i]],
  51. get_unaligned_be32(dst), mask),
  52. dst);
  53. }
  54. /*
  55. * c2p_iplan2 - Copy 8-bit chunky image data to an interleaved planar
  56. * frame buffer with 2 bytes of interleave
  57. * @dst: Starting address of the planar frame buffer
  58. * @dx: Horizontal destination offset (in pixels)
  59. * @dy: Vertical destination offset (in pixels)
  60. * @width: Image width (in pixels)
  61. * @height: Image height (in pixels)
  62. * @dst_nextline: Frame buffer offset to the next line (in bytes)
  63. * @src_nextline: Image offset to the next line (in bytes)
  64. * @bpp: Bits per pixel of the planar frame buffer (2, 4, or 8)
  65. */
  66. void c2p_iplan2(void *dst, const void *src, u32 dx, u32 dy, u32 width,
  67. u32 height, u32 dst_nextline, u32 src_nextline, u32 bpp)
  68. {
  69. union {
  70. u8 pixels[16];
  71. u32 words[4];
  72. } d;
  73. u32 dst_idx, first, last, w;
  74. const u8 *c;
  75. void *p;
  76. dst += dy*dst_nextline+(dx & ~15)*bpp;
  77. dst_idx = dx % 16;
  78. first = 0xffffU >> dst_idx;
  79. first |= first << 16;
  80. last = 0xffffU ^ (0xffffU >> ((dst_idx+width) % 16));
  81. last |= last << 16;
  82. while (height--) {
  83. c = src;
  84. p = dst;
  85. w = width;
  86. if (dst_idx+width <= 16) {
  87. /* Single destination word */
  88. first &= last;
  89. memset(d.pixels, 0, sizeof(d));
  90. memcpy(d.pixels+dst_idx, c, width);
  91. c += width;
  92. c2p_16x8(d.words);
  93. store_iplan2_masked(p, bpp, d.words, first);
  94. p += bpp*2;
  95. } else {
  96. /* Multiple destination words */
  97. w = width;
  98. /* Leading bits */
  99. if (dst_idx) {
  100. w = 16 - dst_idx;
  101. memset(d.pixels, 0, dst_idx);
  102. memcpy(d.pixels+dst_idx, c, w);
  103. c += w;
  104. c2p_16x8(d.words);
  105. store_iplan2_masked(p, bpp, d.words, first);
  106. p += bpp*2;
  107. w = width-w;
  108. }
  109. /* Main chunk */
  110. while (w >= 16) {
  111. memcpy(d.pixels, c, 16);
  112. c += 16;
  113. c2p_16x8(d.words);
  114. store_iplan2(p, bpp, d.words);
  115. p += bpp*2;
  116. w -= 16;
  117. }
  118. /* Trailing bits */
  119. w %= 16;
  120. if (w > 0) {
  121. memcpy(d.pixels, c, w);
  122. memset(d.pixels+w, 0, 16-w);
  123. c2p_16x8(d.words);
  124. store_iplan2_masked(p, bpp, d.words, last);
  125. }
  126. }
  127. src += src_nextline;
  128. dst += dst_nextline;
  129. }
  130. }
  131. EXPORT_SYMBOL_GPL(c2p_iplan2);
  132. MODULE_LICENSE("GPL");