pimit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2024 License: MIT Imports: 6 Imported by: 3

README

(P)arallel (Im)age (It)eration

Go Reference Go Report Card GitHub GitHub release (latest by date including pre-releases) GitHub code size in bytes

A minimalist library that adds concurrent image pixel iteration functionality wrapped in a convenient and intuitive API. The main idea is that the functions take as a parameter the image whose pixels are to be iterated over, and a function, which is a delegate, that will be executed on each pixel. The library contains a number of functions, some allow only reading, some allow reading as well as editing. Some of the functions make changes to the original image and some create a new instance. Error propagation and iteration interrupts are also possible. In general, for each row of pixels, iteration takes place in a separate goroutine, but it is also possible to choose a function that allows you to select the appropriate number of goroutines. Pimit also allows you to perform iterations on matrices, which are represented as two-dimensional generic slices [][]T.

The library includes a general API that works on universal types like image.Image and color.Color, it is more convenient but less efficient and performs more memory allocations, despite this, the operation is expected to perform 2x faster on average.

The library also includes an API for specific color spaces, it works on image pointers and primitive types e.g.: *image.RGBA and uint8, these APIs are more verbose but also much more efficient and avoid additional memory allocations. The operation is expected to perform 10x faster on average.

Installation

go get -u github.com/Krzysztofz01/pimit

Documentation

https://pkg.go.dev/github.com/Krzysztofz01/pimit

Examples

