Web and SD card dithering added.

This commit is contained in:
nitko12 2020-08-10 09:12:23 +02:00
parent 88419ad50b
commit 95bc91e1da
3 changed files with 152 additions and 95 deletions

View File

@ -456,7 +456,7 @@ int Inkplate::drawBitmapFromSD(char *fileName, int x, int y, bool dither, bool i
} }
} }
int Inkplate::drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool invert) int Inkplate::drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool dither, bool invert)
{ {
struct bitmapHeader bmpHeader; struct bitmapHeader bmpHeader;
readBmpHeaderWeb(s, &bmpHeader); readBmpHeaderWeb(s, &bmpHeader);
@ -478,14 +478,14 @@ int Inkplate::drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool inver
if (bmpHeader.color == 4) if (bmpHeader.color == 4)
drawGrayscaleBitmap4Web(s, bmpHeader, x, y, len, invert); drawGrayscaleBitmap4Web(s, bmpHeader, x, y, len, invert);
if (bmpHeader.color == 8) if (bmpHeader.color == 8)
drawGrayscaleBitmap8Web(s, bmpHeader, x, y, len, invert); drawGrayscaleBitmap8Web(s, bmpHeader, x, y, len, dither, invert);
if (bmpHeader.color == 24) if (bmpHeader.color == 24)
drawGrayscaleBitmap24Web(s, bmpHeader, x, y, len, invert); drawGrayscaleBitmap24Web(s, bmpHeader, x, y, len, dither, invert);
return 1; return 1;
} }
int Inkplate::drawBitmapFromWeb(char *url, int x, int y, bool invert) int Inkplate::drawBitmapFromWeb(char *url, int x, int y, bool dither, bool invert)
{ {
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
return 0; return 0;
@ -506,7 +506,7 @@ int Inkplate::drawBitmapFromWeb(char *url, int x, int y, bool invert)
if (len > 0) if (len > 0)
{ {
WiFiClient *dat = http.getStreamPtr(); WiFiClient *dat = http.getStreamPtr();
ret = drawBitmapFromWeb(dat, x, y, len, invert); ret = drawBitmapFromWeb(dat, x, y, len, dither, invert);
} }
} }
@ -1405,7 +1405,7 @@ int Inkplate::drawGrayscaleBitmap4Web(WiFiClient *s, struct bitmapHeader bmpHead
return 1; return 1;
} }
int Inkplate::drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert) int Inkplate::drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool dither, bool invert)
{ {
int w = bmpHeader.width; int w = bmpHeader.width;
int h = bmpHeader.height; int h = bmpHeader.height;
@ -1416,7 +1416,7 @@ int Inkplate::drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHead
if (buf == NULL) if (buf == NULL)
return 0; return 0;
int pnt = 0; uint32_t pnt = 0;
while (pnt < total) while (pnt < total)
{ {
int toread = s->available(); int toread = s->available();
@ -1428,23 +1428,51 @@ int Inkplate::drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHead
} }
} }
int i, j, k = bmpHeader.startRAW - 34; uint8_t *bufferPtr;
uint8_t *f_pointer = buf + (bmpHeader.startRAW - 34);
int i, j;
if (dither) {
bufferPtr = pixelBuffer;
for (i = 0; i < w; i++)
pixelBuffer[i] = *(f_pointer++);
ditherStart(buf, bufferPtr, w, invert, 8);
}
for (j = 0; j < h; j++) for (j = 0; j < h; j++)
{ {
bufferPtr = pixelBuffer;
for (i = 0; i < w; i++)
pixelBuffer[i] = *(f_pointer++);
if (dither && j != h - 1) {
ditherLoadNextLine(buf, bufferPtr, w, invert, 8);
}
for (i = 0; i < w; i++) for (i = 0; i < w; i++)
{ {
if (dither)
drawPixel(i + x, h - 1 - j + y, ditherGetPixel(i, j, w, h) >> 5);
else {
uint8_t px = 0; uint8_t px = 0;
if (invert) if (invert)
px = 255 - buf[k++]; px = 255 - *(bufferPtr++);
else else
px = buf[k++]; px = *(bufferPtr++);
drawPixel(i + x, h - 1 - j + y, px >> 5); drawPixel(i + x, h - 1 - j + y, px >> 5);
} }
}
if (dither)
ditherSwap(w);
if (padding) if (padding)
{ {
for (int p = 0; p < 4 - padding; p++) for (int p = 0; p < 4 - padding; p++)
{ {
k++; ++bufferPtr;
} }
} }
} }
@ -1454,7 +1482,7 @@ int Inkplate::drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHead
return 1; return 1;
} }
int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert) int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool dither, bool invert)
{ {
int w = bmpHeader.width; int w = bmpHeader.width;
int h = bmpHeader.height; int h = bmpHeader.height;
@ -1477,9 +1505,29 @@ int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHea
} }
} }
int i, j, k = bmpHeader.startRAW - 34; uint8_t *bufferPtr;
uint8_t *f_pointer = buf + (bmpHeader.startRAW - 34);
int i, j;
if (dither) {
bufferPtr = pixelBuffer;
for (i = 0; i < w; i++)
pixelBuffer[i] = *(f_pointer++);
ditherStart(buf, bufferPtr, w, invert, 8);
}
for (j = 0; j < h; j++) for (j = 0; j < h; j++)
{ {
bufferPtr = pixelBuffer;
for (i = 0; i < w; i++)
pixelBuffer[i] = *(f_pointer++);
if (dither && j != h - 1) {
ditherLoadNextLine(buf, bufferPtr, w, invert, 8);
}
for (i = 0; i < w; i++) for (i = 0; i < w; i++)
{ {
//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!) //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!)
@ -1488,20 +1536,28 @@ int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHea
//display.drawPixel(i + x, h - j + y, (uint8_t)(px*7)); //display.drawPixel(i + x, h - j + y, (uint8_t)(px*7));
//So then, we are convertng it to grayscale using good old average and gamma correction (from LUT). With this metod, it is still slow (full size image takes 4 seconds), but much beter than prev mentioned method. //So then, we are convertng it to grayscale using good old average and gamma correction (from LUT). With this metod, it is still slow (full size image takes 4 seconds), but much beter than prev mentioned method.
if (dither)
drawPixel(i + x, h - 1 - j + y, ditherGetPixel(i, j, w, h) >> 5);
else {
uint8_t px = 0; uint8_t px = 0;
if (invert) if (invert)
px = ((255 - buf[k++]) * 2126 / 10000) + ((255 - buf[k++]) * 7152 / 10000) + ((255 - buf[k++]) * 722 / 10000); px = ((255 - *(bufferPtr++)) * 2126 / 10000) + ((255 - *(bufferPtr++)) * 7152 / 10000) + ((255 - *(bufferPtr++)) * 722 / 10000);
else else
px = (buf[k++] * 2126 / 10000) + (buf[k++] * 7152 / 10000) + (buf[k++] * 722 / 10000); px = (*(bufferPtr++) * 2126 / 10000) + (*(bufferPtr++) * 7152 / 10000) + (*(bufferPtr++) * 722 / 10000);
//drawPixel(i + x, h - j + y, gammaLUT[px]); //drawPixel(i + x, h - j + y, gammaLUT[px]);
drawPixel(i + x, h - 1 - j + y, px >> 5); drawPixel(i + x, h - 1 - j + y, px >> 5);
//drawPixel(i + x, h - j + y, px/32); //drawPixel(i + x, h - j + y, px/32);
} }
}
if (dither)
ditherSwap(w);
if (padding) if (padding)
{ {
for (int p = 0; p < padding; p++) for (int p = 0; p < padding; p++)
{ {
k++; ++bufferPtr;
} }
} }
} }

