Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

Brightness and contrast with raster operations

Adjust OpenStreetMap and satellite imagery brightness and contrast per pixel in OpenLayers using interactive RasterSource sliders in Angular.

Satellite imagery of central Kraków with raised brightness and contrast, the values 20 and 50 set below it.
A screenshot of the example. Map data © OpenStreetMap contributors. Imagery: Esri World Imagery (Esri, Maxar, Earthstar Geographics and the GIS User Community).

What this example shows

Centred on Kraków, radio buttons switch the input backdrop between OpenStreetMap and satellite imagery while two range sliders (Brightness and Contrast) reprocess every pixel in browser worker threads to brighten dark shadows or boost washed-out contrast on the fly.

When you need it

Underexposed aerial surveys, faded archival map scans and low-contrast thematic rasters become much easier to interpret when analysts can fine-tune brightness and contrast interactively in the browser.

Processing input tiles through RasterSource works across any CORS-enabled raster layer even when using standard 2D canvas rendering.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

brightness-contrast.shared.ts
/** The slider values the pixel operation gets from `beforeoperations`. */
export interface RasterData {
  brightness: number;
  contrast: number;
}

/**
 * @see https://github.com/canastro/image-filter-brightness/blob/master/src/transform.js
 * @see https://github.com/canastro/image-filter-contrast/blob/master/src/transform.js
 */
export function rasterOperation(imageDatas: [ImageData], data: RasterData): ImageData {
  const [imageData] = imageDatas;

  const pixels = imageData.data;
  const pixelsLength = pixels.length;
  const factor = (259 * (data.contrast + 255)) / (255 * (259 - data.contrast));

  for (let i = 0; i < pixelsLength; i += 4) {
    pixels[i] += data.brightness;
    pixels[i + 1] += data.brightness;
    pixels[i + 2] += data.brightness;

    pixels[i] = factor * (pixels[i] - 128) + 128;
    pixels[i + 1] = factor * (pixels[i + 1] - 128) + 128;
    pixels[i + 2] = factor * (pixels[i + 2] - 128) + 128;
  }

  return imageData;
}
brightness-contrast.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import type { RasterSourceEvent } from 'ol/source/Raster';
import {
  ControlFullScreenComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerImageComponent,
  MapComponent,
  SourceOsmComponent,
  SourceRasterComponent,
  SourceXYZComponent,
  ViewComponent,
} from 'cc-openlayers';
import { type RasterData, rasterOperation } from './brightness-contrast.shared';

/** A raster source that adjusts the brightness and contrast of OpenStreetMap or satellite imagery pixel by pixel. */
@Component({
  selector: 'app-brightness-contrast',
  imports: [
    ControlFullScreenComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerImageComponent,
    MapComponent,
    SourceOsmComponent,
    SourceRasterComponent,
    SourceXYZComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-control-fullscreen />
      <aol-view [zoom]="14">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-image>
        <aol-source-raster
          [threads]="4"
          operationType="image"
          [operation]="operation"
          (beforeOperations)="beforeOperations($event)"
        >
          @if (layer() === 'osm') {
            <aol-source-osm />
          } @else {
            <aol-source-xyz
              url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
              crossOrigin=""
            />
          }
        </aol-source-raster>
      </aol-layer-image>
    </aol-map>
    <div class="example-controls">
      <label>
        <input type="radio" name="layer" [checked]="layer() === 'osm'" (change)="layer.set('osm')" />
        OpenStreetMap
      </label>
      <label>
        <input type="radio" name="layer" [checked]="layer() === 'imagery'" (change)="layer.set('imagery')" />
        Satellite imagery
      </label>
      <label>
        Brightness
        <input
          type="range"
          min="-255"
          max="255"
          [value]="brightness()"
          (input)="brightness.set(number($event)); updateRaster()"
        />
        <output>{{ brightness() }}</output>
      </label>
      <label>
        Contrast
        <input
          type="range"
          min="-255"
          max="255"
          [value]="contrast()"
          (input)="contrast.set(number($event)); updateRaster()"
        />
        <output>{{ contrast() }}</output>
      </label>
    </div>
  `,
})
export class BrightnessContrast {
  readonly brightness = signal(0);
  readonly contrast = signal(0);
  readonly layer = signal<'osm' | 'imagery'>('osm');
  readonly rasterSource = viewChild.required(SourceRasterComponent);
  protected readonly operation = rasterOperation;

  /** Hands the current slider values to the pixel operation. */
  beforeOperations(event: Pick<RasterSourceEvent, 'data'>): void {
    const data: RasterData = event.data;
    data.brightness = this.brightness();
    data.contrast = this.contrast();
  }

  updateRaster(): void {
    this.rasterSource().instance.refresh();
  }

  protected number(event: Event): number {
    return (event.target as HTMLInputElement).valueAsNumber;
  }
}

Architecture and OpenLayers mechanics

aol-source-raster wraps either aol-source-osm or aol-source-xyz (switched cleanly via @if, with crossOrigin enabled for pixel access) and runs rasterOperation in a single Raster worker.

(beforeOperations) copies the current brightness and contrast signal values into event.data—the sole state object visible inside worker threads—and dragging either slider calls refresh() on the raster source to trigger an immediate pass.

How it works

  • aol-source-raster reads pixels from its child aol-source-osm or aol-source-xyz input and executes rasterOperation in one worker.
  • (beforeOperations) copies reactive slider signals into event.data before each worker run.
  • Slider updates call refresh() on the raster source so adjusted colours render immediately.
  • Radio buttons swap the child source via @if, with crossOrigin permitting cross-origin pixel reads.

Pitfalls

  • Worker functions execute in isolated threads without access to Angular/React component properties—pass all dynamic slider numbers exclusively via beforeOperations event.data.
  • Cross-origin tile servers that omit CORS headers taint the internal canvas, preventing RasterSource from reading input pixels.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.