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.

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.tsimport { 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;
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
brightness-contrast.react.tsximport { useRef, useState, type ChangeEvent } from 'react';
import type Raster from 'ol/source/Raster.js';
import type { Operation, RasterSourceEvent } from 'ol/source/Raster.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
FullScreenControl,
ImageLayer,
Map,
OSMSource,
RasterSource,
View,
XYZSource,
} from 'cc-openlayers/react';
import { rasterOperation, type RasterData } from './brightness-contrast.shared';
/** A raster source that adjusts the brightness and contrast of OpenStreetMap or satellite imagery pixel by pixel. */
export function BrightnessContrast() {
const [brightness, setBrightness] = useState(0);
const [contrast, setContrast] = useState(0);
const [layer, setLayer] = useState<'osm' | 'imagery'>('osm');
const rasterSource = useRef<Raster>(null);
/** Hands the current slider values to the pixel operation. */
const beforeOperations = (event: Pick<RasterSourceEvent, 'data'>) => {
const data: RasterData = event.data;
data.brightness = brightness;
data.contrast = contrast;
};
const updateRaster = () => rasterSource.current?.refresh();
const slide = (set: (value: number) => void, event: ChangeEvent<HTMLInputElement>) => {
set(event.target.valueAsNumber);
updateRaster();
};
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<FullScreenControl />
<View zoom={14}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<ImageLayer>
<RasterSource
ref={rasterSource}
threads={4}
operationType="image"
operation={rasterOperation as Operation}
onBeforeOperations={beforeOperations}
>
{layer === 'osm' ? (
<OSMSource />
) : (
<XYZSource
url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
crossOrigin=""
/>
)}
</RasterSource>
</ImageLayer>
</Map>
<div className="example-controls">
<label>
<input type="radio" name="layer" checked={layer === 'osm'} onChange={() => setLayer('osm')} />
OpenStreetMap
</label>
<label>
<input type="radio" name="layer" checked={layer === 'imagery'} onChange={() => setLayer('imagery')} />
Satellite imagery
</label>
<label>
Brightness
<input
type="range"
min="-255"
max="255"
value={brightness}
onChange={(event) => slide(setBrightness, event)}
/>
<output>{brightness}</output>
</label>
<label>
Contrast
<input type="range" min="-255" max="255" value={contrast} onChange={(event) => slide(setContrast, event)} />
<output>{contrast}</output>
</label>
</div>
</>
);
}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;
}How the React version works
OpenStreetMap or satellite imagery whose brightness and contrast are adjusted per pixel by a raster source, set with two sliders.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. Both versions import rasterOperation and its data type from the example's .shared.ts file, so the logic exists once. The raster source turns the operation into source text for its worker.
From Angular to React
| Angular | React |
|---|---|
<aol-source-raster [operation]> around <aol-source-osm> or <aol-source-xyz> | <RasterSource operation> around <OSMSource> or <XYZSource>, the input chosen with state |
(beforeOperations) with the slider values | onBeforeOperations reading the values; refresh() on the source after a slider, through viewChild in Angular and useRef in React |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
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-rasterreads pixels from its childaol-source-osmoraol-source-xyzinput and executesrasterOperationin one worker.(beforeOperations)copies reactive slider signals intoevent.databefore each worker run.- Slider updates call
refresh()on the raster source so adjusted colours render immediately. - Radio buttons swap the child source via
@if, withcrossOriginpermitting 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
beforeOperationsevent.data. - Cross-origin tile servers that omit CORS headers taint the internal canvas, preventing
RasterSourcefrom reading input pixels.
Related examples
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.