View File

@ -201,8 +201,8 @@ public:
uint8_t getDisplayMode(); uint8_t getDisplayMode();
int drawBitmapFromSD(SdFile *p, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromSD(SdFile *p, int x, int y, bool dither = false, bool invert = false);
int drawBitmapFromSD(char *fileName, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromSD(char *fileName, int x, int y, bool dither = false, bool invert = false);
int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool invert = false); int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool dither = false, bool invert = false);
int drawBitmapFromWeb(char *url, int x, int y, bool invert = false); int drawBitmapFromWeb(char *url, int x, int y, bool dither = false, bool invert = false);
void drawThickLine(int x1, int y1, int x2, int y2, int color, float thickness); void drawThickLine(int x1, int y1, int x2, int y2, int color, float thickness);
void drawGradientLine(int x1, int y1, int x2, int y2, int color1, int color2, float thickness = -1); void drawGradientLine(int x1, int y1, int x2, int y2, int color1, int color2, float thickness = -1);
int sdCardInit(); int sdCardInit();
@ -250,8 +250,8 @@ private:
int drawGrayscaleBitmap24Sd(SdFile *f, struct bitmapHeader bmpHeader, int x, int y, bool dither, bool invert); int drawGrayscaleBitmap24Sd(SdFile *f, struct bitmapHeader bmpHeader, int x, int y, bool dither, bool invert);
int drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert); int drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert);
int drawGrayscaleBitmap4Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert); int drawGrayscaleBitmap4Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert);
int drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool invert); int drawGrayscaleBitmap8Web(WiFiClient *s, struct bitmapHeader bmpHeader, int x, int y, int len, bool dither, bool invert);
int drawGrayscaleBitmap24Web(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 dither, bool invert);
void precalculateGamma(uint8_t *c, float gamma); void precalculateGamma(uint8_t *c, float gamma);
}; };

View File

@ -21,8 +21,8 @@
#include "WiFi.h" //Include library for WiFi #include "WiFi.h" //Include library for WiFi
Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome) Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome)
const char* ssid = "YourWiFiSSID"; //Your WiFi SSID const char* ssid = "e-radionica.com"; //Your WiFi SSID
const char* password = "YourPass"; //Your WiFi password const char* password = "croduino"; //Your WiFi password
void setup() { void setup() {
display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) display.begin(); //Init Inkplate library (you should call this function ONLY ONCE)
@ -45,11 +45,12 @@ void setup() {
//Draw the first image from web. //Draw the first image from web.
//Monochromatic bitmap with 1 bit depth. Images like this load quickest. //Monochromatic bitmap with 1 bit depth. Images like this load quickest.
//NOTE: Both drawBitmapFromWeb methods allow for an optional fourth "invert" parameter. Setting this parameter to true //NOTE: Both drawBitmapFromWeb methods allow for an optional fifth "invert" parameter. Setting this parameter to true
//will flip all colors on the image, making black white and white black. This may be necessary when exporting bitmaps from //will flip all colors on the image, making black white and white black. This may be necessary when exporting bitmaps from
//certain softwares. //certain softwares.
//Forth parameter will dither the image.
//Photo taken by: Roberto Fernandez //Photo taken by: Roberto Fernandez
if(!display.drawBitmapFromWeb("https://varipass.org/neowise_mono.bmp", 0, 0, true)) { if (!display.drawBitmapFromWeb("https://varipass.org/neowise_mono.bmp", 0, 0, false, true)) {
//If is something failed (wrong filename or wrong bitmap format), write error message on the screen. //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, 4, 8 or 24 bits with no compression! //REMEMBER! You can only use Windows Bitmap file with color depth of 1, 4, 8 or 24 bits with no compression!
display.println("Image open error"); display.println("Image open error");
@ -73,7 +74,7 @@ void setup() {
//Get the response length and make sure it is not 0. //Get the response length and make sure it is not 0.
int32_t len = http.getSize(); int32_t len = http.getSize();
if (len > 0) { if (len > 0) {
if(!display.drawBitmapFromWeb(http.getStreamPtr(), 0, 0, len)) { if (!display.drawBitmapFromWeb(http.getStreamPtr(), 0, 0, len)) {
//If is something failed (wrong filename or wrong bitmap format), write error message on the screen. //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, 4, 8 or 24 bits with no compression! //REMEMBER! You can only use Windows Bitmap file with color depth of 1, 4, 8 or 24 bits with no compression!
display.println("Image open error"); display.println("Image open error");