go-resample: github.com/Zwobot/go-resample/resample Index | Files

package resample

import "github.com/Zwobot/go-resample/resample"

Resample provides generic image resampling (resizing) functions.

Note that the package was only tested via visual inspection of images from http://testimages.tecnick.com . No automated testing has been implemented.

For now the resampling creates image.NRGBA64 images. No other target formats have been implemented. All image formats are supported, there's only a fast path for NRGBA64 images though.

Internally all calculations are done intermediary float32 RGBA values.

The simplest way to use this package is just to resize an image. You'll just need to supply the source image and a new size.

This will use the Catmull-Rom scaling filter and handle the image boundaries by weighing the pixels close with a higher weight accordingly.

Example:

// Double the size
newSize := sourceImage.Bounds().Max.Mul(2)
newImage, err := resample.Resize(newSize, sourceImage)

An error can - theoretically - only occure when you supply nonsensical input such as negative image sizes or a nil source image.

For more general usage - such as specifying the filter and boundary handling see the ResizeToChannel and ResizeToChannelWithFilter functions.

Performance

Fast.

For a (W,H) -> (NW,NH) upsampling with a Lancsoz3 filter it will do roughly 24*min(NW*H+NW*NH, NW*NH + W*NH) floating point 32bit multiplications. That's where the time is spent. No other optimizations have been done.

Index

Package Files

resample.go

Variables

var (
    Lanczos12 = Filter{Apply: lanczos(12), Support: 12}
    Lanczos5  = Filter{Apply: lanczos(5), Support: 5}
    Lanczos3  = Filter{Apply: lanczos(3), Support: 3}
    // Also called linear
    Box = Filter{Apply: box, Support: 0.5}
    // Also called bilinear
    Triangle = Filter{Apply: triangle, Support: 1}
    // Used by FreeImage, Image as bicubic
    Mitchell = Filter{Apply: cubic(1.0/1.3, 1.0/1.3), Support: 2}
    // Used by GIMP as bicubic
    CatmullRom = Filter{Apply: cubic(0, 1.0/2.0), Support: 2}
    // Used by ImageMagick, Paint.Net as (bi-)cubic
    BSpline = Filter{Apply: cubic(1.0, 0.0), Support: 2}
)
var (
    ErrMissingFilter        = errors.New("Filter is invalid.")
    ErrMissingWrapFunc      = errors.New("Wrap function is invalid.")
    ErrSourceImageIsInvalid = errors.New("Source image is invalid.")
    ErrTargetImageIsInvalid = errors.New("Target image is invalid.")
    ErrTargetSizeIsInvalid  = errors.New("Target size is invalid.")
    ErrLogicError           = errors.New("Programming error.")
)

func Clamp Uses

func Clamp(x, min, max int) int

Clamp the filter at the image boundaries.

This results in sampling the boundary pixel repeatedly.

func Reflect Uses

func Reflect(x, min, max int) int

This will cause the filter to see the picture at the boundaries as if it where mirrored.

func Reject Uses

func Reject(x, min, max int) int

Reject samples from outside the image

func Resize Uses

func Resize(dst image.Image, dstRect image.Rectangle,
    src image.Image, srcRect image.Rectangle) (image.Image, error)

Create a new image.NRGBA64 with the size newSize and resampled from src via the Catmull-Rom cubic filter. Boundaries are rejected. Returns an error if the src is nil, or if the newSize is negative in either dimension.

func ResizeToChannel Uses

func ResizeToChannel(dst image.Image, dstRect image.Rectangle,
    src image.Image, srcRect image.Rectangle) (<-chan Step, chan<- bool, error)

Returns a blocking channel of Step.

Once Step.Done() is true, the calculation has finished and the channel is closed. You can use this to abort calculating larger image resamples or to show percentage done indicators.

func ResizeToChannelWithFilter Uses

func ResizeToChannelWithFilter(dst image.Image, dstRect image.Rectangle,
    src image.Image, srcRect image.Rectangle,
    F Filter, XWrap, YWrap WrapFunc) (<-chan Step, chan<- bool, error)

Returns a blocking channel of Step.

Once Step.Done() is true, the calculation has finished and the channel is closed. You can use this to abort calculating larger image resamples or to show percentage done indicators.

The filter F is the resampling function used. See the provided samplers for examples. Additionally X- and YWrap functions are used to define how image boundaries are treated. See the provided Clamp function for examples.

type Filter Uses

type Filter struct {
    // Actual filter function.
    // Integral [-Support,Support] over is assumed to be 1.0
    Apply func(float64) float64
    // Range outside [-Support,Support] is assumed to be zero.
    Support float64
}

A resampling filter and support. The values are pre-calculated inside the ResizeXY functions.

type Step Uses

type Step interface {
    // Returns true on the last step.
    Done() bool

    // The resampled image. Only guaranteed non-nil when done.
    Image() image.Image

    // Percentage done.
    Percent() int
}

A step of the resampling process.

type WrapFunc Uses

type WrapFunc func(x, min, max int) int

Package resample imports 4 packages (graph) and is imported by 2 packages. Updated 2016-07-16. Refresh now. Tools for package owners.