pf

package module
Version: v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2021 License: BSD-3-Clause Imports: 1 Imported by: 2

README

PixelFunctions

Apply functions to each pixel in an image, concurrently.

This module contains functions that fit well together with the pixelpusher module.

The PixelFunction type has this signature:

func(v uint32) uint32

If you have a pixel buffer of type []uint32, with colors on the form ARGB, then this modules allows you to apply functions of the type PixelFunction to that slice, concurrently.

The goal is to avoid looping over all pixels more than once, while applying many different effects, concurrently.

Combine and Map

  • Several PixelFunction functions can be combined to a single PixelFunction by using the Combine function.
  • A PixelFuncion can be applied to a pixel buffer by using the Map function.

Example:

package main

import (
    "fmt"
    "github.com/xyproto/pf"
    "runtime"
)

func main() {
    // Resolution
    const w, h = 320, 200

    pixels := make([]uint32, w*h)

    // Find the number of available CPUs
    n := runtime.NumCPU()

    // Combine two pixel functions
    pfs := pf.Combine(pf.InvertEverything, pf.OnlyBlue)

    // Run the combined pixel functions over all pixels using all available CPUs
    pf.Map(n, pfs, pixels)

    // Retrieve the red, green and blue components of the first pixel
    red := (pixels[0] | 0x00ff0000) >> 0xffff
    green := (pixels[0] | 0x0000ff00) >> 0xff
    blue := (pixels[0] | 0x000000ff)

    // Should output only blue: rgb(0, 0, 255)
    fmt.Printf("rgb(%d, %d, %d)\n", red, green, blue)
}

General info

  • License: BSD
  • Version: 0.1.0
  • Author: Alexander F. Rødseth <xyproto@archlinux.org>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Divide

func Divide(pixels []uint32, n int) [][]uint32

Divide a slice of pixels into several slices

func GlitchyMap

func GlitchyMap(cores int, f PixelFunction, pixels []uint32)

GlitchyMap can map a PixelFunction over every pixel (uint32 ARGB value). This function has data race issues and should not be used for anything but creating glitch effects on purpose.

func Invert

func Invert(v uint32) uint32

Invert the colors

func InvertEverything

func InvertEverything(v uint32) uint32

Invert the colors, including the alpha value

func Map

func Map(cores int, f PixelFunction, pixels []uint32)

func OnlyAlpha

func OnlyAlpha(v uint32) uint32

Keep the alpha component

func OnlyBlue

func OnlyBlue(v uint32) uint32

Keep the blue component

func OnlyGreen

func OnlyGreen(v uint32) uint32

Keep the green component

func OnlyRed

func OnlyRed(v uint32) uint32

Keep the red component

func OrAlpha

func OrAlpha(v uint32) uint32

Make the alpha component of every pixel 0xff

func RemoveAlpha

func RemoveAlpha(v uint32) uint32

Remove the alpha component, making the pixels transparent

func RemoveBlue

func RemoveBlue(v uint32) uint32

Remove the blue component

func RemoveGreen

func RemoveGreen(v uint32) uint32

Remove the green component

func RemoveRed

func RemoveRed(v uint32) uint32

Remove the red component

func SetBlueBits

func SetBlueBits(v uint32) uint32

Make the blue component of every pixel 0xff

func SetGreenBits

func SetGreenBits(v uint32) uint32

Make the green component of every pixel 0xff

func SetRedBits

func SetRedBits(v uint32) uint32

Make the red component of every pixel 0xff

Types

type PixelFunction

type PixelFunction func(v uint32) uint32

Perform an operation on a single ARGB pixel

func Combine

func Combine(a, b PixelFunction) PixelFunction

Combine two functions to a single PixelFunction. The functions are applied in the same order as the arguments.

func Combine3

func Combine3(a, b, c PixelFunction) PixelFunction

Combine three functions to a single PixelFunction. The functions are applied in the same order as the arguments.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL