utils

package
v0.0.0-...-6cd0f2a Latest Latest
Warning

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

Go to latest
Published: May 1, 2015 License: MIT Imports: 18 Imported by: 20

Documentation

Overview

Package utils provides useful helper functions for working with Images and Colors.

Index

Constants

View Source
const (
	PNG  output = "png"
	JPEG        = "jpeg"
	TIFF        = "tiff"
)

Formats that can be created by img.

Variables

View Source
var Output = PNG

Output is the currently selected output type, and shouldn't be modified by anything other than GetOutput.

Functions

func Average

func Average(cs ...color.Color) color.Color

Average takes a list of colours and returns the average. Given an empty list it returns Black.

func ChopRectangle

func ChopRectangle(rect image.Rectangle, rows, cols int, mode ExcessMode) []image.Rectangle

ChopRectangle splits a Rectangle into the number of rows and columns given.

func ChopRectangleToSizes

func ChopRectangleToSizes(rect image.Rectangle, rowHeight, colWidth int, mode ExcessMode) []image.Rectangle

ChopRectangleToSizes splits a Rectangle into smaller Rectangles with the size given.

func Closeness

func Closeness(one, two color.Color) uint32

Closeness calculates the "closeness" of two colours by finding the sum of differences in each colour channel.

func EachColor

func EachColor(img image.Image, f func(c color.Color))

EachColor iterates through each pixel of the Image, applying the function to each colour.

func EachColorInRectangle

func EachColorInRectangle(img image.Image, b image.Rectangle, f func(c color.Color))

EachColorInRectangle is a helper function for working on a part of an image.

func FlagVisited

func FlagVisited(name string, flags flag.FlagSet) (didFind bool)

FlagVisited determines whether the named flag has been visited in the FlagSet given. This is helpful if you want to have a flag that triggers an action when given, but is not a boolean flag.

func GetOutput

func GetOutput(args []string) []string

GetOutput takes the os.Args array as input, uses it to set the output format, then returns the new os.Args array.

os.Args = utils.GetOutput(os.Args)
flag.Parse()

func Map

func Map(f func() Composable) func(image.Image) image.Image

Map takes a Composable function, and returns a function that applies it to every pixel of an Image.

func MapAdjuster

func MapAdjuster(f func(Adjuster) Composable) func(image.Image, Adjuster) image.Image

MapAdjuster takes a Composable function and an Adjuster, and returns a function that applies the function to every pixel of an Image.

func MapColor

func MapColor(img image.Image, f Composable) image.Image

MapColor iterates through each pixel of the Image and applies the given function, drawing the returned colour to a new Image which is then returned.

func MapColorInRectangle

func MapColorInRectangle(img image.Image, bounds image.Rectangle, dest draw.Image,
	f Composable)

MapColorInRectangle is a helper function for working on part of an image. It takes the original image, a function to use, a image to write to, and the bounds of the original (and therefore the final image) to act upon.

func Max

func Max(ns ...uint32) (n uint32)

Max returns the largest value in the list of uint32s given.

func Maxf

func Maxf(ns ...float64) (n float64)

Maxf returns the largest value in the list of float64s given.

func Min

func Min(ns ...uint32) (n uint32)

Min returns the smallest value in the list of uint32s given.

func Minf

func Minf(ns ...float64) (n float64)

Minf returns the smallest value in the list of float64s given.

func NormalisedRGBA

func NormalisedRGBA(c color.Color) (r, g, b, a uint32)

NormalisedRGBA returns the RGBA colour channel values of a Color in a normalised form. The values are in non-premultiplied form and are in the range 0-255.

This is modified from image/color.nrgbaModel

func NormalisedRGBAf

func NormalisedRGBAf(c color.Color) (float64, float64, float64, float64)

NormalisedRGBAf returns the RGBA colour channel values as floating point numbers with values from 0 to 255.

func PEachColor

func PEachColor(img image.Image, f func(c color.Color))

PEachColor is like EachColor, but runs in parallel. This means that order can not be guaranteed.

func RatioRGBA

func RatioRGBA(c color.Color) (float64, float64, float64, float64)

RatioRGBA returns the RGBA colour channel values as floating point numbers with values from 0 to 1.

func ReadStdin

func ReadStdin() (image.Image, *exif.Exif)

ReadStdin reads an image file (either PNG, JPEG or GIF) from standard input.

func Truncate

func Truncate(n uint32) uint32

Truncate takes a colour channel value and forces it into the range 0 to 255 by setting any value below 0 to 0 and and any above 255 to 255.

func Truncatef

func Truncatef(n float64) float64

Truncatef is identical to Truncate but takes and returns a float64.

func Warn

func Warn(s ...interface{})

Warn prints a message to standard error

func WriteStdout

func WriteStdout(img image.Image, data *exif.Exif)

WriteStdout writes an Image to standard output as a PNG file.

Types

type Adjuster

type Adjuster func(float64) float64

An Adjuster is a function which takes a floating point value, most likely between 0 and 1, and returns another floating point value, again between 0 and 1 (though this is not a requirement).

func Adder

func Adder(with float64) Adjuster

Adder returns an Adjuster which adds the value with to the returned Adjusters argument.

func Multiplier

func Multiplier(ratio float64) Adjuster

Multiplier returns an Adjuster which multiplies a given value with ratio.

type Composable

type Composable func(color.Color) color.Color

A Composable function is one which can be composed with another function of the same type. It simply transforms a Color into another Color.

func Compose

func Compose(fs ...Composable) Composable

Compose takes a variable list of Composable functions and returns a single Composable function which performs them sequentially. For example,

var f Composable = Compose(
  greyscale.BlueC(),
  brightness.AdjustC(utils.Adder(0.05)),
)

// Only loops through image once!
img = MapColors(img, f)

type Dimension

type Dimension struct {
	H, W int
}

Dimension represents a rectangle with Height and Width.

func SizeForCols

func SizeForCols(img image.Image, cols int) Dimension

SizeForCols determines the maximum (square) Dimensions to use that will fit the given number of columns into the image.

func SizeForRows

func SizeForRows(img image.Image, rows int) Dimension

SizeForRows determines the maximum (square) Dimensions to use that will fit the given number of rows into the image.

func SizeForRowsAndCols

func SizeForRowsAndCols(img image.Image, rows, cols int) Dimension

SizeForRowsAndCols determines the maximum (rectangular) Dimensions to use that will fit the given number of rows and columns into the image.

func (*Dimension) Set

func (d *Dimension) Set(value string) error

Set takes a string representing a Dimension (ie., in the format HxW, where H and W are both integers) and sets the Dimension called on to the correct value. It returns an error string if a problem occurs.

func (*Dimension) String

func (d *Dimension) String() string

type Direction

type Direction int

Direction represents one of 9 directions. these being the 4 vertices, the 4 sides, and the Centre of a square.

const (
	Centre Direction = iota
	Top
	TopRight
	Right
	BottomRight
	Bottom
	BottomLeft
	Left
	TopLeft
)

type ExcessMode

type ExcessMode int

ExcessMode specifies how excess space is dealt with for tools that may produce results with different dimensions to the input image.

const (
	// Ignore any "left over" space. So the resultant Rectangles may be smaller
	// than the original given.
	IGNORE ExcessMode = iota

	// Add the excess to the right- and bottom-most Rectangles. So the resultant
	// Rectangles will be the same size, but some edge Rectangles may be larger.
	ADD

	// Separate the excess into new Rectangles. So the resultant Rectangles will
	// be the same size, but some edge Rectangles may be smaller.
	SEPARATE
)

Jump to

Keyboard shortcuts

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