imagex

package
v0.3.15 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: BSD-3-Clause Imports: 25 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UpdateTestImages = updateTestImages

UpdateTestImages indicates whether to update currently saved test images in [AssertImage] instead of comparing against them. It is automatically set if the build tag "update" is specified, or if the environment variable "CORE_UPDATE_TESTDATA" is set to "true". It should typically only be set through those methods. It should only be set when behavior has been updated that causes test images to change, and it should only be set once and then turned back off.

Functions

func AsRGBA added in v0.3.11

func AsRGBA(src image.Image) *image.RGBA

AsRGBA returns the image as an *image.RGBA. If it already is one, it returns that image directly. Otherwise it returns a clone. It calls Unwrap first. See also CloneAsRGBA.

func Assert

func Assert(t TestingT, img image.Image, filename string, tols ...int)

Assert asserts that the given image is equivalent to the image stored at the given filename in the testdata directory, with ".png" added to the filename if there is no extension (eg: "button" becomes "testdata/button.png"). Forward slashes are automatically replaced with backslashes on Windows. If it is not, it fails the test with an error, but continues its execution. If there is no image at the given filename in the testdata directory, it creates the image. optional tolerance argument specifies the maximum color difference, which defaults to 10.

func Base64SplitLines

func Base64SplitLines(b []byte) []byte

Base64SplitLines splits the encoded Base64 bytes into standard lines of 76 chars each. The last line also ends in a newline

func CloneAsRGBA

func CloneAsRGBA(src image.Image) *image.RGBA

CloneAsRGBA returns an *image.RGBA copy of the supplied image. It calls Unwrap first. See also AsRGBA.

func ColorsEqual added in v0.3.13

func ColorsEqual(cc, ic color.RGBA, tol int) bool

ColorsEqual returns true if two colors equal within tol

func Crop added in v0.3.12

func Crop(src image.Image, rect image.Rectangle) image.Image

Crop returns a cropped region of the source image (which can be Wrapped), returning a WrapJS image handle on web and using web-native optimized code.

func DiffImage added in v0.3.8

func DiffImage(a, b image.Image) image.Image

DiffImage returns the difference between two images, with pixels having the abs of the difference between pixels. Images must have the same bounds.

func ImagesEqual added in v0.3.13

func ImagesEqual(a, b image.Image, tol int) (bounds, content bool, ac, bc color.RGBA, x, y int)

ImagesEqual compares two images, returns false if not equal, based on bounds or content. If a content difference, then the colors and pixel where they first differ are returned. The first image is considered the reference, correct one. Tol is the color tolerance.

func Resize added in v0.3.12

func Resize(src image.Image, size image.Point) image.Image

Resize returns a resized version of the source image (which can be Wrapped), returning a WrapJS image handle on web and using web-native optimized code. Otherwise, uses medium quality Linear resize.

func Save

func Save(im image.Image, filename string) error

Save saves the image to the given filename, with the format inferred from the filename. png, jpeg, gif, tiff, and bmp are supported.

func ToBase64 added in v0.3.13

func ToBase64(img image.Image, f Formats) ([]byte, string)

ToBase64 returns bytes of image encoded in given format, in Base64 encoding with "image/format" mimetype returned

func Uint8Equal added in v0.3.13

func Uint8Equal(cc, ic uint8, tol int) bool

Uint8Equal returns true if two numbers are within tol

func Unwrap added in v0.3.11

func Unwrap(src image.Image) image.Image

Unwrap calls [Wrapped.Underlying] on a Wrapped if it is one. It returns the original image otherwise.

func Update added in v0.3.11

func Update(src image.Image)

Update calls [Wrapped.Update] on a Wrapped if it is one. It does nothing otherwise.

func WrapJS added in v0.3.11

func WrapJS(src image.Image) image.Image

WrapJS returns a JavaScript optimized wrapper around the given image.Image on web, and just returns the image on other platforms.

func Write

func Write(im image.Image, w io.Writer, f Formats) error

Write writes the image to the given writer using the given foramt. png, jpeg, gif, tiff, and bmp are supported. It [Unwrap]s any Wrapped images.

Types

type Formats

type Formats int32 //enums:enum

Formats are the supported image encoding / decoding formats

