PedroFellipeAntunes/halftone-java
Java program to apply a halftone filter to an image.
Halftone
This project is a Java Maven application built Swing that applies the effect of halftone to an image, with dots, lines or sine waves as the pattern. This code uses a codebase from my other project to separate the CMYK/RGB colors.
|
|
|
|
Table of Contents
Features
- Apply Halftone Effect
- Patterns:
Dots,Polygons,Triangles,Stippling,Lines,Sine Waves,Flow Lines.
- Patterns:
- Full CMYK/RGB Processing
- Separates image into CMYK/RGB channels, applies halftone at predetermined angles, and merges using multiply/screen blend.
- Drag & Drop Support
- Simply drag images into the interface to process them.
- Interactive Controls
- Adjust halftone scale (0–100) and angle (0°–360°) via sliders for live preview.
- Batch Processing
- Drop multiple images at once; each will be processed and automatically saved.
Usage
-
Open the Application
- Double-click
Halftone.jar, or run:java -jar Halftone.jar
- Double-click
-
Configure Settings
- Pattern: Choose between Dots, Squares, Triangles, Lines or Sine Waves.
- Scale Slider: Adjust the size of the halftone elements (0 = minimum, 100 = maximum).
- Angle Slider: Define the angle of the halftone pattern (0°–360°).
- Color Mode: Choose between Default, CMYK or RGB for color processing.
-
Drag & Drop
- Drag one or more images (JPEG, PNG, JPG) directly into the window.
-
Preview and Save
- A live preview will display the result.
- To save, click Save or close the window. Processed images are saved in the same folder as the originals with this pattern:
originalname_Halftone[type;scale;angle].png - Example:
cameraman_Halftone[Lines;50;45.0].png
How It Works
The halftone algorithm works in two broad phases: pre-computing a rotated kernel grid from the source image, then drawing shapes whose size is driven by the luminance of each kernel.
1. Rotation and Bounding Box
Rather than rotating the image itself, the algorithm creates a rotation transform centered on the image for a given angle. It then forward-transforms all four corners of the image through that rotation to compute an axis-aligned bounding box, the smallest rectangle (in rotated space) that fully contains the image.
To better understand the logic behind the kernels try out my kernel simulator!
2. Building the Kernel Grid
The rotated bounding box is divided into a uniform grid of square cells, called kernels, each of side length equal to the chosen scale. For every pixel in the original image, its coordinates are forward-transformed into rotated space, and from there the algorithm determines which kernel cell it falls into. Each kernel accumulates the RGB and alpha values of all pixels assigned to it, allowing the average color and BT.709 luminance to be computed later.
3. Shape Generation (Lines and Sine Waves)
With the kernel grid populated, the line and sine wave patterns iterate row by row. For each kernel in a row, the algorithm:
- Reads the average luminance of that cell. Dark areas produce a large half-thickness value; bright areas produce a small one (pure white produces zero).
- Computes the kernel's center point in rotated space.
- Offsets that center upward by the half-thickness to produce an upper point, and downward to produce a lower point, both still in rotated space.
- Maps both points back into the original image coordinate system using the inverse of the rotation transform, storing them in two parallel arrays:
uppersandlowers.
Once all columns in a row are processed, a single closed polygon is constructed for the entire row: the uppers points are traced left to right, then the lowers points are traced right to left, forming a clockwise-wound closed shape. This means the polygon's top edge narrows and widens with the luminance of the image from left to right, and the bottom edge mirrors it in reverse, together producing a stripe whose local thickness at each position reflects the darkness of the image there.
For sine waves, the center of each stripe is additionally displaced vertically by a sine function before the upper/lower offsets are applied, giving the characteristic wavy appearance while the same polygon-building logic is preserved.
4. CMYK / RGB Processing (Optional)
When a color mode is selected, the image is first separated into its component channels (C, M, Y, K or R, G, B). Each channel is processed independently using a fixed, channel-specific rotation angle (e.g. CMYK uses 15°, 75°, 0°, 45°; RGB uses 0°, 60°, 120°). The resulting single-color halftone images are then composited back together using a multiply blend (CMYK) or screen blend (RGB) to reconstruct a full-color halftone result.
Additional Examples
Angles:
Wave crosshatching:
Stippling:
Flow Lines:
|
|
Overlaying halftones at opposing angles to create a cross-hatching effect, by adding some rotation and spacing error we can mimic a hand made pattern. This effect can be further enhanced by generating an outline (e.g., with Extended Difference of Gaussians).
|
|




















