E-Ink display toolkit - dithering for any palette

Web application for creating images for e-ink displays and conversion images to any palette containing from 2 to 256 colors. The application is part of a project for GooDisplay GDEP073E01 with ESP32 Wemos Lolin32 board.

First: buttons at the top change arrangement of the panels, Center button has three positions, Left and Right buttons have two positions each. Selecting fonts in text layers is only available in the locally running (saved to disk) version. The application does not require internet connection. Function upload to display works only in the application launched from the microcontroller's web server, there is no image receiving endpoint on my site, there will be an error that does not affect the operation of the application.

Converting palette: open an image file with the palette you want to convert other images to, check “File palette” (then you can uncheck), open other images and select original palette from first image. Select a dithering method, select ratio value and adjust color channels to improve the result. In this way, images can be prepared for use in a single palette as in older systems. The result can be saved as binary file, as well as binary file containing palette, and as .bmp file.

When working on microcontrollers with small displays, you can abandon the storage of images palettes and switch to universal generated one.

An example of creating palettes consisting of the basic color combinations used in the application. These palettes are best suited for dithering and are universal for any images.

uint8_t cmp(int a) {
  return (a < 0 ? 0 : a > 255 ? 255 : a);
}

void basicPalette(uint32_t colors[], uint8_t divider) {
  uint8_t k = round(255 / divider);
  uint8_t i = 0;
  for (uint16_t r = 0; r <= k * divider; r += k) {
    for (uint16_t g = 0; g <= k * divider; g += k) {
      for (uint16_t b = 0; b <= k * divider; b += k) {
        colors[i] = (cmp(r) << 16) + (cmp(g) << 8) + cmp(b);
        i++;
      }
    }
  }
}

uint32_t colors8[8];
uint32_t colors27[27];
uint32_t colors64[64];
uint32_t colors125[125];
uint32_t colors216[216];

void setup() {
  basicPalette(colors8, 1);
  basicPalette(colors27, 2);
  basicPalette(colors64, 3);
  basicPalette(colors125, 4);
  basicPalette(colors216, 5);
}

Variant without palette generation, function for calculating color by index (filling arrays with palette colors is given as an example of using the function)

uint8_t cmp(int a) {
  return (a < 0 ? 0 : a > 255 ? 255 : a);
}

uint32_t getPaletteColor(uint8_t colorId, uint8_t divider) {
  uint8_t k = round(255 / divider);
  divider++;
  uint8_t b = colorId / divider;
  uint32_t color = cmp((colorId - b * divider) * k);
  uint8_t g = b / divider;
  color += cmp((b - g * divider) * k) << 8;
  uint8_t r = g / divider;
  color += cmp((g - r * divider) * k) << 16;
  return color;
}

uint32_t colors8[8];
uint32_t colors27[27];
uint32_t colors64[64];
uint32_t colors125[125];
uint32_t colors216[216];

void setup() {
  for (uint8_t i = 0; i < 8; i++) {
    colors8[i] = getPaletteColor(i, 1);
  }
  for (uint8_t i = 0; i < 27; i++) {
    colors27[i] = getPaletteColor(i, 2);
  }
  for (uint8_t i = 0; i < 64; i++) {
    colors64[i] = getPaletteColor(i, 3);
  }
  for (uint8_t i = 0; i < 125; i++) {
    colors125[i] = getPaletteColor(i, 4);
  }
  for (uint8_t i = 0; i < 216; i++) {
    colors216[i] = getPaletteColor(i, 5);
  }
}

Output image to e-ink display must always occur sequentially from one corner of the screen. Use 90 or -90 degree image rotation for upload vertical images to display or to save them. This method will also be useful for displays of other models, sequential image output to display memory will provide performance benefits.

Adapting application to your display: find the beginning of the script block

