Rook
EnglishENНачать
// Фильтры картинки в одном потоке и на всех процессорах: go for делит пиксели между
// воркерами, и каждый применяет к своей части ту же функцию

const WIDTH = 1600
const HEIGHT = 900

// Фильтр — это квадрат весов: канал пикселя становится суммой того же канала пикселей
// вокруг него, умноженных каждый на свой вес
interface Filter {
    name string
    size number
    weights number[]
}

func box(size number) number[] {
    return Array.from({ length: size * size }, () => 1 / (size * size))
}

func diagonal(size number) number[] {
    const weights = box(size).map(_ => 0)
    for let i = 0; i < size; i++ {
        weights[i * (size + 1)] = 1 / size
    }
    return weights
}

const FILTERS Filter[] = [
    { name: "Размытие", size: 13, weights: box(13) },
    { name: "Движение", size: 15, weights: diagonal(15) },
    { name: "Резкость", size: 3, weights: [0, -1, 0, -1, 5, -1, 0, -1, 0] },
    { name: "Контуры", size: 3, weights: [-1, -1, -1, -1, 8, -1, -1, -1, -1] },
    { name: "Тиснение", size: 3, weights: [-2, -1, 0, -1, 1, 1, 0, 1, 2] },
]

// Картинка для работы, нарисованная на canvas: небо, круги и большие буквы
func picture() Uint8ClampedArray {
    const canvas = document.createElement("canvas")
    canvas.width = WIDTH
    canvas.height = HEIGHT
    const context = canvas.getContext("2d")
    if context == null {
        return new Uint8ClampedArray(WIDTH * HEIGHT * 4)
    }
    const sky = context.createLinearGradient(0, 0, WIDTH, HEIGHT)
    sky.addColorStop(0, "#1d2b64")
    sky.addColorStop(1, "#f8a35b")
    context.fillStyle = sky
    context.fillRect(0, 0, WIDTH, HEIGHT)
    for let i = 0; i < 40; i++ {
        context.fillStyle = `hsl(${i * 37} 80% 60% / 0.7)`
        context.beginPath()
        context.arc((i * 211) % WIDTH, (i * 137) % HEIGHT, 30 + (i % 5) * 25, 0, Math.PI * 2)
        context.fill()
    }
    context.fillStyle = "#fff"
    context.font = "bold 300px sans-serif"
    context.fillText("Rook", 380, 560)
    return context.getImageData(0, 0, WIDTH, HEIGHT).data
}

// Канал одного пикселя после фильтра; за краями картинки берётся ближайший пиксель
func filteredAt(src Uint8ClampedArray, i number, filter Filter) number {
    const channel = i % 4
    if channel == 3 {
        return 255
    }
    const x = Math.floor(i / 4) % WIDTH
    const y = Math.floor(i / 4 / WIDTH)
    const half = (filter.size - 1) / 2
    let sum = 0
    for let dy = 0; dy < filter.size; dy++ {
        const yy = Math.min(Math.max(y + dy - half, 0), HEIGHT - 1)
        for let dx = 0; dx < filter.size; dx++ {
            const xx = Math.min(Math.max(x + dx - half, 0), WIDTH - 1)
            sum += src[(yy * WIDTH + xx) * 4 + channel] * filter.weights[dy * filter.size + dx]
        }
    }
    return sum
}

func applyOnOne(src Uint8ClampedArray, filter Filter) Uint8ClampedArray {
    const out = new Uint8ClampedArray(src.length)
    for i, _ in out {
        out[i] = filteredAt(src, i, filter)
    }
    return out
}

// Тот же цикл с go: каждый воркер получает копии src и filter и пишет свою часть out
async func applyOnAll(src Uint8ClampedArray, filter Filter) Uint8ClampedArray {
    const out = new Uint8ClampedArray(src.length)
    go for i, _ in out {
        out[i] = filteredAt(src, i, filter)
    }
    return out
}

comp Filters() {
    const source = picture()
    state filter = FILTERS[0]
    state report = "Выберите фильтр и где его применить"
    state busy = false
    // Крутится, пока страница свободна: в одном потоке замирает, пока фильтр не закончит
    state turn = 0
    ref canvas

    func draw(pixels Uint8ClampedArray) {
        const image = new ImageData(WIDTH, HEIGHT)
        image.data.set(pixels)
        canvas.getContext("2d")?.putImageData(image, 0, 0)
    }

    async func apply(all bool) {
        busy = true
        report = "Работаю…"
        <-after(50)                         // страница успевает это показать, пока поток не занят
        const start = performance.now()
        const out = all ? await applyOnAll(source, filter) : applyOnOne(source, filter)
        const ms = Math.round(performance.now() - start)
        draw(out)
        const where = all ? `на процессорах: ${navigator.hardwareConcurrency}` : "в одном потоке"
        report = `${filter.name}: ${ms} мс ${where}`
        busy = false
    }

    mount {
        draw(source)
        const timer = setInterval(() => {
            turn = (turn + 1) % 8
        }, 100)
        defer clearInterval(timer)
    }

    return <div>
        <canvas ref={canvas} width={WIDTH} height={HEIGHT} style="width: 100%" />
        <p>{for each in FILTERS {
            <label>
                <input type="radio" name="filter" checked={each == filter} onChange={filter = each} />
                {each.name}
            </label>
        }}</p>
        <p>
            <button disabled={busy} onClick={apply(false)}>Один поток</button>
            <button disabled={busy} onClick={apply(true)}>Все процессоры</button>
            <button disabled={busy} onClick={draw(source)}>Оригинал</button>
        </p>
        <p>{"◐◓◑◒◐◓◑◒"[turn]} {report}</p>
    </div>
}

document.body.append(<Filters />)

◑ Выберите фильтр и где его применить

Программа ничего не вывела в консоль.