Compare dithering kernels
What this example shows
Drawing the same picture from the microSD card once per dithering kernel, so you can compare the results. Dithering spreads the error of reducing an image to the display's limited grey levels over neighboring pixels. Call setDitherKernel() before draw(), because it only affects draw calls that follow with dithering enabled.
Example code
Trimmed from the pinned example: only the file header comment is removed, the code is unchanged.
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif
#include "Inkplate.h"
Inkplate display(INKPLATE_3BIT);
SdFile file;
Image::DitherKernel kernels[] = {
Image::FloydSteinberg, Image::JarvisJudiceNinke, Image::Atkinson, Image::Burkes,
Image::Stucki, Image::SierraLite, Image::ReducedDiffusion,
};
const char *kernelNames[] = {
"Floyd-Steinberg", "Jarvis-Judice-Ninke", "Atkinson", "Burkes", "Stucki", "Sierra Lite", "Reduced Diffusion",
};
const uint8_t kernelCount = sizeof(kernels) / sizeof(kernels[0]);
void setup()
{
display.begin();
if (!display.sdCardInit())
{
display.println("SD card error!");
display.display();
return;
}
for (uint8_t i = 0; i < kernelCount; i++)
{
display.clearDisplay();
display.setCursor(0, 0);
display.setTextColor(BLACK);
display.setTextSize(2);
display.print("Kernel: ");
display.println(kernelNames[i]);
// Select the kernel used for all subsequent dithered draws
display.image.setDitherKernel(kernels[i]);
if (!display.image.draw("picture1.jpg", 0, 30, true))
{
display.println("Image open error");
}
display.display();
delay(5000);
}
display.sdCardSleep();
}
void loop()
{
// Nothing...
}
Functions used
Inkplate(uint8_t mode)returns NoneCreates the display object. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` gives 8 levels of grey but only full updates.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | mode | INKPLATE_1BIT or INKPLATE_3BIT. |
display.begin()returns voidInitializes the library and the display. Call it once, at the start of `setup()`.
ReturnsNothing.
display.sdCardInit()returns int16_tPowers the microSD card and starts the SD interface.
Returns`1` if the card initialized, `0` if it failed.
display.println(value)returns size_tSame as `print()`, then moves the cursor to the start of the next line. It comes from the Arduino `Print` class that `Adafruit_GFX` builds on.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text or number | value | What to print. |
display.display(bool _leaveOn = 0)returns voidDoes a full refresh: sends the frame buffer to the panel. If `_leaveOn` is true, the panel supply stays on after the refresh, which saves time on the next update.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | Keeps the panel supply on after the update when true. Defaults to 0. |
display.clearDisplay()returns voidClears the frame buffer in memory. It does not change what the panel shows until you call `display()`.
ReturnsNothing.
display.setCursor(int16_t x, int16_t y)returns voidSets where the next `print()` starts, in pixels from the top left corner.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int16_t | x | Horizontal position in pixels. |
int16_t | y | Vertical position in pixels. |
display.setTextColor(uint16_t c)returns voidSets the text color.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | c | Text color, for example BLACK. |
display.setTextSize(uint8_t s)returns voidScales text by an integer factor. The base font is 5 x 7 pixels.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | s | Scale factor. |
display.print(value)returns size_tPrints text or a number at the cursor. The `print` functions come from the Arduino `Print` class that `Adafruit_GFX` builds on. For a number, an optional second argument sets the decimal places of a float or the number base of an integer.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text, number or char | value | What to print. |
int | second argument (optional) | Decimal places for a float, or the base for an integer, for example DEC. |
display.image.setDitherKernel(const DitherKernel kernel)returns voidSelects the dithering kernel used by draw calls that follow with dithering enabled. The choices are `Image::FloydSteinberg`, `Image::JarvisJudiceNinke`, `Image::Atkinson`, `Image::Burkes`, `Image::Stucki`, `Image::SierraLite` and `Image::ReducedDiffusion`.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
Image::DitherKernel | kernel | The kernel to use. |
display.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns boolDraws an image file from the SD card. The format (BMP, JPEG or PNG) is picked from the file.
Returns`true` if the image drew, `false` if it failed (for example a wrong file name or an unsupported format).
Parameters
| Type | Name | Description |
|---|---|---|
const char * | path | File name on the card. |
int | x | Left position in pixels. |
int | y | Top position in pixels. |
bool | dither | Dithers the image when true. Defaults to 1. |
bool | invert | Swaps black and white when true. Defaults to 0. |
delay(unsigned long ms)returns voidArduino function that pauses the sketch.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | ms | Time to wait in milliseconds. |
SdFile()returns NoneCreates a file object for reading and writing files on the card. `SdFile` is part of the SdFat library bundled with the Inkplate library.
ReturnsConstructor; no return value.
display.sdCardSleep()returns voidTurns off the power to the microSD card and sets the SD interface pins to inputs to save energy.
ReturnsNothing.
What to expect
Put a file named picture1.jpg in the root of the card. The display shows Kernel: followed by the kernel name and the picture below it. It changes to the next kernel every 5 seconds, in this order: Floyd-Steinberg, Jarvis-Judice-Ninke, Atkinson, Burkes, Stucki, Sierra Lite, Reduced Diffusion. If the picture cannot be opened, the display shows Image open error. If the card does not start, it shows SD card error!.
The example header describes the kernels as follows: Floyd-Steinberg is the default and classic error diffusion, Jarvis-Judice-Ninke has wider diffusion that is smoother and slower, Atkinson has lighter diffusion and less noise, Burkes is a simplified Jarvis-Judice-Ninke variant, Stucki is similar to Jarvis-Judice-Ninke but sharper, Sierra Lite is lightweight and fast, and Reduced Diffusion is minimal, faster and coarser.