Documentation ¶
Overview ¶
Package image implements a basic 2-D image library.
The fundamental interface is called Image. An Image contains colors, which are described in the image/color package.
Values of the Image interface are created either by calling functions such as NewRGBA and NewPaletted, or by calling Decode on an io.Reader containing image data in a format such as GIF, JPEG or PNG. Decoding any particular image format requires the prior registration of a decoder function. Registration is typically automatic as a side effect of initializing that format's package so that, to decode a PNG image, it suffices to have
import _ "image/png"
in a program's main package. The _ means to import a package purely for its initialization side effects.
See "The Go image package" for more details: http://golang.org/doc/articles/image_package.html
Example ¶
package main import ( "fmt" "image" "log" "os" // Package image/jpeg is not used explicitly in the code below, // but is imported for its initialization side-effect, which allows // image.Decode to understand JPEG formatted images. Uncomment these // two lines to also understand GIF and PNG images: // _ "image/gif" // _ "image/png" _ "image/jpeg" ) func main() { // Open the file. file, err := os.Open("testdata/video-001.jpeg") if err != nil { log.Fatal(err) } defer file.Close() // Decode the image. m, _, err := image.Decode(file) if err != nil { log.Fatal(err) } bounds := m.Bounds() // Calculate a 16-bin histogram for m's red, green, blue and alpha components. // // An image's bounds do not necessarily start at (0, 0), so the two loops start // at bounds.Min.Y and bounds.Min.X. Looping over Y first and X second is more // likely to result in better memory access patterns than X first and Y second. var histogram [16][4]int for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { r, g, b, a := m.At(x, y).RGBA() // A color's RGBA method returns values in the range [0, 65535]. // Shifting by 12 reduces this to the range [0, 15]. histogram[r>>12][0]++ histogram[g>>12][1]++ histogram[b>>12][2]++ histogram[a>>12][3]++ } } // Print the results. fmt.Printf("%-14s %6s %6s %6s %6s\n", "bin", "red", "green", "blue", "alpha") for i, x := range histogram { fmt.Printf("0x%04x-0x%04x: %6d %6d %6d %6d\n", i<<12, (i+1)<<12-1, x[0], x[1], x[2], x[3]) } }
Output: bin red green blue alpha 0x0000-0x0fff: 471 819 7596 0 0x1000-0x1fff: 576 2892 726 0 0x2000-0x2fff: 1038 2330 943 0 0x3000-0x3fff: 883 2321 1014 0 0x4000-0x4fff: 501 1295 525 0 0x5000-0x5fff: 302 962 242 0 0x6000-0x6fff: 219 358 150 0 0x7000-0x7fff: 352 281 192 0 0x8000-0x8fff: 3688 216 246 0 0x9000-0x9fff: 2277 237 283 0 0xa000-0xafff: 971 254 357 0 0xb000-0xbfff: 317 306 429 0 0xc000-0xcfff: 203 402 401 0 0xd000-0xdfff: 256 394 241 0 0xe000-0xefff: 378 343 173 0 0xf000-0xffff: 3018 2040 1932 15450
Index ¶
- Variables
- func RegisterFormat(name, magic string, decode func(io.Reader) (Image, error), ...)
- type Alpha
- func (p *Alpha) At(x, y int) color.Color
- func (p *Alpha) Bounds() Rectangle
- func (p *Alpha) ColorModel() color.Model
- func (p *Alpha) Opaque() bool
- func (p *Alpha) PixOffset(x, y int) int
- func (p *Alpha) Set(x, y int, c color.Color)
- func (p *Alpha) SetAlpha(x, y int, c color.Alpha)
- func (p *Alpha) SubImage(r Rectangle) Image
- type Alpha16
- func (p *Alpha16) At(x, y int) color.Color
- func (p *Alpha16) Bounds() Rectangle
- func (p *Alpha16) ColorModel() color.Model
- func (p *Alpha16) Opaque() bool
- func (p *Alpha16) PixOffset(x, y int) int
- func (p *Alpha16) Set(x, y int, c color.Color)
- func (p *Alpha16) SetAlpha16(x, y int, c color.Alpha16)
- func (p *Alpha16) SubImage(r Rectangle) Image
- type Config
- type Gray
- func (p *Gray) At(x, y int) color.Color
- func (p *Gray) Bounds() Rectangle
- func (p *Gray) ColorModel() color.Model
- func (p *Gray) Opaque() bool
- func (p *Gray) PixOffset(x, y int) int
- func (p *Gray) Set(x, y int, c color.Color)
- func (p *Gray) SetGray(x, y int, c color.Gray)
- func (p *Gray) SubImage(r Rectangle) Image
- type Gray16
- func (p *Gray16) At(x, y int) color.Color
- func (p *Gray16) Bounds() Rectangle
- func (p *Gray16) ColorModel() color.Model
- func (p *Gray16) Opaque() bool
- func (p *Gray16) PixOffset(x, y int) int
- func (p *Gray16) Set(x, y int, c color.Color)
- func (p *Gray16) SetGray16(x, y int, c color.Gray16)
- func (p *Gray16) SubImage(r Rectangle) Image
- type Image
- type NRGBA
- func (p *NRGBA) At(x, y int) color.Color
- func (p *NRGBA) Bounds() Rectangle
- func (p *NRGBA) ColorModel() color.Model
- func (p *NRGBA) Opaque() bool
- func (p *NRGBA) PixOffset(x, y int) int
- func (p *NRGBA) Set(x, y int, c color.Color)
- func (p *NRGBA) SetNRGBA(x, y int, c color.NRGBA)
- func (p *NRGBA) SubImage(r Rectangle) Image
- type NRGBA64
- func (p *NRGBA64) At(x, y int) color.Color
- func (p *NRGBA64) Bounds() Rectangle
- func (p *NRGBA64) ColorModel() color.Model
- func (p *NRGBA64) Opaque() bool
- func (p *NRGBA64) PixOffset(x, y int) int
- func (p *NRGBA64) Set(x, y int, c color.Color)
- func (p *NRGBA64) SetNRGBA64(x, y int, c color.NRGBA64)
- func (p *NRGBA64) SubImage(r Rectangle) Image
- type Paletted
- func (p *Paletted) At(x, y int) color.Color
- func (p *Paletted) Bounds() Rectangle
- func (p *Paletted) ColorIndexAt(x, y int) uint8
- func (p *Paletted) ColorModel() color.Model
- func (p *Paletted) Opaque() bool
- func (p *Paletted) PixOffset(x, y int) int
- func (p *Paletted) Set(x, y int, c color.Color)
- func (p *Paletted) SetColorIndex(x, y int, index uint8)
- func (p *Paletted) SubImage(r Rectangle) Image
- type PalettedImage
- type Point
- type RGBA
- func (p *RGBA) At(x, y int) color.Color
- func (p *RGBA) Bounds() Rectangle
- func (p *RGBA) ColorModel() color.Model
- func (p *RGBA) Opaque() bool
- func (p *RGBA) PixOffset(x, y int) int
- func (p *RGBA) Set(x, y int, c color.Color)
- func (p *RGBA) SetRGBA(x, y int, c color.RGBA)
- func (p *RGBA) SubImage(r Rectangle) Image
- type RGBA64
- func (p *RGBA64) At(x, y int) color.Color
- func (p *RGBA64) Bounds() Rectangle
- func (p *RGBA64) ColorModel() color.Model
- func (p *RGBA64) Opaque() bool
- func (p *RGBA64) PixOffset(x, y int) int
- func (p *RGBA64) Set(x, y int, c color.Color)
- func (p *RGBA64) SetRGBA64(x, y int, c color.RGBA64)
- func (p *RGBA64) SubImage(r Rectangle) Image
- type Rectangle
- func (r Rectangle) Add(p Point) Rectangle
- func (r Rectangle) Canon() Rectangle
- func (r Rectangle) Dx() int
- func (r Rectangle) Dy() int
- func (r Rectangle) Empty() bool
- func (r Rectangle) Eq(s Rectangle) bool
- func (r Rectangle) In(s Rectangle) bool
- func (r Rectangle) Inset(n int) Rectangle
- func (r Rectangle) Intersect(s Rectangle) Rectangle
- func (r Rectangle) Overlaps(s Rectangle) bool
- func (r Rectangle) Size() Point
- func (r Rectangle) String() string
- func (r Rectangle) Sub(p Point) Rectangle
- func (r Rectangle) Union(s Rectangle) Rectangle
- type Uniform
- type YCbCr
- type YCbCrSubsampleRatio
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Black is an opaque black uniform image. Black = NewUniform(color.Black) // White is an opaque white uniform image. White = NewUniform(color.White) // Transparent is a fully transparent uniform image. Transparent = NewUniform(color.Transparent) // Opaque is a fully opaque uniform image. Opaque = NewUniform(color.Opaque) )
var ErrFormat = errors.New("image: unknown format")
ErrFormat indicates that decoding encountered an unknown format.
Functions ¶
func RegisterFormat ¶
func RegisterFormat(name, magic string, decode func(io.Reader) (Image, error), decodeConfig func(io.Reader) (Config, error))
RegisterFormat registers an image format for use by Decode. Name is the name of the format, like "jpeg" or "png". Magic is the magic prefix that identifies the format's encoding. The magic string can contain "?" wildcards that each match any one byte. Decode is the function that decodes the encoded image. DecodeConfig is the function that decodes just its configuration.
Types ¶
type Alpha ¶
type Alpha struct { // Pix holds the image's pixels, as alpha values. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*1]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
Alpha is an in-memory image whose At method returns color.Alpha values.
func (*Alpha) ColorModel ¶
func (*Alpha) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
type Alpha16 ¶
type Alpha16 struct { // Pix holds the image's pixels, as alpha values in big-endian format. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*2]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
Alpha16 is an in-memory image whose At method returns color.Alpha64 values.
func NewAlpha16 ¶
NewAlpha16 returns a new Alpha16 with the given bounds.
func (*Alpha16) ColorModel ¶
func (*Alpha16) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
type Config ¶
Config holds an image's color model and dimensions.
func DecodeConfig ¶
DecodeConfig decodes the color model and dimensions of an image that has been encoded in a registered format. The string returned is the format name used during format registration. Format registration is typically done by the init method of the codec-specific package.
type Gray ¶
type Gray struct { // Pix holds the image's pixels, as gray values. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*1]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
Gray is an in-memory image whose At method returns color.Gray values.
func (*Gray) ColorModel ¶
type Gray16 ¶
type Gray16 struct { // Pix holds the image's pixels, as gray values in big-endian format. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*2]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
Gray16 is an in-memory image whose At method returns color.Gray16 values.
func (*Gray16) ColorModel ¶
func (*Gray16) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
type Image ¶
type Image interface { // ColorModel returns the Image's color model. ColorModel() color.Model // Bounds returns the domain for which At can return non-zero color. // The bounds do not necessarily contain the point (0, 0). Bounds() Rectangle // At returns the color of the pixel at (x, y). // At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid. // At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one. At(x, y int) color.Color }
Image is a finite rectangular grid of color.Color values taken from a color model.
type NRGBA ¶
type NRGBA struct { // Pix holds the image's pixels, in R, G, B, A order. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
NRGBA is an in-memory image whose At method returns color.NRGBA values.
func (*NRGBA) ColorModel ¶
func (*NRGBA) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
type NRGBA64 ¶
type NRGBA64 struct { // Pix holds the image's pixels, in R, G, B, A order and big-endian format. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*8]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
NRGBA64 is an in-memory image whose At method returns color.NRGBA64 values.
func NewNRGBA64 ¶
NewNRGBA64 returns a new NRGBA64 with the given bounds.
func (*NRGBA64) ColorModel ¶
func (*NRGBA64) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
type Paletted ¶
type Paletted struct { // Pix holds the image's pixels, as palette indices. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*1]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle // Palette is the image's palette. Palette color.Palette }
Paletted is an in-memory image of uint8 indices into a given palette.
func NewPaletted ¶
NewPaletted returns a new Paletted with the given width, height and palette.
func (*Paletted) ColorIndexAt ¶
func (*Paletted) ColorModel ¶
func (*Paletted) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
func (*Paletted) PixOffset ¶
PixOffset returns the index of the first element of Pix that corresponds to the pixel at (x, y).
func (*Paletted) SetColorIndex ¶
type PalettedImage ¶
type PalettedImage interface { // ColorIndexAt returns the palette index of the pixel at (x, y). ColorIndexAt(x, y int) uint8 Image }
PalettedImage is an image whose colors may come from a limited palette. If m is a PalettedImage and m.ColorModel() returns a PalettedColorModel p, then m.At(x, y) should be equivalent to p[m.ColorIndexAt(x, y)]. If m's color model is not a PalettedColorModel, then ColorIndexAt's behavior is undefined.
type Point ¶
type Point struct {
X, Y int
}
A Point is an X, Y coordinate pair. The axes increase right and down.
var ZP Point
ZP is the zero Point.
func (Point) Mod ¶
Mod returns the point q in r such that p.X-q.X is a multiple of r's width and p.Y-q.Y is a multiple of r's height.
type RGBA ¶
type RGBA struct { // Pix holds the image's pixels, in R, G, B, A order. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
RGBA is an in-memory image whose At method returns color.RGBA values.
func (*RGBA) ColorModel ¶
type RGBA64 ¶
type RGBA64 struct { // Pix holds the image's pixels, in R, G, B, A order and big-endian format. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*8]. Pix []uint8 // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect Rectangle }
RGBA64 is an in-memory image whose At method returns color.RGBA64 values.
func (*RGBA64) ColorModel ¶
func (*RGBA64) Opaque ¶
Opaque scans the entire image and returns whether or not it is fully opaque.
type Rectangle ¶
type Rectangle struct {
Min, Max Point
}
A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. It is well-formed if Min.X <= Max.X and likewise for Y. Points are always well-formed. A rectangle's methods always return well-formed outputs for well-formed inputs.
var ZR Rectangle
ZR is the zero Rectangle.
func (Rectangle) Canon ¶
Canon returns the canonical version of r. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.
func (Rectangle) Inset ¶
Inset returns the rectangle r inset by n, which may be negative. If either of r's dimensions is less than 2*n then an empty rectangle near the center of r will be returned.
func (Rectangle) Intersect ¶
Intersect returns the largest rectangle contained by both r and s. If the two rectangles do not overlap then the zero rectangle will be returned.
type Uniform ¶
Uniform is an infinite-sized Image of uniform color. It implements the color.Color, color.Model, and Image interfaces.
func NewUniform ¶
func (*Uniform) ColorModel ¶
type YCbCr ¶
type YCbCr struct {
Y, Cb, Cr []uint8
YStride int
CStride int
SubsampleRatio YCbCrSubsampleRatio
Rect Rectangle
}
YCbCr is an in-memory image of Y'CbCr colors. There is one Y sample per pixel, but each Cb and Cr sample can span one or more pixels. YStride is the Y slice index delta between vertically adjacent pixels. CStride is the Cb and Cr slice index delta between vertically adjacent pixels that map to separate chroma samples. It is not an absolute requirement, but YStride and len(Y) are typically multiples of 8, and:
For 4:4:4, CStride == YStride/1 && len(Cb) == len(Cr) == len(Y)/1. For 4:2:2, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/2. For 4:2:0, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/4. For 4:4:0, CStride == YStride/1 && len(Cb) == len(Cr) == len(Y)/2.
func NewYCbCr ¶
func NewYCbCr(r Rectangle, subsampleRatio YCbCrSubsampleRatio) *YCbCr
NewYCbCr returns a new YCbCr with the given bounds and subsample ratio.
func (*YCbCr) COffset ¶
COffset returns the index of the first element of Cb or Cr that corresponds to the pixel at (x, y).
func (*YCbCr) ColorModel ¶
type YCbCrSubsampleRatio ¶
type YCbCrSubsampleRatio int
YCbCrSubsampleRatio is the chroma subsample ratio used in a YCbCr image.
const ( YCbCrSubsampleRatio444 YCbCrSubsampleRatio = iota YCbCrSubsampleRatio422 YCbCrSubsampleRatio420 YCbCrSubsampleRatio440 )
func (YCbCrSubsampleRatio) String ¶
func (s YCbCrSubsampleRatio) String() string
Directories ¶
Path | Synopsis |
---|---|
Package color implements a basic color library.
|
Package color implements a basic color library. |
Package draw provides image composition functions.
|
Package draw provides image composition functions. |
Package gif implements a GIF image decoder.
|
Package gif implements a GIF image decoder. |
Package jpeg implements a JPEG image decoder and encoder.
|
Package jpeg implements a JPEG image decoder and encoder. |
Package png implements a PNG image decoder and encoder.
|
Package png implements a PNG image decoder and encoder. |