diff --git a/Inkplate.cpp b/Inkplate.cpp index 2b86b97..8c2e3e2 100644 --- a/Inkplate.cpp +++ b/Inkplate.cpp @@ -360,7 +360,7 @@ int Inkplate::drawBitmapFromSD(char* fileName, int x, int y) { } } -int Inkplate::drawBitmapFromWeb(WiFiClient* s, int x, int y, int len) { +int Inkplate::drawBitmapFromWeb(WiFiClient* s, int x, int y, int len, bool invert) { struct bitmapHeader bmpHeader; readBmpHeaderWeb(s, &bmpHeader); if (bmpHeader.signature != 0x4D42 || bmpHeader.compression != 0 || !(bmpHeader.color == 1 || bmpHeader.color == 24)) return 0; @@ -373,13 +373,13 @@ int Inkplate::drawBitmapFromWeb(WiFiClient* s, int x, int y, int len) { selectDisplayMode(INKPLATE_1BIT); } - if (bmpHeader.color == 1) drawMonochromeBitmapWeb(s, bmpHeader, x, y, len); - if (bmpHeader.color == 24) drawGrayscaleBitmap24Web(s, bmpHeader, x, y, len); + if (bmpHeader.color == 1) drawMonochromeBitmapWeb(s, bmpHeader, x, y, len, invert); + if (bmpHeader.color == 24) drawGrayscaleBitmap24Web(s, bmpHeader, x, y, len, invert); return 1; } -int Inkplate::drawBitmapFromWeb(char* url, int x, int y) { +int Inkplate::drawBitmapFromWeb(char* url, int x, int y, bool invert) { if (WiFi.status() != WL_CONNECTED) return 0; int ret = 0; @@ -396,7 +396,7 @@ int Inkplate::drawBitmapFromWeb(char* url, int x, int y) { int32_t len = http.getSize(); if (len > 0) { WiFiClient * dat = http.getStreamPtr(); - ret = drawBitmapFromWeb(dat, x, y, len); + ret = drawBitmapFromWeb(dat, x, y, len, invert); } } @@ -899,7 +899,7 @@ int Inkplate::drawGrayscaleBitmap24Sd(SdFile *f, struct bitmapHeader bmpHeader, return 1; } -int Inkplate::drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len) { +int Inkplate::drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert) { int w = bmpHeader.width; int h = bmpHeader.height; uint8_t paddingBits = w % 32; @@ -928,7 +928,8 @@ int Inkplate::drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHead uint8_t c = buf[k++]; uint8_t d = buf[k++]; uint32_t pixelRow = a << 24 | b << 16 | c << 8 | d; - pixelRow = ~pixelRow; + if (invert) + pixelRow = ~pixelRow; for (int n = 0; n < 32; n++) { drawPixel((i * 32) + n + x, h - j + y, !(pixelRow & (1ULL << (31 - n)))); } @@ -939,7 +940,8 @@ int Inkplate::drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHead uint8_t c = buf[k++]; uint8_t d = buf[k++]; uint32_t pixelRow = a << 24 | b << 16 | c << 8 | d; - pixelRow = ~pixelRow; + if (invert) + pixelRow = ~pixelRow; for (int n = 0; n < paddingBits; n++) { drawPixel((i * 32) + n + x, h - j + y, !(pixelRow & (1ULL << (31 - n)))); } @@ -947,11 +949,11 @@ int Inkplate::drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHead } free(buf); - + return 1; } -int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len) { +int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert) { int w = bmpHeader.width; int h = bmpHeader.height; char padding = w % 4; @@ -977,6 +979,11 @@ int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHea uint8_t r = buf[k++]; uint8_t g = buf[k++]; uint8_t b = buf[k++]; + if (invert) { + r = 255 - r; + g = 255 - g; + b = 255 - b; + } //This is the proper way of converting True Color (24 Bit RGB) bitmap file into grayscale, but it takes waaay too much time (full size picture takes about 17s to decode!) //float px = (0.2126 * (readByteFromSD(&file) / 255.0)) + (0.7152 * (readByteFromSD(&file) / 255.0)) + (0.0722 * (readByteFromSD(&file) / 255.0)); //px = pow(px, 1.5); diff --git a/Inkplate.h b/Inkplate.h index 1a63316..3d3f471 100644 --- a/Inkplate.h +++ b/Inkplate.h @@ -133,8 +133,8 @@ class Inkplate : public Adafruit_GFX { uint8_t getDisplayMode(); int drawBitmapFromSD(SdFile* p, int x, int y); int drawBitmapFromSD(char* fileName, int x, int y); - int drawBitmapFromWeb(WiFiClient* s, int x, int y, int len); - int drawBitmapFromWeb(char* url, int x, int y); + int drawBitmapFromWeb(WiFiClient* s, int x, int y, int len, bool invert = false); + int drawBitmapFromWeb(char* url, int x, int y, bool invert = false); int sdCardInit(); SdFat getSdFat(); SPIClass getSPI(); @@ -170,8 +170,8 @@ class Inkplate : public Adafruit_GFX { void readBmpHeaderWeb(WiFiClient *_s, struct bitmapHeader *_h); int drawMonochromeBitmapSd(SdFile *f, struct bitmapHeader bmpHeader, int x, int y); int drawGrayscaleBitmap24Sd(SdFile *f, struct bitmapHeader bmpHeader, int x, int y); - int drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len); - int drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len); + int drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert); + int drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert); void precalculateGamma(uint8_t* c, float gamma); }; diff --git a/examples/2. Advanced Inkplate Features/10-Inkplate_Download_And_Show/10-Inkplate_Download_And_Show.ino b/examples/2. Advanced Inkplate Features/10-Inkplate_Download_And_Show/10-Inkplate_Download_And_Show.ino index 23f967c..191d4a0 100644 --- a/examples/2. Advanced Inkplate Features/10-Inkplate_Download_And_Show/10-Inkplate_Download_And_Show.ino +++ b/examples/2. Advanced Inkplate Features/10-Inkplate_Download_And_Show/10-Inkplate_Download_And_Show.ino @@ -25,8 +25,8 @@ Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome) SdFile file; //Create SdFile object used for accessing files on SD card -const char* ssid = "ssid"; //Your WiFi SSID -const char* password = "password"; //Your WiFi password +const char* ssid = "YourWiFiSSID"; //Your WiFi SSID +const char* password = "YourPass"; //Your WiFi password void setup() { display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) @@ -48,9 +48,10 @@ void setup() { display.partialUpdate(); //Draw the first image from web. - //24 bit example, will take around 20 secs to load. + //Monochromatic bitmap with 1 bit depth. Images like this load quickest. + //The parameter set to true at the end may be used to swap the back and white channels. //Photo taken by: Roberto Fernandez - if(!display.drawBitmapFromWeb("https://varipass.org/neowise.bmp", 0, 0)) { + if(!display.drawBitmapFromWeb("https://varipass.org/neowise_mono.bmp", 0, 0, true)) { //If is something failed (wrong filename or wrong bitmap format), write error message on the screen. //REMEMBER! You can only use Windows Bitmap file with color depth of 1 or 24 bits with no compression! display.println("Image open error"); @@ -58,12 +59,10 @@ void setup() { } display.display(); - delay(5000); - //Draw the second image from web. - //1 bit example, will load very quickly. + //Full color 24 bit images are large and take a long time to load, will take around 20 secs. //Photo taken by: Roberto Fernandez - if(!display.drawBitmapFromWeb("https://varipass.org/neowise_mono.bmp", 0, 0)) { + if(!display.drawBitmapFromWeb("https://varipass.org/neowise.bmp", 0, 0)) { //If is something failed (wrong filename or wrong bitmap format), write error message on the screen. //REMEMBER! You can only use Windows Bitmap file with color depth of 1 or 24 bits with no compression! display.println("Image open error");