This is a creation in Article, where the information may have evolved or changed.
Image package implements a basic 2D image library, which contains the basic interface called image, which contains color, which is described in Image/color,
There are several ways to create the value of the image interface:
1 Calling Newrgba and newpaletted
2 decoding a io.reader that contains image data in Gif.jpen or PNG format
First, some image interfaces are introduced
Type Image//image is a color that is taken from the colour model. A rectangular grid of color
Type Image Interface {ColorModel () color. Model//colormodel Returns the color of the picture. Modelbounds () Rectangle//Picture non-0color region at (x, y int) color. Color//Returns the pixel color of the specified point. Color}
Any struct implements the image interface as long as the three methods in the image are implemented
Func Decode (R io. Reader) (image, String, error)//decode decodes a picture encoded according to the specified format, and string returns the formatted name used during the registration process, such as "GIF" or "JPEG".
Func Registerformat (Name, magic String, decode func (IO). Reader) (Image, error), decodeconfig func (IO). Reader) (Config, error))
Registerformat registers an image format for decoding use, name is a formatted name, such as "JPEG" or "PNG", magic indicates the prefix of the formatted encoding, the magic string can contain a character, to match any one character, Decode is a function used to decode "encoded images", and Decodeconfig is a function that simply decodes its configuration.
Type Alpha//used to set the transparency of the picture
The type alpha struct {pix []uint8//PIX stores the pixels of the picture, like the alpha value. pixels in (x, y) starts at pix[(Y-RECT.MIN.Y) *stride + (x-rect.min.x) * 1]. Stride int //Stride represents the stride length between two pixels vertically (distance) Rect Rectangle//rect represents the picture boundary.}
Func Newalpha (R Rectangle) *alpha//using a given rectangular boundary to produce an Alpha
Func (P *alpha) Alphaat (x, y int) color. Alpha//Gets the transparency of the specified point
Func (P *alpha) at (x, y int) color. Color//Gets the color of the specified point (the red-green-blue transparency of the point)
Func (P *alpha) Bounds () Rectangle//Get Alpha boundary
Func (P *alpha) ColorModel () color. Model//Get alpha color models
Func (P *alpha) Opaque () bool//Check if Alpha is completely opaque
Func (P *alpha) pixoffset (x, y int) int//Gets the relative offset of the specified pixel relative to the first pixel
Func (P *alpha) Set (x, y int, C color.) color)//Set color at the specified position
Func (P *alpha) Setalpha (x, y int, C color.) Alpha)//Set Alpha at the specified position
Func (P *alpha) subimage (R Rectangle) image//Get a sub-image in P image that is covered by R, parent image and sub-image common pixels
The following examples illustrate their usage:
Package Mainimport ("FMT" "Image" "Image/color" "Image/jpeg" "Log" "OS") const (DX = 500dy = $) Func main () {file, err: = OS. Create ("Test.jpeg") if err! = Nil {log. Fatal (Err)}defer file. Close () Alpha: = image. Newalpha (image. Rect (0, 0, dx, dy)) for x: = 0; x < DX; X + + {for y: = 0; y < dy; y++ {alpha. Set (x, y, color. Alpha{uint8 (x%)}) //Set the alpha image transparency}}fmt. Println (Alpha. at (+))//144 the pixel FMT at the specified position. Println (Alpha. Bounds ()) //(0,0)-(500,200), picture border FMT. Println (Alpha. Opaque ()) //false, whether the picture is completely transparent FMT. Println (Alpha. Pixoffset (1, 1))//501, which refers to the distance of the fixed point relative to the first point of FMT. Println (Alpha. Stride) //500, a distance of two vertical pixels between jpeg. Encode (file, alpha, nil) //write image information to file}
The resulting graphs are as follows:
Because each type has a similar method, the following is no longer a distance description.
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) * *]. The big-endian mode, so pixel compute and alpha differs from PIX []uint8//Stride is the pix Stride (in bytes) between vertically adjacent pixels. Stride int//Rect is the image ' s bounds. Rect Rectangle}
The alpha16 of Chinese law is similar to the usage of Alpha Chinese law, which is not mentioned here.
Func NewAlpha16 (r Rectangle) *alpha16
Func (P *alpha16) alpha16at (x, y int) color. 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//Includes image color model and wide height size
Type Config struct {colormodel color. Modelwidth, Height int}
Func decodeconfig (R io. Reader) (Config, string, error)
Type gray//To set the grayscale of the picture
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) *]. PIX []uint8//Stride is the pix Stride (in bytes) between vertically adjacent pixels. Stride int//Rect is the image ' s bounds. Rect Rectangle}
Where the gray method is exactly the same as Alpha, not repeating
Func Newgray (r Rectangle) *gray
Func (P *gray) at (x, y int) color. Color
Func (P *gray) Bounds () Rectangle
Func (P *gray) ColorModel () color. Model
Func (P *gray) Grayat (x, y int) color. Gray
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
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) * *]. PIX []uint8//Stride is the pix Stride (in bytes) between vertically adjacent pixels. Stride int//Rect is the image ' s bounds. Rect Rectangle}
The Gray16 method is exactly the same as the Alpha16 method and does not repeat
Func NewGray16 (r Rectangle) *GRAY16
Func (P *GRAY16) at (x, y int) color. Color
Func (P *GRAY16) Bounds () Rectangle
Func (P *GRAY16) ColorModel () color. Model
Func (P *gray16) gray16at (x, y int) color. Gray16
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 Nrgba
Type Nrgba struct {//Pix holds the image ' s pixels, in R, G, B, A order. The pixel at//(x, y) starts at <span style= "color: #FF0000;" >pix[(Y-RECT.MIN.Y) *stride + (x-rect.min.x) *4]</span>. PIX []uint8//Stride is the pix Stride (in bytes) between vertically adjacent pixels. Stride int//Rect is the image ' s bounds. Rect Rectangle}
Func Newnrgba (r Rectangle) *nrgba
Func (P *nrgba) at (x, y int) color. Color
Func (P *nrgba) Bounds () Rectangle
Func (P *nrgba) ColorModel () color. Model
Func (P *nrgba) Nrgbaat (x, y int) color. Nrgba
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
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}
Func NewNRGBA64 (r Rectangle) *nrgba64
Func (P *nrgba64) at (x, y int) color. Color
Func (P *nrgba64) Bounds () Rectangle
Func (P *nrgba64) ColorModel () color. Model
Func (P *nrgba64) nrgba64at (x, y int) color. NRGBA64
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
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) *]. 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. The picture's palette palette color. Palette}
Func newpaletted (r Rectangle, p color. Palette) *paletted//Generate a new palette based on the specified width and color palette
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//Palette Image interface
Type Palettedimage Interface {colorindexat (x, y int) uint8//return index of pixels at position (x, y) image//image Interface}
Type point//(x, y) coordinate pairs of points
Type point struct {X, Y int}
Func Pt (X, Y int) Point//pt is a shorthand for point{x, y}
The Func (P point) Add (q points) point//two vector dot sum
Func (P point) Div (k int) point//div returns the vector p/k, to find the value of point/k
Func (P point) Eq (Q-point) bool//Determines whether two vector points are equal
Func (P point) in (R Rectangle) bool//Determine if a vector dot is in the matrix
Func (P point) Mod (R-Rectangle) point//in Matrix R to find a dot Q, yes p.x-q.x is a multiple of the width of the matrix, P.Y-Q.Y is a multiple of the matrix height
Func (P point) Mul (k int) dot//return vector point and the product of the specified value
Func (P Point) string () string//returns the vector point represented by string, with a style such as
The Func (P point) Sub (q point) dot//two vector points differential
Examples are as follows:
Func Main () {pt: = image. Point{x:5, Y:5}fmt. Println (PT) //(5,5), outputs a point position (x, y) fmt.println (image. PT (1, 2)) //(FMT.PRINTLN) , PT outputs a short form of point position (Pt. ADD (image. Pt (1, 1)) //(6,6), two points summation fmt. Println (Pt. String ()) //(5,5), outputting the point fmt as a string. Println (Pt. Eq (image. Pt (5, 5)) //true, determine whether the two points are exactly equal to the FMT. Println (Pt. In (image. Rect (0, 0, ten))) //true, determines whether a point is in the Matrix FMT. Println (Pt. Div (2)) //(2,2), seeking point of the commercial FMT. Println (Pt. Mul (2)) //(10,10), to find the product of the dot fmt. Println (Pt. Sub (image. Pt (1, 1)) //(bis), to find the difference between the two points fmt. Println (Pt. Mod (image. Rect (9, 8, ten,)) //(9,9), dx=10-9=1,dy=10-8=2,9-5=4,4 is a multiple of 1 and 2 and (9,9) in The matrix}
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 are the Pix Stride (in) TES) between vertically adjacent pixels. Stride int//Rect is the image ' s bounds. Rect Rectangle}
Func Newrgba (r Rectangle) *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
Examples of RGBA usage:
Package Mainimport ("FMT" "Image" "Image/color" "Image/jpeg" "Log" "OS") const (DX = 500dy = $) Func main () {file, err: = OS. Create ("test.jpg") if err! = Nil {log. Fatal (Err)}defer file. Close () Rgba: = image. Newrgba (image. Rect (0, 0, dx, dy)) for x: = 0; x < DX; X + + {for y: = 0; y < dy; y++ {Rgba. Set (x, y, color. Nrgba{uint8 (x%), uint8 (y%), 0, 255})}}fmt. Println (Rgba. at ((+)) //{144 0 255}fmt. Println (Rgba. Bounds ()) //(0,0)-(500,200) fmt. Println (Rgba. Opaque ()) //true, its completely transparent FMT. Println (Rgba. Pixoffset (1, 1))//2004fmt. Println (Rgba. Stride) //2000jpeg. Encode (file, Rgba, nil) //Storing image information in file}
The documents obtained are as follows;
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}
Func NewRGBA64 (r Rectangle) *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) rgba64at (x, y int) color. RGBA64
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//use two coordinate points to generate a matrix
Type Rectangle struct {Min, Max point}
Func Rect (x0, y0, x1, y1 int) Rectangle//rect is a shorthand form of rectangle{pt (x0, y0), Pt (x1, y1)}
Func (R Rectangle) ADD (P point) Rectangle///matrix two points sum up a new matrix with the specified points
Func (R Rectangle) Canon () Rectangle//Returns the standard format of the rectangle and, if necessary, the interchange of the minimum and maximum coordinates
Func (R Rectangle) dx () int//return matrix width dx
Func (R Rectangle) dy () int//return matrix height dy
Func (R Rectangle) empty () bool//Determines whether the matrix is empty, that is, does not contain any point
Func (R Rectangle) Eq (s Rectangle) bool//Determine if two matrices are equal, referring to exactly coincident
Func (R Rectangle) in (s Rectangle) bool//Determine if a matrix is within another matrix
Func (R Rectangle) Inset (n int) Rectangle//Returns an embedded matrix based on n, calculated by subtracting n for each coordinate of the matrix, the obtained matrix must be embedded in the known matrix, and if not, an empty matrix is returned
Func (R Rectangle) Intersect (s Rectangle) Rectangle//two matrix intersection matrix, if two matrices do not intersect, return 0 matrix
Func (R Rectangle) overlaps (s Rectangle) bool//Determine if two matrices have intersections, that is, determine if two matrices have common areas
Func (R Rectangle) Size () point//return matrix width and height, i.e. dx and dy
Func (r Rectangle) string representation of the matrix
Func (R Rectangle) Sub (P point) Rectangle//A matrix of two coordinate points minus a specified coordinate point p, resulting in a new matrix
Func (R Rectangle) Union (s Rectangle) Rectangle//Two matrices, this is the opposite of intersect (for the intersection of two matrices)
Illustrate rectangle usage:
Package Mainimport ("FMT" "image") Func main () {RT: = image. Rect (0, 0, rt1): = image. Rect (+, +, ten) fmt. Println (Rt1. Canon ()) //(10,10)-(100,100), rt1 size coordinate swap location FMT. Println (RT, RT1) //(0,0)-(100,50) (10,10)-(100,100) fmt. Println (Rt. Dx (), Rt. Dy ())//100 50, returns the width and height of the matrix FMT. Println (Rt. Empty ()) //false, whether the matrix is an empty FMT. Println (Rt. Eq (RT1)) //false, two matrices are equal to FMT. Println (Rt. In (RT1)) //false, whether the matrix RT is in the Matrix Rt1 FMT. Println (Rt. Inset (x)) //(10,10)-(90,40), find the embedded matrix, with the original matrix coordinate point minus the given value 10 the resulting matrix, the matrix must be the original matrix of the embedded matrix if Rt. Overlaps (rt1) {fmt. Println (Rt. Intersect (RT1))//(10,10)-(100,50)//Two intersection of matrices}fmt. Println (Rt. Size ()) //(100,50), the matrix size, which is equivalent to (Dx,dy) fmt. Println (Rt. String ()) // (0,0)-(100,50) fmt. Println (Rt. Sub (image. Pt (10, 10))//( -10,-10)-(90,40), the difference between a matrix and a point, used to shift the matrix into the FMT. Println (Rt. Union (RT1)) //(0,0)-(100,100), for two matrices}
The type Uniform//uniform is a picture with a uniform color infinity size, which implements color. color, color. Model, and the interface of the image
Type Uniform struct {C color. Color}
Func newuniform (c color. color) *uniform//based on color. Color produces a uniform
Func (c *uniform) at (x, y int) color. Color//Gets the pixel information for the specified point
Func (c *uniform) Bounds () Rectangle//Get the boundary matrix information of an image
Func (c *uniform) ColorModel () color. Model//Get Image Color models
Func (c *uniform) Convert (color. color) color. Color//Converts the pixel information of a picture to another specified pixel information
Func (c *uniform) Opaque () bool//Determine whether the picture is completely transparent
Func (c *uniform) RGBA () (R, G, B, a uint32)//Return the value of the picture's r,g,b,a (red, green, blue, transparency)
Type YCbCr//ycbcr is a y ' cbcr color picture, each y sample represents a pixel, but each CB and CR can represent one or more pixels, Ystride is the Yslice index increment//amount in adjacent vertical pixels, Cstride is CB and CR Slice the index increment in adjacent vertical pixels (mapped to independent chroma sampling). Usually Ystride and Len (Y) are multiples of 8, and the cstride result is as follows:
For 4:4:4, cstride = = ystride/1 && len (Cb) = = Len (Cr) = = Len (Y)/1.for 4:2:2, cstride = YSTRIDE/2 && len (C b) = = 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.
Type YCbCr struct {Y, Cb, Cr []uint8ystride intcstride intsubsampleratio ycbcrsubsampleratiorect Rectangle}
Func newycbcr (R Rectangle, Subsampleratio ycbcrsubsampleratio) *YCBCR//Create new YCbCr with a given boundary and sub-sample scale
Func (P *ycbcr) at (x, y int) color. Color//Gets the pixel of the specified point
Func (P *ycbcr) Bounds () Rectangle//Get image bounds
Func (P *ycbcr) coffset (x, y int) int//Gets the relative position of the pixel point of the specified point relative to the first CB element
Func (P *ycbcr) ColorModel () color. Model//Get color model
Func (P *ycbcr) Opaque () bool//decision is fully transparent
Func (P *ycbcr) subimage (R Rectangle) image//Sub-image of the original image based on the specified matrix
Func (P *ycbcr) Ycbcrat (x, y int) color. YCbCr
Func (P *ycbcr) yoffset (x, y int) int//Gets the relative position of the pixel point relative to the first Y element
The Chroma sub-sample scale of type Ycbcrsubsampleratio//ycbcr, commonly used in NEWYCBCR (R Rectangle, Subsampleratio ycbcrsubsampleratio) to create YCbCr
Const (YCbCrSubsampleRatio444 ycbcrsubsampleratio = iotaycbcrsubsampleratio422ycbcrsubsampleratio420ycbcrsubsampleratio440)
Func (s Ycbcrsubsampleratio) string representation of the//ycbcrsubsampleratio structure of a string () string