Skip to content

Add new method drawBitmapFast with better performance #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Adafruit_SPITFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,55 @@ void Adafruit_SPITFT::writePixel(int16_t x, int16_t y, uint16_t color) {
}
}

/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position,
using the specified foreground (for set bits) and background (unset bits)
colors. This is a clone of the Adafruit_GFX method but without a call to
setAddrWindow for each pixel. Instead the window is setup once, and then
all pixels are written. This relies on the address auto-increment of the TFT.
If the image does not fit within the display bounds, clipping is applied.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_SPITFT::drawBitmapFast(int16_t x, int16_t y, uint8_t *bitmap, int16_t w,
int16_t h, uint16_t color, uint16_t bg) {
// Sanity check of X and Y position
if ((x < 0) || (x >= _width) || (y < 0) || (y >= _height))
return;

int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t b = 0;
int16_t wclip = w;
int16_t hclip = h;

// Clipped width and height
if (w > (_width - x))
wclip = _width - x;

if (h > (_height - y))
hclip = _height - y;

startWrite();
setAddrWindow(x, y, wclip, hclip);
for (int16_t j = 0; j < hclip; j++, y++) {
for (int16_t i = 0; i < wclip; i++) {
if (i & 7)
b <<= 1;
else
b = bitmap[j * byteWidth + i / 8];
SPI_WRITE16((b & 0x80) ? color : bg);
}
}
endWrite();
}

/*!
@brief Swap bytes in an array of pixels; converts little-to-big or
big-to-little endian. Used by writePixels() below in some
Expand Down
2 changes: 2 additions & 0 deletions Adafruit_SPITFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class Adafruit_SPITFT : public Adafruit_GFX {
// before ending the transaction. It's more efficient than starting a
// transaction every time.
void writePixel(int16_t x, int16_t y, uint16_t color);
void drawBitmapFast(int16_t x, int16_t y, uint8_t *bitmap,
int16_t w, int16_t h, uint16_t color, uint16_t bg);
void writePixels(uint16_t *colors, uint32_t len, bool block = true,
bool bigEndian = false);
void writeColor(uint16_t color, uint32_t len);
Expand Down