std/image
On this page
Index
- type
Rect - type
Color- method
Color.gray - method
Color.gray16 - method
Color.nrgba
- method
- type
Model - type
Image- method
Image.at - method
Image.set - method
Image.sub_image - method
Image.crop - method
Image.to_rgba - method
Image.to_nrgba - method
Image.to_png - method
Image.to_jpeg - method
Image.draw - method
Image.draw_mask - method
Image.fill - method
Image.scale - method
Image.floyd_steinberg
- method
- type
Op - type
Kernel - fn
rgb_to_ycbcr - fn
ycbcr_to_rgb - fn
rgb_to_cmyk - fn
cmyk_to_rgb - fn
from_nrgba - fn
web_safe - fn
plan9 - fn
from_png - fn
from_jpeg
Types
Rect
struct Rect {
x0: Int
y0: Int
x1: Int
y1: Int
} derive EqGo’s image.Rectangle: x0 <= x < x1 and y0 <= y < y1.
Color
struct Color {
r: Int
g: Int
b: Int
a: Int
} derive EqGo’s color.Color.RGBA(): 16 bit channels, alpha premultiplied (each of r, g and b is at most a).
Color.gray
fn (c: Color) gray() -> IntGo’s color.GrayModel: the 8 bit luma of c, (19595 r + 38470 g + 7471 b + 1<<15) » 24 over the 16 bit premultiplied channels.
Color.gray16
fn (c: Color) gray16() -> IntGo’s color.Gray16Model: the same sum » 16.
Color.nrgba
fn (c: Color) nrgba() -> IntGo’s color.NRGBAModel: 8 bit channels, not premultiplied, as 0xRRGGBBAA.
Model
enum Model {
Gray
Gray16
Paletted
RGBA
NRGBA
RGBA64
NRGBA64
CMYK
YCbCr
} derive Eq, DisplayThe pixel models of image/png and image/jpeg, with Go’s layouts:
Gray (1 byte), Gray16 (2, big endian), Paletted (1, an index),
RGBA (4, premultiplied), NRGBA (4), RGBA64 (8, big endian), NRGBA64 (8),
CMYK (4, 255 is full ink) and YCbCr (the planes y, cb and cr).
Image
struct Image {
width: Int
height: Int
model: Model
pix: Bytes
stride: Int = 0
palette: List[Int] = []
min_x: Int = 0
min_y: Int = 0
y: Bytes = b""
cb: Bytes = b""
cr: Bytes = b""
y_stride: Int = 0
c_stride: Int = 0
subsample: jpeg.Subsample = jpeg.Subsample.S444
}A raster image whose top left pixel is (min_x, min_y) (Go’s
Rect().Min; 0, 0 for a decoded image) and whose size is width by
height. pix holds rows of stride bytes; a view made by
sub_image keeps the parent’s stride. palette is for Paletted, one
0xRRGGBBAA (not premultiplied) per index. A YCbCr image keeps its samples
in the planes y, cb and cr (rows of y_stride and c_stride bytes,
0 for tight rows; the chroma subsampled by subsample) and pix is empty.
Image.at
fn (im: Image) at(x: Int, y: Int) -> ColorGo’s Image.At for the pixel models that keep their samples in pix
(everything but YCbCr): the color at (x, y) as 16 bit premultiplied
channels. Outside the image it is what Go’s zero value gives (Gray and
Gray16: opaque black, RGBA, NRGBA, RGBA64 and NRGBA64: transparent,
CMYK: opaque white, Paletted: palette entry 0).
Image.set
fn (var im: Image) set(x: Int, y: Int, c: Color)Go’s Image.Set for the models that keep their samples in pix (not
YCbCr): stores c converted by the model as Go’s color model does (Gray,
Gray16, NRGBA, NRGBA64, CMYK from the premultiplied channels; Paletted
picks the nearest entry by Go’s Palette.Index). A point outside the image
is ignored, as in Go.
Image.sub_image
fn (im: Image) sub_image(r: Rect) -> ImageGo’s Image.SubImage for the models that keep their samples in pix (not
YCbCr): the part of the image inside r (clipped to the image) sharing the
pixels of the parent, with its coordinates kept (min_x, min_y are r’s
corner) and the parent’s stride. An empty intersection gives the empty
image Go returns.
Image.crop
fn (im: Image) crop(r: Rect) -> ImageA tightly packed copy of the part of the image inside r (stride 0, min at r’s corner after clipping).
Image.to_rgba
fn (im: Image) to_rgba() -> ImageThe image as an RGBA image (Go’s draw.Draw of it onto image.NewRGBA with Src, i.e. color.RGBAModel on every pixel): premultiplied 8 bit samples, tight rows, the same size and corner. Not YCbCr yet.
Image.to_nrgba
fn (im: Image) to_nrgba() -> ImageThe image as an NRGBA image (color.NRGBAModel on every pixel).
Image.to_png
fn (im: Image) to_png() -> png.Image?The PNG form of the image (std/png’s Image, tight rows from the corner (0, 0): a sub image is cropped first), or None for a model PNG has no layout for (CMYK, YCbCr).
Image.to_jpeg
fn (im: Image) to_jpeg() -> jpeg.Image?The JPEG form of the image (std/jpeg’s Image, tight rows from (0, 0)), or None for a model JPEG has no layout for (everything but Gray, YCbCr, CMYK and RGBA) and for a YCbCr sub image (crop does not pack planes yet).
Image.draw
fn (var dst: Image) draw(r: Rect, src: Image, sp_x: Int, sp_y: Int, op: Op)Go’s draw.Draw (a nil mask): draws the part of src that lies over r onto the image, aligning r’s corner with the source point (sp_x, sp_y), after clipping r to the image and to the source (draw.clip). Src replaces the pixels, Over composites the source over them (Porter-Duff, alpha premultiplied). Each pixel comes out as Go’s fast paths compute it (an RGBA destination takes 16 bit source channels and 8 bit destination channels as drawRGBA does; NRGBA and NRGBA64 copy a source of their own model with Src; the rest go through the RGBA64 formula and Set). A YCbCr destination is left alone (Go has no Set for it). The samples are copied once per call.
Image.draw_mask
fn (var dst: Image) draw_mask(r: Rect, src: Image, sp_x: Int, sp_y: Int, mask: Image, mp_x: Int, mp_y: Int, op: Op)Go’s draw.DrawMask: as draw, with the alpha of mask (at mask point (mp_x, mp_y) for r’s corner) scaling the source: Over composites src * mask over the pixels, Src replaces them with src * mask. r is also clipped to the mask’s bounds. Any model’s alpha serves as the mask (an opaque Gray, say, is a mask of all ones); the mask’s own coordinates are its min_x and min_y. Each pixel comes out as Go’s general path computes it (16 bit channels, premultiplied), which is what Go’s glyph and mask fast paths compute too.
Image.fill
fn (var dst: Image) fill(r: Rect, c: Color, op: Op)Go’s draw.Draw with an image.Uniform source of color c (16 bit premultiplied channels, as Color is everywhere): fills r clipped to the image, with Src or Over (an opaque c fills as Src either way).
Image.scale
fn (var dst: Image) scale(dr: Rect, src: Image, sr: Rect, op: Op, kernel: Kernel)golang.org/x/image/draw’s Scale: scales the part of src over sr to the part of the image over dr (clipped to the image), with the Porter-Duff op and the interpolator. Equal sizes of dr and sr draw without scaling, as the Go function does. NearestNeighbor picks, for destination pixel (dx, dy) of dr, the source pixel at ((2 dx + 1) sw / (2 dw), (2 dy + 1) sh / (2 dh)) from sr’s corner; source pixels outside the source image read as transparent black (or the palette’s first entry). ApproxBiLinear blends the four nearest source pixels by the fractions of the source position. BiLinear (the tent kernel, support 1) and CatmullRom (support 2) blend the source pixels the kernel reaches in two passes, columns then rows, in float64 as x/image does, and round each channel to 16 bits; an opaque source takes Src for Over.
Image.floyd_steinberg
fn (var dst: Image) floyd_steinberg(r: Rect, src: Image, sp_x: Int, sp_y: Int)Go’s draw.FloydSteinberg.Draw (the Src op with Floyd-Steinberg error diffusion) onto a Paletted image: the part of src over r, clipped as draw clips it, quantized to the nearest palette entry (Go’s sum of squared differences over the four 16 bit channels) with the error spread to the neighbours in sixteenths. Any other destination model is left alone.
Op
enum Op {
Over
Src
} derive Eq, DisplayGo’s draw.Op: how the source combines with the destination.
Kernel
enum Kernel {
NearestNeighbor
ApproxBiLinear
BiLinear
CatmullRom
} derive Eq, DisplayThe interpolation algorithms of golang.org/x/image/draw.
Functions
rgb_to_ycbcr
fn rgb_to_ycbcr(r: Int, g: Int, b: Int) -> (Int, Int, Int)Go’s color.RGBToYCbCr over 8 bit channels: (y, cb, cr).
ycbcr_to_rgb
fn ycbcr_to_rgb(y: Int, cb: Int, cr: Int) -> (Int, Int, Int)Go’s color.YCbCrToRGB: 8 bit (r, g, b).
rgb_to_cmyk
fn rgb_to_cmyk(r: Int, g: Int, b: Int) -> (Int, Int, Int, Int)Go’s color.RGBToCMYK: 8 bit (c, m, y, k).
cmyk_to_rgb
fn cmyk_to_rgb(c: Int, m: Int, y: Int, k: Int) -> (Int, Int, Int)Go’s color.CMYKToRGB: 8 bit (r, g, b).
from_nrgba
fn from_nrgba(v: Int) -> ColorThe color.Color of a 0xRRGGBBAA value that is not premultiplied (Go’s color.NRGBA.RGBA()).
web_safe
fn web_safe() -> List[Int]Go’s palette.WebSafe: the 216 colors with each channel one of 0, 51, 102,
153, 204, 255, red the slowest index and blue the fastest, as
0xRRGGBBAA (alpha 255).
plan9
fn plan9() -> List[Int]Go’s palette.Plan9: the 256 colors of Plan 9’s default palette, as
0xRRGGBBAA (alpha 255), in Go’s order.
from_png
fn from_png(p: png.Image) -> ImageThe image of a decoded PNG (the pixels move, no copy): the same model, tight rows, corner (0, 0).
from_jpeg
fn from_jpeg(j: jpeg.Image) -> ImageThe image of a decoded JPEG (the samples move, no copy): Gray, CMYK and RGBA keep their layout, YCbCr keeps its planes and chroma subsampling.
↑ index