/** * Preset numbl filter scripts. * * Each is a function `out = filterImage(img)` where: * - img : H x W x 3 array of doubles in [0, 255] (RGB) * - out : H x W x 3 (color) or H x W (grayscale) array in [0, 255] * * MATLAB syntax (this is what numbl runs). Values outside 0-255 are clamped * when the image is displayed. */ export interface Example { name: string; code: string; } export const EXAMPLES: Example[] = [ { name: "Invert", code: `function out = filterImage(img) % Invert each channel out = 255 - img; end `, }, { name: "Grayscale", code: `function out = filterImage(img) % Luminance-weighted grayscale, copied back to 3 channels g = 0.2989 * img(:,:,1) + 0.5870 * img(:,:,2) + 0.1140 * img(:,:,3); out = cat(3, g, g, g); end `, }, { name: "Brighten", code: `function out = filterImage(img) % Scale brightness (min keeps it in range) out = min(255, img * 1.4); end `, }, { name: "Sepia", code: `function out = filterImage(img) r = img(:,:,1); g = img(:,:,2); b = img(:,:,3); sr = 0.393*r + 0.769*g + 0.189*b; sg = 0.349*r + 0.686*g + 0.168*b; sb = 0.272*r + 0.534*g + 0.131*b; out = min(255, cat(3, sr, sg, sb)); end `, }, { name: "Swap R/B channels", code: `function out = filterImage(img) % Reorder the 3rd dimension: RGB -> BGR out = img(:, :, [3 2 1]); end `, }, { name: "Posterize", code: `function out = filterImage(img) % Snap each channel to a few levels levels = 4; step = 255 / (levels - 1); out = round(img / step) * step; end `, }, { name: "Increase contrast", code: `function out = filterImage(img) % Push values away from mid-gray (128) k = 1.6; out = min(255, max(0, (img - 128) * k + 128)); end `, }, { name: "Sobel edges", code: `function out = filterImage(img) % Edge magnitude on the grayscale image g = 0.2989*img(:,:,1) + 0.5870*img(:,:,2) + 0.1140*img(:,:,3); kx = [-1 0 1; -2 0 2; -1 0 1]; ky = [-1 -2 -1; 0 0 0; 1 2 1]; gx = conv2(g, kx, 'same'); gy = conv2(g, ky, 'same'); out = min(255, sqrt(gx.^2 + gy.^2)); end `, }, { name: "Box blur", code: `function out = filterImage(img) % 5x5 average blur, applied to each channel k = ones(5, 5) / 25; out = zeros(size(img)); for c = 1:3 out(:,:,c) = conv2(img(:,:,c), k, 'same'); end end `, }, ]; export const DEFAULT_SCRIPT = EXAMPLES[0].code;