nxglib_copyrun.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /****************************************************************************
  2. * graphics/nxglib/nxsglib_copyrun.h
  3. *
  4. * Copyright (C) 2010 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __GRAPHICS_NXGLIB_NXGLIB_COPYRUN_H
  36. #define __GRAPHICS_NXGLIB_NXGLIB_COPYRUN_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. /****************************************************************************
  44. * Pre-Processor Definitions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Private Types
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Public Data
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Private Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Public Functions
  60. ****************************************************************************/
  61. /****************************************************************************
  62. * Name: nxgl_copyrun_*bpp
  63. *
  64. * Description:
  65. * Copy a row from an image into run.
  66. *
  67. ****************************************************************************/
  68. #if NXGLIB_BITSPERPIXEL == 1
  69. static inline void
  70. nxgl_copyrun_1bpp(FAR const uint8_t *src, FAR uint8_t *dest,
  71. unsigned int remainder, size_t npixels)
  72. {
  73. uint8_t indata;
  74. uint8_t outdata;
  75. uint8_t nextdata;
  76. unsigned int outpixels = 0;
  77. DEBUGASSERT(remainder > 0 && remainder < 8);
  78. /* Take only the first 8-remainder pixels from the first byte.
  79. * remainder is number between 1 and 7 (not zero!) that represents
  80. * the alignment of the pixel bits in the source.
  81. */
  82. indata = *src++;
  83. #ifdef CONFIG_NX_PACKEDMSFIRST
  84. /* If CONFIG_NX_PACKEDMSFIRST is defined, then bits 0-(remainder-1)
  85. * are carried over to the first pass through the loop. For
  86. * example if remainder == 2:
  87. *
  88. * indata: xxAA AAAA maps to nextdata: AAAA AAxx
  89. */
  90. nextdata = (indata << remainder);
  91. #else
  92. /* If CONFIG_NX_PACKEDMSFIRST is NOT defined, then bits (7-remainder)-7
  93. * are carried over to the first pass through the loop. For example
  94. * if remainder == 2:
  95. *
  96. * indata: AAAA AAxx maps to nextdata: xxAA AAAA
  97. */
  98. nextdata = (indata >> remainder);
  99. #endif
  100. /* Loop until all pixels have been packed into the destination. Note:
  101. * a outpixels increments by 8 so a few extra pixels will be packed on
  102. * the output. This should not be an issue.
  103. */
  104. while (outpixels < npixels)
  105. {
  106. /* Get the next byte from the source */
  107. indata = *src++;
  108. outdata = nextdata;
  109. /* remainder is number between 1 and 7 (not zero!) that represents
  110. * the alignment of the pixel bits in the source.
  111. */
  112. #ifdef CONFIG_NX_PACKEDMSFIRST
  113. /* If CONFIG_NX_PACKEDMSFIRST is defined, then bits (7-remainder)-7
  114. * are carried over from that last pass through the loop. For
  115. * example if remainder == 2:
  116. *
  117. * nextdata = AAAA AAxx - dest = AAAA AABB
  118. * src = BBCC CCCC - nextdata = CCCC CCxx
  119. */
  120. outdata |= (indata >> (8 - remainder));
  121. nextdata = (indata << remainder);
  122. #else
  123. /* If CONFIG_NX_PACKEDMSFIRST is NOT defined, then bits 0-(remainder-1)
  124. * are carried over from that last pass through the loop . For
  125. * example if remainder == 2:
  126. *
  127. * nextdata = xxAA AAAA - dest = BBAA AAAA
  128. * src = CCCC CCBB - nextdata = xxCC CCCC
  129. */
  130. outdata |= (indata << (8 - remainder));
  131. nextdata = (indata >> remainder);
  132. #endif
  133. /* Transfer the byte to the run buffer */
  134. *dest++ = outdata;
  135. outpixels += 8;
  136. }
  137. }
  138. #elif NXGLIB_BITSPERPIXEL == 2
  139. static inline void
  140. nxgl_copyrun_2bpp(FAR const uint8_t *src, FAR uint8_t *dest,
  141. unsigned int remainder, size_t npixels)
  142. {
  143. uint8_t indata;
  144. uint8_t outdata;
  145. uint8_t nextdata;
  146. unsigned int outpixels = 0;
  147. unsigned int shift;
  148. DEBUGASSERT(remainder > 0 && remainder < 4);
  149. /* Take only the first 8-(2*remainder) pixels from the first byte.
  150. * remainder is number between 1 and 3 (not zero!) that represents
  151. * the alignment of the pixel bits in the source.
  152. */
  153. indata = *src++;
  154. shift = (remainder << 1);
  155. #ifdef CONFIG_NX_PACKEDMSFIRST
  156. /* If CONFIG_NX_PACKEDMSFIRST is defined, then bits 0-(2*remainder-1)
  157. * are carried over to the first pass through the loop. For
  158. * example if remainder == 1:
  159. *
  160. * indata: xxAA AAAA maps to nextdata: AAAA AAxx
  161. */
  162. nextdata = (indata << shift);
  163. #else
  164. /* If CONFIG_NX_PACKEDMSFIRST is NOT defined, then bits (7-2*remainder)-7
  165. * are carried over to the first pass through the loop. For example
  166. * if remainder == 1:
  167. *
  168. * indata: AAAA AAxx maps to nextdata: xxAA AAAA
  169. */
  170. nextdata = (indata >> shift);
  171. #endif
  172. /* Loop until all pixels have been packed into the destination. Note:
  173. * a outpixels increments by 8 so a few extra pixels will be packed on
  174. * the output. This should not be an issue.
  175. */
  176. while (outpixels < npixels)
  177. {
  178. /* Get the next byte from the source */
  179. indata = *src++;
  180. outdata = nextdata;
  181. /* remainder is number between 1 and 3 (not zero!) that represents
  182. * the alignment of the pixel bits in the source.
  183. */
  184. #ifdef CONFIG_NX_PACKEDMSFIRST
  185. /* If CONFIG_NX_PACKEDMSFIRST is defined, then bits (7-2*remainder)-7
  186. * are carried over from that last pass through the loop. For example
  187. * if remainder == 1:
  188. *
  189. * nextdata = AAAA AAxx - dest = AAAA AABB
  190. * src = BBCC CCCC - nextdata = CCCC CCxx
  191. */
  192. outdata |= (indata >> (8 - shift));
  193. nextdata = (indata << shift);
  194. #else
  195. /* If CONFIG_NX_PACKEDMSFIRST is NOT defined, then bits 0-(2*remainder-1)
  196. * are carried over from that last pass through the loop. For example
  197. * if remainder == 1:
  198. *
  199. * nextdata = xxAA AAAA - dest = BBAA AAAA
  200. * src = CCCC CCBB - nextdata = xxCC CCCC
  201. */
  202. outdata |= (indata << (8 - shift));
  203. nextdata = (indata >> shift);
  204. #endif
  205. /* Transfer the byte to the run buffer */
  206. *dest++ = outdata;
  207. outpixels += 4;
  208. }
  209. }
  210. #elif NXGLIB_BITSPERPIXEL == 4
  211. static inline void
  212. nxgl_copyrun_4bpp(FAR const uint8_t *src, FAR uint8_t *dest,
  213. unsigned int remainder, size_t npixels)
  214. {
  215. uint8_t indata;
  216. uint8_t outdata;
  217. uint8_t nextdata;
  218. unsigned int outpixels = 0;
  219. DEBUGASSERT(remainder == 1);
  220. /* Take only the first 8-remainder pixels from the first byte.
  221. * remainder is number between 1 and 3 (not zero!) that represents
  222. * the alignment of the pixel bits in the source.
  223. */
  224. indata = *src++;
  225. #ifdef CONFIG_NX_PACKEDMSFIRST
  226. /* If CONFIG_NX_PACKEDMSFIRST is defined, then bits 0-3
  227. * are carried over to the first pass through the loop. For
  228. * example:
  229. *
  230. * indata: xxxx AAAA maps to nextdata: AAAA xxxx
  231. */
  232. nextdata = (indata << 4);
  233. #else
  234. /* If CONFIG_NX_PACKEDMSFIRST is NOT defined, then bits 4-7
  235. * are carried over to the first pass through the loop. For example:
  236. *
  237. * indata: AAAA xxxx maps to nextdata: xxxx AAAA
  238. */
  239. nextdata = (indata >> 4);
  240. #endif
  241. /* Loop until all pixels have been packed into the destination. Note:
  242. * a outpixels increments by 8 so a few extra pixels will be packed on
  243. * the output. This should not be an issue.
  244. */
  245. while (outpixels < npixels)
  246. {
  247. /* Get the next byte from the source */
  248. indata = *src++;
  249. outdata = nextdata;
  250. /* remainder is number between 1 and 3 (not zero!) that represents
  251. * the alignment of the pixel bits in the source.
  252. */
  253. #ifdef CONFIG_NX_PACKEDMSFIRST
  254. /* If CONFIG_NX_PACKEDMSFIRST is defined, then bits 4-7
  255. * are carried over from that last pass through the loop (or are
  256. * ignored initially. For example if remainder == 1:
  257. *
  258. * nextdata = AAAA xxxx - dest = AAAA BBBB
  259. * src = BBBB CCCC - nextdata = CCCC xxxx
  260. */
  261. outdata |= (indata >> 4);
  262. nextdata = (indata << 4);
  263. #else
  264. /* If CONFIG_NX_PACKEDMSFIRST is NOT defined, then bits 0-(remainder-1)
  265. * are carried over from that last pass through the loop (or are
  266. * ignored initially). For example if remainder == 2:
  267. *
  268. * nextdata = xxAA AAAA - dest = BBAA AAAA
  269. * src = CCCC CCBB - nextdata = xxCC CCCC
  270. */
  271. outdata |= (indata << 4);
  272. nextdata = (indata >> 4);
  273. #endif
  274. /* Transfer the byte to the run buffer */
  275. *dest++ = outdata;
  276. outpixels += 2;
  277. }
  278. }
  279. #endif
  280. #endif /* __GRAPHICS_NXGLIB_NXGLIB_COPYRUN_H */