nxglib_setpixel.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. * graphics/nxglib/fb/nxglib_setpixel.c
  3. *
  4. * Copyright (C) 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/fb.h>
  41. #include <nuttx/nx/nxglib.h>
  42. #include "nxglib_bitblit.h"
  43. /****************************************************************************
  44. * Pre-Processor Definitions
  45. ****************************************************************************/
  46. #ifndef NXGLIB_SUFFIX
  47. # error "NXGLIB_SUFFIX must be defined before including this header file"
  48. #endif
  49. /****************************************************************************
  50. * Private Types
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Private Data
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Public Data
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Private Functions
  60. ****************************************************************************/
  61. /****************************************************************************
  62. * Public Functions
  63. ****************************************************************************/
  64. /****************************************************************************
  65. * Name: nxgl_setpixel_*bpp
  66. *
  67. * Descripton:
  68. * Draw a single pixel in frambuffer memory at the given position and with
  69. * the given color. This is equivalent to nxgl_fillrectangle_*bpp() with
  70. * a 1x1 rectangle but is more efficient.
  71. *
  72. ****************************************************************************/
  73. void NXGL_FUNCNAME(nxgl_setpixel,NXGLIB_SUFFIX)
  74. (FAR struct fb_planeinfo_s *pinfo,
  75. FAR const struct nxgl_point_s *pos,
  76. NXGL_PIXEL_T color)
  77. {
  78. FAR uint8_t *dest;
  79. #if NXGLIB_BITSPERPIXEL < 8
  80. uint8_t shift;
  81. uint8_t mask;
  82. #else
  83. FAR NXGL_PIXEL_T *pixel;
  84. #endif
  85. /* Get the address of the first byte of the pixel to write */
  86. dest = pinfo->fbmem + pos->y * pinfo->stride + NXGL_SCALEX(pos->x);
  87. #if NXGLIB_BITSPERPIXEL < 8
  88. /* Shift the color into the proper position */
  89. # ifdef CONFIG_NX_PACKEDMSFIRST
  90. #if NXGLIB_BITSPERPIXEL == 1
  91. shift = (7 - (pos->x & 7)); /* Shift is 0, 1, ... 7 */
  92. mask = (1 << shift); /* Mask is 0x01, 0x02, .. 0x80 */
  93. color <<= shift; /* Color is positioned under the mask */
  94. #elif NXGLIB_BITSPERPIXEL == 2
  95. shift = (6 - ((pos->x & 3) << 1)); /* Shift is 0, 2, 4, or 6 */
  96. mask = (3 << shift); /* Mask is 0x03, 0x0c, 0x30, or 0xc0 */
  97. color <<= shift; /* Color is positioned under the mask */
  98. #elif NXGLIB_BITSPERPIXEL == 4
  99. shift = (4 - ((pos->x & 1) << 2)); /* Shift is 0 or 4 */
  100. mask = (15 << shift); /* Mask is 0x0f or 0xf0 */
  101. color <<= shift; /* Color is positioned under the mask */
  102. #else
  103. # error "Unsupport pixel depth"
  104. #endif
  105. # else /* CONFIG_NX_PACKEDMSFIRST */
  106. #if NXGLIB_BITSPERPIXEL == 1
  107. shift = (pos->x & 7); /* Shift is 0, 1, ... 7 */
  108. mask = (1 << shift); /* Mask is 0x01, 0x02, .. 0x80 */
  109. color <<= shift; /* Color is positioned under the mask */
  110. #elif NXGLIB_BITSPERPIXEL == 2
  111. shift = (pos->x & 3) << 1; /* Shift is 0, 2, 4, or 6 */
  112. mask = (3 << shift); /* Mask is 0x03, 0x0c, 0x30, or 0xc0 */
  113. color <<= shift; /* Color is positioned under the mask */
  114. #elif NXGLIB_BITSPERPIXEL == 4
  115. shift = (pos->x & 1) << 2; /* Shift is 0 or 4 */
  116. mask = (15 << shift); /* Mask is 0x0f or 0xf0 */
  117. color <<= shift; /* Color is positioned under the mask */
  118. #else
  119. # error "Unsupport pixel depth"
  120. #endif
  121. #endif /* CONFIG_NX_PACKEDMSFIRST */
  122. /* Handle masking of the fractional byte */
  123. *dest = (*dest & ~mask) | (color & mask);
  124. #else
  125. /* Write the pixel (proper alignment assumed) */
  126. pixel = (FAR NXGL_PIXEL_T *)dest;
  127. *pixel = color;
  128. #endif
  129. }