Read example: Count all the black pixels in the image.
Without pimit (no concurrecy and more code)
func CountBlackPixel(i image.Image) int {
    height := image.Bounds().Dy()
    width := image.Bounds().Dx()
    count := 0
    
    for y := 0; y < height; y += 1 {
        for x := 0; x < width; x += 1 {
            color := image.At(xIndex, yIndex)
            if color == color.Black {
                count += 1
            }
        }
    }
    
    return count
}
With pimit, using the general API
func CountBlackPixel(i image.Image) int {
    var count int32 = 0
    
    pimit.ParallelRead(i, func(x, y int, c color.Color) {
        if c == color.Black {
            atomic.AddInt32(&count, 1)
        }
    })
    
    return int(atomic.LoadInt32(count))
}
With pimit, using the specific API
func CountBlackPixel(i *image.RGBA) int {
    var count int32 = 0
    
    pimit.ParallelRgbaRead(i, func(x, y int, r, g, b, a uint8) {
        if 0 == a && a == b && b == c {
            atomic.AddInt32(&count, 1)
        }
    })
    
    return int(count)
}
Read/write example: Image to grayscale converting.
Without pimit (no concurrecy and more code)
func ToGrayscale(i draw.Image) (image.Image) {
    height := image.Bounds().Dy()
    width := image.Bounds().Dx()
 
    for y := 0; y < height; y += 1 {
        for x := 0; x < width; x += 1 {
            c := image.At(xIndex, yIndex)
            rgba, _ := c.(color.RGBA)

            y := uint8(0.299*float32(rgb.R) + 0.587*float32(rgb.G) + 0.114*float32(rgb.B))
                        
            i.Set(xIndex, yIndex, color.RGBA{
                R: y,
                G: y,
                B: y,
                A: rgba.A,
            })
        }
    }
    
    return i
}
With pimit, using the general API
func ToGrayscale(i draw.Image) image.Image  {
    pimit.ParallelReadWrite(i, func(x, y int, c color.Color) color.Color {
        rgba, _ := c.(color.RGBA)
        y := uint8(0.299*float32(rgb.R) + 0.587*float32(rgb.G) + 0.114*float32(rgb.B))
                        
        return color.RGBA{
            R: y,
            G: y,
            B: y,
            A: rgba.A,
        })
    })
    
    return i
}
With pimit, using the specific API
func ToGrayscale(i *image.RGBA) image.Image {
    pimit.ParallelRgbaReadWrite(i, func(x, y int, r, g, b, a uint8) (uint8, uint8, uint8, uint8) {
        y := uint8(0.299*float32(r) + 0.587*float32(g) + 0.114*float32(b))
        return y, y, y, a
    })
    
    return i
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewErrorTrap added in v0.2.0

func NewErrorTrap() *errorTrap

func ParallelDistributedReadWrite added in v0.2.0

func ParallelDistributedReadWrite(src draw.Image, c int, d ReadWriteDelegate)

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. The integer parameter is the number of clustes into which the image will be devided. Each cluster is then iterated in a separate goroutine.

func ParallelDistributedReadWriteE added in v0.2.0

func ParallelDistributedReadWriteE(src draw.Image, c int, d ReadWriteErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. The integer parameter is the number of clustes into which the image will be devided. Each cluster is then iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelIndices added in v0.2.0

func ParallelIndices(w, h int, d IndicesDelegate)

Perform a parallel iteration of the indexes according to the width and height provided via the parameters. Execute the delegate for each indexes combination. Each row is iterated in a separate goroutine.

func ParallelMatrixReadWrite added in v0.1.0

func ParallelMatrixReadWrite[T any](m [][]T, d func(x, y int, value T) T)

Perform a parallel iteration of the values of the provided matrix represented as a two-dimentional generic slice. For each entry, execute the delegate function allowing you to read the values and coordinates, the delegate return value will be set at the given coordinates. This changes will be applied to the passed two-dimentional slice instance. Each column is iterated in a separate goroutine.

func ParallelMatrixReadWriteE added in v0.1.0

func ParallelMatrixReadWriteE[T any](m [][]T, d func(x, y int, value T) (T, error)) error

Perform a parallel iteration of the values of the provided matrix represented as a two-dimentional generic slice. For each entry, execute the delegate function allowing you to read the values and coordinates, the delegate return value will be set at the given coordinates. This changes will be applied to the passed two-dimentional slice instance. Each column is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelNrgbaRead added in v0.2.0

func ParallelNrgbaRead(src *image.NRGBA, d NrgbaReadDelegate)

Perform a parallel iteration of the pixels of the provided NRGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates. Each row is iterated in a separate goroutine.

func ParallelNrgbaReadE added in v0.2.0

func ParallelNrgbaReadE(src *image.NRGBA, d NrgbaReadErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided NRGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelNrgbaReadWrite added in v0.2.0

func ParallelNrgbaReadWrite(src *image.NRGBA, d NrgbaReadWriteDelegate)

Perform a parallel iteration of the pixels of the provided NRGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. Consider using ParallelReadWriteNew if you want to avoid changes to the original image at the expense of additional allocations. Each row is iterated in a separate goroutine.

func ParallelNrgbaReadWriteE added in v0.2.0

func ParallelNrgbaReadWriteE(src *image.NRGBA, d NrgbaReadWriteErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided NRGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. Consider using ParallelReadWriteNewE if you want to avoid changes to the original image at the expense of additional allocations. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelNrgbaReadWriteNew added in v0.2.0

func ParallelNrgbaReadWriteNew(src *image.NRGBA, d NrgbaReadWriteDelegate) *image.NRGBA

Perform a parallel iteration of the pixels of the provided NRGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to a new image instance which internaly uses the NRGBA color space and is returned by the function. Each row is iterated in a separate goroutine.

func ParallelNrgbaReadWriteNewE added in v0.2.0

func ParallelNrgbaReadWriteNewE(src *image.NRGBA, d NrgbaReadWriteErrorableDelegate) (*image.NRGBA, error)

Perform a parallel iteration of the pixels of the provided NRGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to a new image instance which internaly uses the NRGBA color space and is returned by the function. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelRead added in v0.2.0

func ParallelRead(src image.Image, d ReadDelegate)

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates. Each row is iterated in a separate goroutine.

func ParallelReadE added in v0.2.0

func ParallelReadE(src image.Image, d ReadErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelReadWrite added in v0.2.0

func ParallelReadWrite(src draw.Image, d ReadWriteDelegate)

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. Consider using ParallelReadWriteNew if you want to avoid changes to the original image at the expense of additional allocations. Each row is iterated in a separate goroutine.

func ParallelReadWriteE added in v0.2.0

func ParallelReadWriteE(src draw.Image, d ReadWriteErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. Consider using ParallelReadWriteNewE if you want to avoid changes to the original image at the expense of additional allocations. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelReadWriteNew added in v0.2.0

func ParallelReadWriteNew(src image.Image, d ReadWriteDelegate) draw.Image

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to a new image instance which internaly uses the NRGBA color space and is returned by the function. Each row is iterated in a separate goroutine.

func ParallelReadWriteNewE added in v0.2.0

func ParallelReadWriteNewE(src image.Image, d ReadWriteErrorableDelegate) (draw.Image, error)

Perform a parallel iteration of the pixels of the provided image. For each pixel, execute the delegate function allowing you to read the color and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to a new image instance which internaly uses the NRGBA color space and is returned by the function. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelRgbaRead added in v0.2.0

func ParallelRgbaRead(src *image.RGBA, d RgbaReadDelegate)

Perform a parallel iteration of the pixels of the provided RGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates. Each row is iterated in a separate goroutine.

func ParallelRgbaReadE added in v0.2.0

func ParallelRgbaReadE(src *image.RGBA, d RgbaReadErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided RGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelRgbaReadWrite added in v0.2.0

func ParallelRgbaReadWrite(src *image.RGBA, d RgbaReadWriteDelegate)

Perform a parallel iteration of the pixels of the provided RGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. Consider using ParallelReadWriteNew if you want to avoid changes to the original image at the expense of additional allocations. Each row is iterated in a separate goroutine.

func ParallelRgbaReadWriteE added in v0.2.0

func ParallelRgbaReadWriteE(src *image.RGBA, d RgbaReadWriteErrorableDelegate) error

Perform a parallel iteration of the pixels of the provided RGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to the passed image instance. Consider using ParallelReadWriteNewE if you want to avoid changes to the original image at the expense of additional allocations. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

func ParallelRgbaReadWriteNew added in v0.2.0

func ParallelRgbaReadWriteNew(src *image.RGBA, d RgbaReadWriteDelegate) *image.RGBA

Perform a parallel iteration of the pixels of the provided RGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to a new image instance which internaly uses the NRGBA color space and is returned by the function. Each row is iterated in a separate goroutine.

func ParallelRgbaReadWriteNewE added in v0.2.0

func ParallelRgbaReadWriteNewE(src *image.RGBA, d RgbaReadWriteErrorableDelegate) (*image.RGBA, error)

Perform a parallel iteration of the pixels of the provided RGBA image. For each pixel, execute the delegate function allowing you to read the color (R, G, B and A as uint8) and coordinates, the delegate return color will be set at the given coordinates. This changes will be applied to a new image instance which internaly uses the NRGBA color space and is returned by the function. Each row is iterated in a separate goroutine. The iteration will break after the first error occurs and the error will be returned.

Types

type IndicesDelegate added in v0.2.0

type IndicesDelegate = func(x, y int)

type NrgbaReadDelegate added in v0.2.0

type NrgbaReadDelegate = func(x, y int, r, g, b, a uint8)

type NrgbaReadErrorableDelegate added in v0.2.0

type NrgbaReadErrorableDelegate = func(x, y int, r, g, b, a uint8) error

type NrgbaReadWriteDelegate added in v0.2.0

type NrgbaReadWriteDelegate = func(x, y int, r, g, b, a uint8) (uint8, uint8, uint8, uint8)

type NrgbaReadWriteErrorableDelegate added in v0.2.0

type NrgbaReadWriteErrorableDelegate = func(x, y int, r, g, b, a uint8) (uint8, uint8, uint8, uint8, error)

type ReadDelegate added in v0.2.0

type ReadDelegate = func(x, y int, c color.Color)

type ReadErrorableDelegate added in v0.2.0

type ReadErrorableDelegate = func(x, y int, c color.Color) error

type ReadWriteDelegate added in v0.2.0

type ReadWriteDelegate = func(x, y int, c color.Color) color.Color

type ReadWriteErrorableDelegate added in v0.2.0

type ReadWriteErrorableDelegate = func(x, y int, c color.Color) (color.Color, error)

type RgbaReadDelegate added in v0.2.0

type RgbaReadDelegate = func(x, y int, r, g, b, a uint8)

type RgbaReadErrorableDelegate added in v0.2.0

type RgbaReadErrorableDelegate = func(x, y int, r, g, b, a uint8) error

type RgbaReadWriteDelegate added in v0.2.0

type RgbaReadWriteDelegate = func(x, y int, r, g, b, a uint8) (uint8, uint8, uint8, uint8)

type RgbaReadWriteErrorableDelegate added in v0.2.0

type RgbaReadWriteErrorableDelegate = func(x, y int, r, g, b, a uint8) (uint8, uint8, uint8, uint8, error)

Jump to

Keyboard shortcuts

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