Zum Hauptinhalt springen

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.

Select Board
Inkplate10_Dither_Kernels.ino
#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 None

Creates 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

TypeNameDescription
uint8_tmodeINKPLATE_1BIT or INKPLATE_3BIT.
display.begin()returns void

Initializes the library and the display. Call it once, at the start of `setup()`.

ReturnsNothing.

display.sdCardInit()returns int16_t

Powers the microSD card and starts the SD interface.

Returns`1` if the card initialized, `0` if it failed.

display.println(value)returns size_t

Same 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

TypeNameDescription
text or numbervalueWhat to print.
display.display(bool _leaveOn = 0)returns void

Does 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

TypeNameDescription
bool_leaveOnKeeps the panel supply on after the update when true. Defaults to 0.
display.clearDisplay()returns void

Clears 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 void

Sets where the next `print()` starts, in pixels from the top left corner.

ReturnsNothing.

Parameters

TypeNameDescription
int16_txHorizontal position in pixels.
int16_tyVertical position in pixels.
display.setTextColor(uint16_t c)returns void

Sets the text color.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tcText color, for example BLACK.
display.setTextSize(uint8_t s)returns void

Scales text by an integer factor. The base font is 5 x 7 pixels.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsScale factor.
display.print(value)returns size_t

Prints 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

TypeNameDescription
text, number or charvalueWhat to print.
intsecond argument (optional)Decimal places for a float, or the base for an integer, for example DEC.
display.image.setDitherKernel(const DitherKernel kernel)returns void

Selects 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

TypeNameDescription
Image::DitherKernelkernelThe kernel to use.
display.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns bool

Draws 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

TypeNameDescription
const char *pathFile name on the card.
intxLeft position in pixels.
intyTop position in pixels.
boolditherDithers the image when true. Defaults to 1.
boolinvertSwaps black and white when true. Defaults to 0.
delay(unsigned long ms)returns void

Arduino function that pauses the sketch.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longmsTime to wait in milliseconds.
SdFile()returns None

Creates 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 void

Turns 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.