| 1 | /* |
| 2 | |
| 3 | u8g2_bitmap.c |
| 4 | |
| 5 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 6 | |
| 7 | Copyright (c) 2016, olikraus@gmail.com |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without modification, |
| 11 | are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, this list |
| 14 | of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 17 | list of conditions and the following disclaimer in the documentation and/or other |
| 18 | materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 22 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 32 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | |
| 34 | */ |
| 35 | |
| 36 | #include "u8g2.h" |
| 37 | |
| 38 | |
| 39 | void u8g2_SetBitmapMode(u8g2_t *u8g2, uint8_t is_transparent) { |
| 40 | u8g2->bitmap_transparency = is_transparent; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | x,y Position on the display |
| 45 | len Length of bitmap line in pixel. Note: This differs from u8glib which had a bytecount here. |
| 46 | b Pointer to the bitmap line. |
| 47 | Only draw pixels which are set. |
| 48 | */ |
| 49 | |
| 50 | void u8g2_DrawHorizontalBitmap(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b) |
| 51 | { |
| 52 | uint8_t mask; |
| 53 | uint8_t color = u8g2->draw_color; |
| 54 | uint8_t ncolor = (color == 0 ? 1 : 0); |
| 55 | |
| 56 | #ifdef U8G2_WITH_INTERSECTION |
| 57 | if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 ) |
| 58 | return; |
| 59 | #endif /* U8G2_WITH_INTERSECTION */ |
| 60 | |
| 61 | mask = 128; |
| 62 | while(len > 0) |
| 63 | { |
| 64 | if ( *b & mask ) { |
| 65 | u8g2->draw_color = color; |
| 66 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 67 | } else if ( u8g2->bitmap_transparency == 0 ) { |
| 68 | u8g2->draw_color = ncolor; |
| 69 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 70 | } |
| 71 | |
| 72 | x++; |
| 73 | mask >>= 1; |
| 74 | if ( mask == 0 ) |
| 75 | { |
| 76 | mask = 128; |
| 77 | b++; |
| 78 | } |
| 79 | len--; |
| 80 | } |
| 81 | u8g2->draw_color = color; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* u8glib compatible bitmap draw function */ |
| 86 | 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) |
| 87 | { |
| 88 | u8g2_uint_t w; |
| 89 | w = cnt; |
| 90 | w *= 8; |
| 91 | #ifdef U8G2_WITH_INTERSECTION |
| 92 | if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 ) |
| 93 | return; |
| 94 | #endif /* U8G2_WITH_INTERSECTION */ |
| 95 | |
| 96 | while( h > 0 ) |
| 97 | { |
| 98 | u8g2_DrawHorizontalBitmap(u8g2, x, y, w, bitmap); |
| 99 | bitmap += cnt; |
| 100 | y++; |
| 101 | h--; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | void u8g2_DrawHXBM(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b) |
| 108 | { |
| 109 | uint8_t mask; |
| 110 | uint8_t color = u8g2->draw_color; |
| 111 | uint8_t ncolor = (color == 0 ? 1 : 0); |
| 112 | #ifdef U8G2_WITH_INTERSECTION |
| 113 | if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 ) |
| 114 | return; |
| 115 | #endif /* U8G2_WITH_INTERSECTION */ |
| 116 | |
| 117 | mask = 1; |
| 118 | while(len > 0) { |
| 119 | uint8_t current_bit = (*b) & mask; |
| 120 | #ifdef OLD |
| 121 | if ( current_bit ) { |
| 122 | u8g2->draw_color = color; |
| 123 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 124 | } else if ( u8g2->bitmap_transparency == 0 ) { |
| 125 | u8g2->draw_color = ncolor; |
| 126 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 127 | } |
| 128 | x++; |
| 129 | mask <<= 1; |
| 130 | if ( mask == 0 ) |
| 131 | { |
| 132 | mask = 1; |
| 133 | b++; |
| 134 | } |
| 135 | len--; |
| 136 | #else |
| 137 | u8g2_uint_t run_length = 0; |
| 138 | // Determine the run length of consecutive bits with the same color |
| 139 | while (len > 0 && (current_bit == 0 ? (((*b) & mask) == 0) : (((*b) & mask) != 0 ) )) |
| 140 | { |
| 141 | run_length++; |
| 142 | x++; |
| 143 | mask <<= 1; |
| 144 | if (mask == 0) |
| 145 | { |
| 146 | mask = 1; |
| 147 | b++; |
| 148 | } |
| 149 | len--; |
| 150 | } |
| 151 | if (current_bit) |
| 152 | { |
| 153 | u8g2->draw_color = color; |
| 154 | u8g2_DrawHVLine(u8g2, x - run_length, y, run_length, 0); |
| 155 | } |
| 156 | else if (u8g2->bitmap_transparency == 0) |
| 157 | { |
| 158 | u8g2->draw_color = ncolor; |
| 159 | u8g2_DrawHVLine(u8g2, x - run_length, y, run_length, 0); |
| 160 | } |
| 161 | #endif |
| 162 | } |
| 163 | u8g2->draw_color = color; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | 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) |
| 168 | { |
| 169 | u8g2_uint_t blen; |
| 170 | blen = w; |
| 171 | blen += 7; |
| 172 | blen >>= 3; |
| 173 | #ifdef U8G2_WITH_INTERSECTION |
| 174 | if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 ) |
| 175 | return; |
| 176 | #endif /* U8G2_WITH_INTERSECTION */ |
| 177 | |
| 178 | while( h > 0 ) |
| 179 | { |
| 180 | u8g2_DrawHXBM(u8g2, x, y, w, bitmap); |
| 181 | bitmap += blen; |
| 182 | y++; |
| 183 | h--; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | void u8g2_DrawHXBMP(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b) |
| 193 | { |
| 194 | uint8_t mask; |
| 195 | uint8_t color = u8g2->draw_color; |
| 196 | uint8_t ncolor = (color == 0 ? 1 : 0); |
| 197 | #ifdef U8G2_WITH_INTERSECTION |
| 198 | if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 ) |
| 199 | return; |
| 200 | #endif /* U8G2_WITH_INTERSECTION */ |
| 201 | |
| 202 | mask = 1; |
| 203 | while(len > 0) |
| 204 | { |
| 205 | uint8_t current_bit = u8x8_pgm_read(b) & mask; |
| 206 | //#define OLD |
| 207 | #ifdef OLD |
| 208 | if ( current_bit ) { |
| 209 | u8g2->draw_color = color; |
| 210 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 211 | } else if( u8g2->bitmap_transparency == 0 ) { |
| 212 | u8g2->draw_color = ncolor; |
| 213 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 214 | } |
| 215 | |
| 216 | x++; |
| 217 | mask <<= 1; |
| 218 | if ( mask == 0 ) |
| 219 | { |
| 220 | mask = 1; |
| 221 | b++; |
| 222 | } |
| 223 | len--; |
| 224 | #else |
| 225 | u8g2_uint_t run_length = 0; |
| 226 | // Determine the run length of consecutive bits with the same color |
| 227 | while (len > 0 && (current_bit == 0 ? ((u8x8_pgm_read(b) & mask) == 0) : ((u8x8_pgm_read(b) & mask) != 0 ) )) |
| 228 | { |
| 229 | run_length++; |
| 230 | x++; |
| 231 | mask <<= 1; |
| 232 | if (mask == 0) |
| 233 | { |
| 234 | mask = 1; |
| 235 | b++; |
| 236 | } |
| 237 | len--; |
| 238 | } |
| 239 | if (current_bit) |
| 240 | { |
| 241 | u8g2->draw_color = color; |
| 242 | u8g2_DrawHVLine(u8g2, x - run_length, y, run_length, 0); |
| 243 | } |
| 244 | else if (u8g2->bitmap_transparency == 0) |
| 245 | { |
| 246 | u8g2->draw_color = ncolor; |
| 247 | u8g2_DrawHVLine(u8g2, x - run_length, y, run_length, 0); |
| 248 | } |
| 249 | #endif |
| 250 | } |
| 251 | u8g2->draw_color = color; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | 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) |
| 256 | { |
| 257 | u8g2_uint_t blen; |
| 258 | blen = w; |
| 259 | blen += 7; |
| 260 | blen >>= 3; |
| 261 | #ifdef U8G2_WITH_INTERSECTION |
| 262 | if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 ) |
| 263 | return; |
| 264 | #endif /* U8G2_WITH_INTERSECTION */ |
| 265 | |
| 266 | while( h > 0 ) |
| 267 | { |
| 268 | u8g2_DrawHXBMP(u8g2, x, y, w, bitmap); |
| 269 | bitmap += blen; |
| 270 | y++; |
| 271 | h--; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |