Merge pull request #27 from jpirnay/fix-quantization

fix: Use evenly-spaced dithering thresholds for factory LUT (Patryk Radtke)
This commit is contained in:
jpirnay
2026-04-06 19:34:20 +02:00
committed by GitHub
2 changed files with 30 additions and 63 deletions
+4 -5
View File
@@ -52,14 +52,13 @@ int adjustPixel(int gray) {
return gray; return gray;
} }
// Simple quantization without dithering - divide into 4 levels // Simple quantization without dithering — evenly-spaced midpoint thresholds for factory LUT
// The thresholds are fine-tuned to the X4 display
uint8_t quantizeSimple(int gray) { uint8_t quantizeSimple(int gray) {
if (gray < 45) { if (gray < 43) {
return 0; return 0;
} else if (gray < 70) { } else if (gray < 128) {
return 1; return 1;
} else if (gray < 140) { } else if (gray < 213) {
return 2; return 2;
} else { } else {
return 3; return 3;
+26 -58
View File
@@ -125,37 +125,21 @@ class AtkinsonDitherer {
if (adjusted < 0) adjusted = 0; if (adjusted < 0) adjusted = 0;
if (adjusted > 255) adjusted = 255; if (adjusted > 255) adjusted = 255;
// Quantize to 4 levels // Quantize to 4 levels — evenly-spaced midpoint thresholds for factory LUT
uint8_t quantized; uint8_t quantized;
int quantizedValue; int quantizedValue;
if (false) { // original thresholds if (adjusted < 43) {
if (adjusted < 43) { quantized = 0;
quantized = 0; quantizedValue = 0;
quantizedValue = 0; } else if (adjusted < 128) {
} else if (adjusted < 128) { quantized = 1;
quantized = 1; quantizedValue = 85;
quantizedValue = 85; } else if (adjusted < 213) {
} else if (adjusted < 213) { quantized = 2;
quantized = 2; quantizedValue = 170;
quantizedValue = 170; } else {
} else { quantized = 3;
quantized = 3; quantizedValue = 255;
quantizedValue = 255;
}
} else { // fine-tuned to X4 eink display
if (adjusted < 30) {
quantized = 0;
quantizedValue = 15;
} else if (adjusted < 50) {
quantized = 1;
quantizedValue = 30;
} else if (adjusted < 140) {
quantized = 2;
quantizedValue = 80;
} else {
quantized = 3;
quantizedValue = 210;
}
} }
// Calculate error (only distribute 6/8 = 75%) // Calculate error (only distribute 6/8 = 75%)
@@ -229,37 +213,21 @@ class FloydSteinbergDitherer {
if (adjusted < 0) adjusted = 0; if (adjusted < 0) adjusted = 0;
if (adjusted > 255) adjusted = 255; if (adjusted > 255) adjusted = 255;
// Quantize to 4 levels (0, 85, 170, 255) // Quantize to 4 levels — evenly-spaced midpoint thresholds for factory LUT
uint8_t quantized; uint8_t quantized;
int quantizedValue; int quantizedValue;
if (false) { // original thresholds if (adjusted < 43) {
if (adjusted < 43) { quantized = 0;
quantized = 0; quantizedValue = 0;
quantizedValue = 0; } else if (adjusted < 128) {
} else if (adjusted < 128) { quantized = 1;
quantized = 1; quantizedValue = 85;
quantizedValue = 85; } else if (adjusted < 213) {
} else if (adjusted < 213) { quantized = 2;
quantized = 2; quantizedValue = 170;
quantizedValue = 170; } else {
} else { quantized = 3;
quantized = 3; quantizedValue = 255;
quantizedValue = 255;
}
} else { // fine-tuned to X4 eink display
if (adjusted < 30) {
quantized = 0;
quantizedValue = 15;
} else if (adjusted < 50) {
quantized = 1;
quantizedValue = 30;
} else if (adjusted < 140) {
quantized = 2;
quantizedValue = 80;
} else {
quantized = 3;
quantizedValue = 210;
}
} }
// Calculate error // Calculate error