nxglib_copyrectangle.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * graphics/nxglib/fb/nxsglib_copyrectangle.c
  3. *
  4. * Copyright (C) 2008-2011 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. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <nuttx/video/fb.h>
  41. #include <nuttx/nx/nxglib.h>
  42. #include "nxglib_bitblit.h"
  43. /****************************************************************************
  44. * Public Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: nxgl_copyrectangle_*bpp
  48. *
  49. * Descripton:
  50. * Copy a rectangular bitmap image into the specific position in the
  51. * framebuffer memory.
  52. *
  53. ****************************************************************************/
  54. void NXGL_FUNCNAME(nxgl_copyrectangle, NXGLIB_SUFFIX)
  55. (FAR struct fb_planeinfo_s *pinfo, FAR const struct nxgl_rect_s *dest,
  56. FAR const void *src, FAR const struct nxgl_point_s *origin,
  57. unsigned int srcstride)
  58. {
  59. FAR const uint8_t *sline;
  60. FAR uint8_t *dline;
  61. unsigned int width;
  62. unsigned int deststride;
  63. unsigned int rows;
  64. #if NXGLIB_BITSPERPIXEL < 8
  65. FAR const uint8_t *sptr;
  66. FAR uint8_t *dptr;
  67. uint8_t leadmask;
  68. uint8_t tailmask;
  69. uint8_t mask;
  70. int lnlen;
  71. #endif
  72. /* Get the width of the framebuffer in bytes */
  73. deststride = pinfo->stride;
  74. /* Get the dimensions of the rectange to fill: width in pixels,
  75. * height in rows
  76. */
  77. width = dest->pt2.x - dest->pt1.x + 1;
  78. rows = dest->pt2.y - dest->pt1.y + 1;
  79. #if NXGLIB_BITSPERPIXEL < 8
  80. # ifdef CONFIG_NX_PACKEDMSFIRST
  81. /* Get the mask for pixels that are ordered so that they pack from the
  82. * MS byte down.
  83. */
  84. leadmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(dest->pt1.x)));
  85. tailmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(dest->pt2.x-1)));
  86. # else
  87. /* Get the mask for pixels that are ordered so that they pack from the
  88. * LS byte up.
  89. */
  90. leadmask = (uint8_t)(0xff << (8 - NXGL_REMAINDERX(dest->pt1.x)));
  91. tailmask = (uint8_t)(0xff >> (8 - NXGL_REMAINDERX(dest->pt1.x-1)));
  92. # endif
  93. #endif
  94. /* Then copy the image */
  95. sline = (FAR const uint8_t *)src + NXGL_SCALEX(dest->pt1.x - origin->x) + (dest->pt1.y - origin->y) * srcstride;
  96. dline = pinfo->fbmem + dest->pt1.y * deststride + NXGL_SCALEX(dest->pt1.x);
  97. while (rows--)
  98. {
  99. #if NXGLIB_BITSPERPIXEL < 8
  100. /* Handle masking of the fractional initial byte */
  101. mask = leadmask;
  102. sptr = sline;
  103. dptr = dline;
  104. lnlen = width;
  105. if (lnlen > 1 && mask)
  106. {
  107. dptr[0] = (dptr[0] & ~mask) | (sptr[0] & mask);
  108. mask = 0xff;
  109. dptr++;
  110. sptr++;
  111. lnlen--;
  112. }
  113. /* Handle masking of the fractional final byte */
  114. mask &= tailmask;
  115. if (lnlen > 0 && mask)
  116. {
  117. dptr[lnlen-1] = (dptr[lnlen-1] & ~mask) | (sptr[lnlen-1] & mask);
  118. lnlen--;
  119. }
  120. /* Handle all of the unmasked bytes in-between */
  121. if (lnlen > 0)
  122. {
  123. NXGL_MEMCPY(dptr, sptr, lnlen);
  124. }
  125. #else
  126. /* Copy the whole line */
  127. NXGL_MEMCPY((NXGL_PIXEL_T *)dline, (NXGL_PIXEL_T *)sline, width);
  128. #endif
  129. dline += deststride;
  130. sline += srcstride;
  131. }
  132. }