<script>
const def={w:800,h:480,p:5,b:4,mw:4000,mh:4000,nw:8,nh:8},
pG=[{n:"E-Ink Displays"},{n:"Basic"},{n:"Hardware & Software"}],
pP=[
{n:"Black and white",
p:[0,0xffffff]},

def.w and def.h - display resolution
def.p - display palette from array pP
def.b - default dithering method
def.mw and def.mh - maximum image resolution
def.nw and def.nh - minimum image resolution

Add palette to array pP, to create an unused color, repeat any previously declared color or explicitly specify null

{n:"GooDisplay 4 colors",g:0,
p:[0,0,0,0,0,0xffffff,0,0,0,0,0xffff00,0,0,0,0,0xff0000]},

n - palette name
g - palette group from array pG (not set - without group)
p - array of three-byte color values (rgb)

Creating your own dithering matrices: find declaration of array mF to add Floyd-Steinberg algorithms

const mF=[
{
n:"Floyd-Steinberg",
m:[[1,0,7/16],
[3/16,5/16,1/16]]
},

find declaration of array mB to add Bayer matrices

const mB=[
{
n:"Bayer 2x2",
m(){return BM(1)}
},

add the calculated matrices by analogy

{
n:"Matrix 8x8 1/256",
d:256,
m(){return this.a.map(j=>j.map(i=>i))},
a:[[0,128,32,160,8,136,40,168],
[192,64,224,96,200,72,232,104],
[48,176,16,144,56,184,24,152],
[240,112,208,80,248,120,216,88],
[12,140,44,172,4,132,36,164],
[204,76,236,108,196,68,228,100],
[60,188,28,156,52,180,20,148],
[252,124,220,92,244,116,212,84]]
},

API usage example: weather station on router MikroTik

Application is still under development; much is not yet ready, but a lot of new features have been added. Got into pixel art, which gave me an understanding of how best to prepare images for e-ink displays. Broke the app's functionality down into menu items:

Application

Settings - button "Save defaults" will save the current state of menu, display resolution and palette, dithering method, colors compare and color correction instruments order

Data clearing - deleting saved app data

Advanced - enabling log output for dithering operations (for developments)

Project

Saving and loading project files. The project file can be saved together with the source images and fonts used, or only the layer settings can be saved. Project file is a simple JSON which can be edited manually or with another tool

Local Storage - saving project files to browser storage. Catalog may be empty, and filenames may not be unique. The browser application does not have access to file system, local storage will be used for automation

Web Gallery - galleries with project files and templates for display. You can create an endpoint on your web server that collects project files and returns a JSON response like this:

{
"name": "Heap",
"description": "",
"dtk": [
{
"name": "Montezumas Revenge sketch",
"url": "https://syo.su/dtk/heap/Montezumas_Revenge_sketch_12.dtk",
"palette": "BK0010",
"colors": 4,
"size": "256x256",
"date": "2026-06-11"
}
]
}

"url" must contain a live link, all other fields are relevant only for screen display and sorting. To test adding gallery, you can use (paste into "Gallery url" field then click "Add gallery" button): https://syo.su/dtk/heap.json

Enabled gallery makes request to its endpoint when the application starts, disabled one makes no requests

View

Application appearance, scaling of the resulting image

Display

Display model presets or manual resolution specification and aspect ratio value

Palette

True/High - palette color values ​​in the output binary files

File - build palette from open image files

Custom Palette - creating custom palettes and saving them to browser storage. Palettes can be parsed from text with almost any delimiter. An unused color can be specified by repeating any previous one or by explicitly specifying null. Click on "Colors table ..." will open list of colors, click on this list will copy them to buffer. Button Number - show colors count in palette name. Button Add with Parse add colors to the current ones

Generated Palette - palette generation algorithms. In the resulting binary files, the palette can be saved either as an explicitly defined set of colors or as the number of a generated palette

Colors table - click on color will disable its

Character Cell

Template for working with character cells; not yet finished, partially functional.

Make Image

Creating image layers: image files, text with any fonts, SVG path, resulting image of another window with application

Dithering

Selection of dithering method. The dithering coefficient can be broken down by RGB channels. It can also be negative (an option in the Settings section)

Correction

Main section for converting image to selected palette. Several color comparison algorithms are available. Correction tools can be repositioned and copied. At the correction stage, colors are represented as signed integers; the Clamp button rounds values ​​to the byte range. Cleaning up extra pixels works, but it's still rough

Result Image

Saving the resulting image or sending it to display (if it has a web server implemented to receive images). Click on "Binary image ..." or "Binary picture ..." will open them, click on text will copy it to buffer

Binary image - each pixel is represented by its own color, no image attributes and palette data, set manually file header and footer if need. Data can be expressed as: pixel - first pixel in high-order bits, byte - first pixel in low-order bits, word - first pixel in low-order bits of secon byte, dword - first pixel in low-order bits of fourth byte. Row length can be aligned to byte, two bytes, or four bytes: in which case first pixel of new row will be in next byte, word, or double word. With row alignment disabled, first pixel of new row will be in high-order bits of previous row byte. Enter header or footer file data in hexadecimal values ​​separated by any delimiter or as one string

Binary picture - binary image with header containing attributes and color palette. Bytes 1-4 - keyword and count of bits per color: PIN1, PIN2, PIN4, PIN8 is supported by application. Bytes 5-6 - image width, bytes 7-8 - image hieght. Byte 9 - flags: bits 1-2 - how data expressed: 0 - pixel, 1 - byte, 2 - word, 3 - dword; bits 3-4 - how row aligned: 0 - no, 1 - byte, 2 - word, 3 - dword; bit 5 - number format, affects width and height values ​​of image, number of generated palette and user specified number, palette values in 2 bytes: 0 - little-endian, 1 - big-endian; bits 6-8 reserved. Byte 10 - how palette is represented in file. Byte 10 has value 0 - palette is contained in file header, in this case byte 12 contains index value of last color in file palette (count of colors in palette minus 1); byte 11 - flags: bit 1-6 - color depth: 0 - true color (3 bytes), 25 - high color (2 bytes); bit 7 - alignment of color values: 0 - no, 1 - dword; bit 8 - bit mask of used colors, mask starts at byte 13 and is always same size: 1 byte for 1 and 2 bits per color, 2 bytes for 4 bits per color, 32 bytes for 8 bits per color, data in mask is expressed similarly to image; palette starts after bit mask, if it is contained, or at byte 13; binary image begining after palette data. Byte 10 has value 1-2 - generated palette is used, in this case bytes 11-12 contain value for generation algorithm; binary image begining at byte 13. Byte 10 values 3-254 reserved. Byte 10 has value 255 - user defined, in this case bytes 11-12 contain an arbitrary value specified by user

Application query parameters

The following line will launch the application, open project file from the web server, insert text into the text layers, and trigger saving of the result file as a PNG:

https://syo.su/eink?open[url]=https://syo.su/dtk/templates/JaimeTravezan-Dream-1-BWRY.dtk&layers[2][text]=aug&layers[1][text]=10&layers[0][text]=mon&save[png]=filename

This will also work in the locally saved version of the application

Albert Einstein in 4 colors: black, red, green, blue