Skip to content

Halftone

Fact Value
Also called Halftone screen, Dot screen
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Nonlinear, spatial, amplitude-modulated dot screen
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/artistic/painterly.ts, halftoneFilter
Operator code web/src/core/filters/artistic/painterly.ts, halftonePlane
Channels Brightness only, RGB

Overview

Halftoning makes gray from black ink only, as newspapers print photos. The image is redrawn as round black dots on a turned grid. Each dot covers the share of its cell that the image is dark there, so the eye sees the right gray from a distance.

Two panels: a gray ramp from black to white, and its halftone with large merging dots at the dark end and small dots at the light end. Two panels: a gray ramp from black to white, and its halftone with large merging dots at the dark end and small dots at the light end.
A gray ramp, and its halftone with cells of 8 pixels at 45 degrees.

How it works

  1. Lay a grid of square cells over the image, turned by the screen angle.
  2. Read the darkness of each cell at its center from a box blurred copy.
  3. Find the dot radius whose area inside the cell matches that darkness.
  4. Paint each pixel black inside the dot and white outside, with a soft edge.

Mathematical foundation

\[ a = 1 - \frac{\bar I(c)}{255}, \quad \operatorname{area}\left(\text{disk}(\rho) \cap \text{cell}\right) = a\, s^2, \quad I'(p) = 255 \left(1 - \operatorname{clamp}\left(\rho - |p - c| + \tfrac{1}{2}\right)\right), \qquad s = 8,\ \theta = 45^{\circ} \]

The formula shows the default parameter values.

where

  • \(a\): The ink share of a cell
  • \(\bar I(c)\): The box mean of the image at the cell center
  • \(\rho\): The dot radius
  • \(s, \theta\): The cell size and the screen angle

The screen is a grid of square cells of side s, turned by theta. The darkness a of a cell is one minus the box mean of the image at the cell center c. The box has about the cell's size, and it is read by bilinear interpolation. The dot is a disk centered at c whose area inside the cell is a s^2. Above a quarter of pi, about 0.785, the disk passes the sides of the cell. Then its radius rho comes from the area of a circle cut by a square, solved by bisection. Each pixel is black inside the dot and white outside, with one pixel of anti-aliasing at the edge. So a flat gray keeps its mean tone. These rules are PixelNexus's own. Ulichney's book covers halftoning in depth.

Up to a quarter of pi, about 78.5 percent ink, the dot fits inside the cell and its area is pi rho^2. A larger dot is cut by the four sides of the cell. Each side cuts off a circular segment of area rho^2 acos(h / rho) - h sqrt(rho^2 - h^2), with h half the cell. The area is monotonic in rho, so bisection finds the radius for any ink share. Black ink then covers the whole cell.

Kernel or operator

The Inspector shows the steps of the effect as formulas with the current settings.

Algorithm

  1. Blur the image with a box of about the cell's size.
  2. For each pixel, find its cell on the turned screen and the cell center.
  3. Read the darkness at the cell center and find the dot radius that inks that share of the cell.
  4. Paint the pixel by its distance to the dot center, with a soft one-pixel edge.

Parameters

Parameter Default Values Meaning
Cell size cell 8 3 to 40 px The side of a cell of the screen.
Screen angle angle 45 0 to 90 deg The angle of the screen grid.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

halftoneFilter in web/src/core/filters/artistic/painterly.ts uses halftonePlane, dotRadius and circleInSquare. It is global, since the screen depends on the position in the whole image.

Worked example

Flat gray 32 64 128 192 224
Mean after the halftone 35 65.5 127 191.1 223.1

Flat 96 by 96 grays with cells of 8 pixels at 45 degrees. Each dot inks the cell's share, so the mean tone stays within a few levels. The rest comes from the cells cut by the image edge and from the anti-aliased dot edges.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. The box mean uses running sums.

Artifacts and limitations

Limitations

  • Detail smaller than a cell is lost.
  • One screen for all channels in RGB scope, unlike real color printing with different angles per ink.

Artifacts

  • Moire against fine repeating patterns in the image.

Tests

The reference writes the screen and the dot rules again with NumPy and SciPy, and compares every output value on the test images of the fixtures. Unit tests check that the dot area inside the cell equals the ink share, and that flat grays keep their mean within a few levels.

References

  • Ulichney, R. Digital Halftoning. MIT Press, 1987.