Skip to content

Commit 08467dd

Browse files
authored
Merge pull request #5 from Codaea/copilot/fix-e36851cb-ba18-4737-9684-70cef7d240d9
Enhance dithering options: Add new algorithms for image processing in…
2 parents d5fc79b + f0e7cbc commit 08467dd

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,36 @@ Prints an image from base64-encoded data.
196196
**Parameters:**
197197
- `data` (string): Base64-encoded image data (PNG format recommended)
198198
- `alignment` (string): Image alignment - `"left"`, `"center"`, or `"right"`
199-
- `dither-mode` (string): Dithering algorithm - `"floydsteinberg"`, `"atkinson"`, `"burkes"`, and `"none"`.
199+
- `dither_mode` (string): Dithering algorithm to apply to the image. Available options:
200+
- **Error Diffusion Algorithms:**
201+
- `"floydsteinberg"` - Floyd-Steinberg dithering (classic, good general purpose)
202+
- `"atkinson"` - Atkinson dithering (popular in retro Mac applications)
203+
- `"burkes"` - Burkes dithering (reduces artifacts)
204+
- `"jarvisjudiceninke"` - Jarvis-Judice-Ninke dithering (high quality, more processing)
205+
- `"sierra"` - Sierra dithering (good balance of quality and speed)
206+
- `"sierralite"` - Sierra Lite dithering (faster version of Sierra)
207+
- `"stucki"` - Stucki dithering (high quality, similar to Jarvis-Judice-Ninke)
208+
- `"stevenpigeon"` - Steven Pigeon dithering (experimental algorithm)
209+
- `"simple2d"` - Simple 2D dithering (basic algorithm)
210+
- `"tworowsierra"` - Two Row Sierra dithering (variation of Sierra)
211+
- `"falsefloydsteinberg"` - False Floyd-Steinberg (simplified version)
212+
- **Ordered Dithering Algorithms:**
213+
- `"clustereddot4x4"` - 4x4 clustered dot pattern (creates dot patterns)
214+
- `"clustereddot6x6"` - 6x6 clustered dot pattern (finer dot patterns)
215+
- `"clustereddot8x8"` - 8x8 clustered dot pattern (finest dot patterns)
216+
- **No Dithering:**
217+
- `"none"` - No dithering applied (direct conversion to black/white)
218+
219+
### About Dithering
220+
221+
Dithering is a technique used to convert grayscale or color images to black and white (monochrome) for thermal printers. Since thermal printers can only print black dots, dithering algorithms determine how to represent different shades of gray using patterns of black and white pixels.
222+
223+
**When to use different algorithms:**
224+
- **Floyd-Steinberg** - Best general-purpose choice, good for most images
225+
- **Atkinson** - Great for images with fine details and text
226+
- **Sierra/Stucki** - Excellent for photographs and smooth gradients
227+
- **Ordered dithering (ClusteredDot)** - Best for images that should maintain a "halftone" newspaper-like appearance
228+
- **None** - Use when you want sharp, high-contrast black and white conversion
200229

201230
## Response Codes
202231

@@ -322,6 +351,16 @@ Here's a complete example that demonstrates printing a receipt with multiple ele
322351
"type": "feed",
323352
"lines": 1
324353
},
354+
{
355+
"type": "image",
356+
"data": "data:image/png;base64,iVBORw0KGgoAAAANSU...",
357+
"alignment": "center",
358+
"dither_mode": "sierra"
359+
},
360+
{
361+
"type": "feed",
362+
"lines": 1
363+
},
325364
{
326365
"type": "line",
327366
"content": "Thank you for your visit!",

main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,42 @@ func processImage(i Image) image.Image {
6262
case "atkinson":
6363
d.Matrix = dither.Atkinson
6464
return d.Dither(i.img)
65+
case "clustereddot8x8":
66+
d.Mapper = dither.PixelMapperFromMatrix(dither.ClusteredDot8x8, 0.5)
67+
return d.Dither(i.img)
68+
case "clustereddot4x4":
69+
d.Mapper = dither.PixelMapperFromMatrix(dither.ClusteredDot4x4, 0.5)
70+
return d.Dither(i.img)
71+
case "clustereddot6x6":
72+
d.Mapper = dither.PixelMapperFromMatrix(dither.ClusteredDot6x6, 0.5)
73+
return d.Dither(i.img)
6574
case "burkes":
6675
d.Matrix = dither.Burkes
6776
return d.Dither(i.img)
77+
case "jarvisjudiceninke":
78+
d.Matrix = dither.JarvisJudiceNinke
79+
return d.Dither(i.img)
80+
case "sierra":
81+
d.Matrix = dither.Sierra
82+
return d.Dither(i.img)
83+
case "sierralite":
84+
d.Matrix = dither.SierraLite
85+
return d.Dither(i.img)
86+
case "stucki":
87+
d.Matrix = dither.Stucki
88+
return d.Dither(i.img)
89+
case "stevenpigeon":
90+
d.Matrix = dither.StevenPigeon
91+
return d.Dither(i.img)
92+
case "simple2d":
93+
d.Matrix = dither.Simple2D
94+
return d.Dither(i.img)
95+
case "tworowsierra":
96+
d.Matrix = dither.TwoRowSierra
97+
return d.Dither(i.img)
98+
case "falsefloydsteinberg":
99+
d.Matrix = dither.FalseFloydSteinberg
100+
return d.Dither(i.img)
68101
case "none":
69102
return i.img
70103
default:

0 commit comments

Comments
 (0)