Skip to main content

Dither kernels

What this example shows​

Draw the same picture from the microSD card once with each of the seven dithering kernels the library offers, so you can compare them. A kernel controls how the error from reducing the picture to the panel's six colors is spread to neighboring pixels; the kernels trade off sharpness, noise and speed. The screen shows the kernel's name above the picture and refreshes once per kernel, five seconds apart.

You need a FAT or FAT32 formatted microSD card with a file named picture1.jpg on it, inserted in Inkplate.

Example code​

Select Board
Inkplate7SPECTRA_Dither_Kernels.ino
#include "Inkplate.h"

Inkplate display;
SdFile file;

ImageColor::DitherKernel kernels[] = {
ImageColor::FloydSteinberg, ImageColor::JarvisJudiceNinke, ImageColor::Atkinson, ImageColor::Burkes,
ImageColor::Stucki, ImageColor::SierraLite, ImageColor::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()returns None

Creates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument.

ReturnsConstructor; no return value.

display.begin()returns void

Initializes the library, the peripherals and the frame buffer. Call it once in setup().

ReturnsNothing.

SdFile()returns None

SdFat class, bundled with the Inkplate library: a file object on the microSD card. The sketch creates one as `file` and does not use it further.

ReturnsConstructor; no return value.

display.sdCardInit()returns int16_t

Switches on the microSD card's supply, starts its SPI bus and mounts the card.

Returns1 if the card was initialized, 0 if it failed.

display.image.setDitherKernel(const DitherKernel kernel)returns void

Selects the error-diffusion kernel used by dithered draws made afterwards. The kernels are ImageColor::FloydSteinberg (the default), ImageColor::JarvisJudiceNinke, ImageColor::Atkinson, ImageColor::Burkes, ImageColor::Stucki, ImageColor::SierraLite and ImageColor::ReducedDiffusion.

ReturnsNothing.

display.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns bool

Opens an image file on the microSD card by name and draws it into the frame buffer with its top-left corner at (x, y). The sketch draws picture1.jpg at (0, 30) with dithering on.

Returnstrue if the image was drawn, false if the file could not be opened or decoded.

display.clearDisplay()returns void

Fills the frame buffer with white. It does not change the panel.

ReturnsNothing.

display.setCursor(int16_t x, int16_t y)returns void

Sets where the next printed text starts.

ReturnsNothing.

display.setTextColor(uint16_t c)returns void

Sets the text color, here BLACK.

ReturnsNothing.

display.setTextSize(uint8_t s)returns void

Sets the text magnification, here 2.

ReturnsNothing.

display.print(value)returns size_t

Prints text into the frame buffer, like Serial.print().

ReturnsNumber of bytes printed.

display.println(text)returns size_t

Prints text into the frame buffer and moves to the next line.

ReturnsNumber of bytes printed.

display.display(bool _leaveOn = 0)returns void

Sends the whole frame buffer to the panel and refreshes it.

ReturnsNothing.

delay(ms)returns void

Arduino core function: waits for the given number of milliseconds, here 5000.

ReturnsNothing.

display.sdCardSleep()returns void

Sets the card's SPI pins to inputs and switches off the card's supply, to save power in deep sleep.

ReturnsNothing.

What to expect​

The screen shows Kernel: Floyd-Steinberg above picture1.jpg. Every five seconds it refreshes with the next kernel's name and the same picture, in this order: Floyd-Steinberg, Jarvis-Judice-Ninke, Atkinson, Burkes, Stucki, Sierra Lite, Reduced Diffusion. After the last one the card is switched off and the last picture stays on the screen.

If the card cannot be started, the screen shows SD card error!; if the picture cannot be opened, Image open error appears under the kernel name. The sketch prints nothing to the Serial Monitor.

🔗Open the complete example

examples/Inkplate7SPECTRA/Advanced/Other/Inkplate7SPECTRA_Dither_Kernels

→