# CRT Effect Algorithms
This document details the algorithms and techniques implemented in `crtimg` to achieve the desired CRT (Cathode Ray Tube) visual effects. The core logic resides primarily in `src/crt.rs`.
## 1. Upscaling (Nearest Neighbor)
To maintain the sharp pixel art aesthetic, the image is upscaled using the **Nearest Neighbor** interpolation method. This method selects the color of the nearest pixel in the original image, avoiding any blending or anti-aliasing that would soften the pixel edges.
- **Implementation**: `image::DynamicImage::resize_exact` with `image::imageops::FilterType::Nearest`.
- **Purpose**: Enlarges the image to a higher resolution (e.g., 2x, 3x, 4x) without introducing blur, making it suitable for subsequent effects that operate on a larger canvas.
## 2. Scanlines
The scanline effect simulates the dark lines between illuminated phosphor lines on a CRT screen. This is achieved by drawing horizontal black lines over specific rows of the upscaled image.
- **Algorithm**:
1. The `scanline_height` parameter determines the thickness of the black lines.
2. For each original row of pixels, a corresponding block of `upscale_factor` rows exists in the upscaled image.
3. Within each block, the last `scanline_height` rows are colored black. The remaining `upscale_factor - scanline_height` rows retain the original upscaled pixel data.
- **Purpose**: Adds a distinctive horizontal line pattern that is characteristic of CRT displays.
## 3. Glow Effect (Multi-stage Gaussian Blur Blending)
The glow effect simulates the luminous bloom of CRT phosphors, where bright areas appear to bleed into surrounding darker regions. This is achieved through a multi-stage additive blending of Gaussian-blurred layers.
- **Algorithm**:
1. Three copies of the upscaled image are created.
2. Each copy is subjected to a Gaussian blur with a different radius, typically increasing with each copy (e.g., `upscale_factor / 2.0`, `upscale_factor`, `upscale_factor * 1.5`). This creates layers of varying blur intensities.
3. A pixel-by-pixel iteration is performed over the original upscaled image.
4. For each pixel, its original RGB values are additively combined with the corresponding pixel values from the three blurred layers, each multiplied by a specific coefficient (e.g., `COEFFICIENT_1X`, `COEFFICIENT_2X`, `COEFFICIENT_3X`).
5. The resulting color values are clamped to the valid 0-255 range.
- **Purpose**: Creates a vibrant, luminous "bloom" around brighter pixels, enhancing the retro CRT feel.
## 4. Barrel Distortion (Backward Mapping with Bilinear Interpolation)
Barrel distortion simulates the outward bulging effect caused by the curved glass of a CRT screen. This is a geometric transformation applied to the image.
- **Algorithm (Backward Mapping)**:
1. Instead of determining where each source pixel goes in the destination (forward mapping), the algorithm works backward: for each pixel in the *destination* image, it calculates where that pixel would have come from in the *source* image.
2. A distortion function `(1 + strength * r^2)` is applied, where `r` is the radial distance from the image center and `strength` is a configurable parameter. This function distorts the radial coordinates.
3. The inverse of this distortion is applied to the destination coordinates to find the corresponding source coordinates.
- **Bilinear Interpolation**:
1. Since the calculated source coordinates (`x_src_f`, `y_src_f`) are typically floating-point numbers, they do not directly correspond to integer pixel locations.
2. **Bilinear interpolation** is used to determine the color of the source pixel. This involves:
- Identifying the four integer-coordinate pixels surrounding `(x_src_f, y_src_f)`.
- Performing linear interpolation horizontally between the top two pixels.
- Performing linear interpolation horizontally between the bottom two pixels.
- Performing linear interpolation vertically between these two interpolated results to get the final color.
- **Purpose**: Mimics the physical curvature of old CRT screens, adding a subtle yet impactful element to the effect.
## 5. Optional Post-Blur
A final, subtle Gaussian blur can be applied to the entire image after all other effects have been processed.
- **Implementation**: `image::DynamicImage::blur` with a small radius (e.g., `0.25`).
- **Purpose**: Softens the overall appearance slightly, which can further enhance the analog feel or smooth out any harsh edges resulting from other effects.