const (
	None Formats = iota
	PNG
	JPEG
	GIF
	TIFF
	BMP
	WebP
)

The supported image encoding formats

const FormatsN Formats = 7

FormatsN is the highest valid value for type Formats, plus one.

func ExtToFormat

func ExtToFormat(ext string) (Formats, error)

ExtToFormat returns a Format based on a filename extension, which can start with a . or not

func FormatsValues

func FormatsValues() []Formats

FormatsValues returns all possible values for the type Formats.

func FromBase64

func FromBase64(eb []byte) (image.Image, Formats, error)

FromBase64 returns image from Base64-encoded bytes

func Open

func Open(filename string) (image.Image, Formats, error)

Open opens an image from the given filename. The format is inferred automatically, and is returned using the Formats enum. png, jpeg, gif, tiff, bmp, and webp are supported.

func OpenFS

func OpenFS(fsys fs.FS, filename string) (image.Image, Formats, error)

OpenFS opens an image from the given filename using the given fs.FS filesystem (e.g., for embed files). The format is inferred automatically, and is returned using the Formats enum. png, jpeg, gif, tiff, bmp, and webp are supported.

func Read

func Read(r io.Reader) (image.Image, Formats, error)

Read reads an image to the given reader, The format is inferred automatically, and is returned using the Formats enum. png, jpeg, gif, tiff, bmp, and webp are supported.

func (Formats) Desc

func (i Formats) Desc() string

Desc returns the description of the Formats value.

func (Formats) Int64

func (i Formats) Int64() int64

Int64 returns the Formats value as an int64.

func (Formats) MarshalText

func (i Formats) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Formats) SetInt64

func (i *Formats) SetInt64(in int64)

SetInt64 sets the Formats value from an int64.

func (*Formats) SetString

func (i *Formats) SetString(s string) error

SetString sets the Formats value from its string representation, and returns an error if the string is invalid.

func (Formats) String

func (i Formats) String() string

String returns the string representation of this Formats value.

func (*Formats) UnmarshalText

func (i *Formats) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Formats) Values

func (i Formats) Values() []enums.Enum

Values returns all possible values for the type Formats.

type JSON added in v0.3.13

type JSON struct {
	Image image.Image
}

JSON is a wrapper around an image.Image that defines JSON Marshal and Unmarshal methods, so that the image will automatically be properly saved / loaded when used as a struct field, for example. Must be a pointer type to support custom unmarshal function. The original image is not anonymously embedded so that you have to extract it, otherwise it will be processed inefficiently.

func NewJSON added in v0.3.13

func NewJSON(im image.Image) *JSON

NewJSON returns a new JSON wrapper around given image, to support automatic wrapping and unwrapping.

func (*JSON) MarshalJSON added in v0.3.13

func (js *JSON) MarshalJSON() ([]byte, error)

func (*JSON) UnmarshalJSON added in v0.3.13

func (js *JSON) UnmarshalJSON(b []byte) error

type JSONEncoded added in v0.3.13

type JSONEncoded struct {
	Width  int
	Height int

	// Image is the encoded byte stream, which will be encoded in JSON
	// using Base64
	Image []byte
}

JSONEncoded is a representation of an image encoded into a byte stream, using the PNG encoder. This can be Marshal and Unmarshal'd directly.

type JSRGBA added in v0.3.13

type JSRGBA struct {
	*image.RGBA
}

JSRGBA is a dummy version of this type so code does not have to be conditionalized.

type TestingT

type TestingT interface {
	Errorf(format string, args ...any)
}

TestingT is an interface wrapper around *testing.T

type Wrapped added in v0.3.11

type Wrapped interface {
	image.Image

	// Update is called whenever the image data has been updated,
	// to update any additional data based on the new image.
	// This may copy an image to the GPU or update JavaScript pointers.
	Update()

	// Underlying returns the underlying image.Image, which should
	// be called whenever passing the image to some other Go-based
	// function that is likely to be optimized for different image types,
	// such as [draw.Draw]. Do NOT use this for functions that will
	// directly handle the wrapped image!
	Underlying() image.Image
}

Wrapped extends the image.Image interface with two methods that manage the wrapping of an underlying Go image.Image. This can be used for images that are actually GPU textures, and to manage JavaScript pointers on the js platform.

Jump to

Keyboard shortcuts

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