u8g2_bitmap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. u8g2_bitmap.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8g2.h"
  28. void u8g2_SetBitmapMode(u8g2_t *u8g2, uint8_t is_transparent) {
  29. u8g2->bitmap_transparency = is_transparent;
  30. }
  31. /*
  32. x,y Position on the display
  33. len Length of bitmap line in pixel. Note: This differs from u8glib which had a bytecount here.
  34. b Pointer to the bitmap line.
  35. Only draw pixels which are set.
  36. */
  37. void u8g2_DrawHorizontalBitmap(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b)
  38. {
  39. uint8_t mask;
  40. uint8_t color = u8g2->draw_color;
  41. uint8_t ncolor = (color == 0 ? 1 : 0);
  42. #ifdef U8G2_WITH_INTERSECTION
  43. if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 )
  44. return;
  45. #endif /* U8G2_WITH_INTERSECTION */
  46. mask = 128;
  47. while(len > 0)
  48. {
  49. if ( *b & mask ) {
  50. u8g2->draw_color = color;
  51. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  52. } else if ( u8g2->bitmap_transparency == 0 ) {
  53. u8g2->draw_color = ncolor;
  54. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  55. }
  56. x++;
  57. mask >>= 1;
  58. if ( mask == 0 )
  59. {
  60. mask = 128;
  61. b++;
  62. }
  63. len--;
  64. }
  65. u8g2->draw_color = color;
  66. }
  67. /* u8glib compatible bitmap draw function */
  68. void u8g2_DrawBitmap(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t cnt, u8g2_uint_t h, const uint8_t *bitmap)
  69. {
  70. u8g2_uint_t w;
  71. w = cnt;
  72. w *= 8;
  73. #ifdef U8G2_WITH_INTERSECTION
  74. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  75. return;
  76. #endif /* U8G2_WITH_INTERSECTION */
  77. while( h > 0 )
  78. {
  79. u8g2_DrawHorizontalBitmap(u8g2, x, y, w, bitmap);
  80. bitmap += cnt;
  81. y++;
  82. h--;
  83. }
  84. }
  85. void u8g2_DrawHXBM(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b)
  86. {
  87. uint8_t mask;
  88. uint8_t color = u8g2->draw_color;
  89. uint8_t ncolor = (color == 0 ? 1 : 0);
  90. #ifdef U8G2_WITH_INTERSECTION
  91. if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 )
  92. return;
  93. #endif /* U8G2_WITH_INTERSECTION */
  94. mask = 1;
  95. while(len > 0) {
  96. if ( *b & mask ) {
  97. u8g2->draw_color = color;
  98. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  99. } else if ( u8g2->bitmap_transparency == 0 ) {
  100. u8g2->draw_color = ncolor;
  101. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  102. }
  103. x++;
  104. mask <<= 1;
  105. if ( mask == 0 )
  106. {
  107. mask = 1;
  108. b++;
  109. }
  110. len--;
  111. }
  112. u8g2->draw_color = color;
  113. }
  114. void u8g2_DrawXBM(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h, const uint8_t *bitmap)
  115. {
  116. u8g2_uint_t blen;
  117. blen = w;
  118. blen += 7;
  119. blen >>= 3;
  120. #ifdef U8G2_WITH_INTERSECTION
  121. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  122. return;
  123. #endif /* U8G2_WITH_INTERSECTION */
  124. while( h > 0 )
  125. {
  126. u8g2_DrawHXBM(u8g2, x, y, w, bitmap);
  127. bitmap += blen;
  128. y++;
  129. h--;
  130. }
  131. }
  132. void u8g2_DrawHXBMP(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b)
  133. {
  134. uint8_t mask;
  135. uint8_t color = u8g2->draw_color;
  136. uint8_t ncolor = (color == 0 ? 1 : 0);
  137. #ifdef U8G2_WITH_INTERSECTION
  138. if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 )
  139. return;
  140. #endif /* U8G2_WITH_INTERSECTION */
  141. mask = 1;
  142. while(len > 0)
  143. {
  144. if( u8x8_pgm_read(b) & mask ) {
  145. u8g2->draw_color = color;
  146. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  147. } else if( u8g2->bitmap_transparency == 0 ) {
  148. u8g2->draw_color = ncolor;
  149. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  150. }
  151. x++;
  152. mask <<= 1;
  153. if ( mask == 0 )
  154. {
  155. mask = 1;
  156. b++;
  157. }
  158. len--;
  159. }
  160. u8g2->draw_color = color;
  161. }
  162. void u8g2_DrawXBMP(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h, const uint8_t *bitmap)
  163. {
  164. u8g2_uint_t blen;
  165. blen = w;
  166. blen += 7;
  167. blen >>= 3;
  168. #ifdef U8G2_WITH_INTERSECTION
  169. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  170. return;
  171. #endif /* U8G2_WITH_INTERSECTION */
  172. while( h > 0 )
  173. {
  174. u8g2_DrawHXBMP(u8g2, x, y, w, bitmap);
  175. bitmap += blen;
  176. y++;
  177. h--;
  178. }
  179. }