本文整理匯總了Golang中image.NewRGBA函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewRGBA函數的具體用法?Golang NewRGBA怎麽用?Golang NewRGBA使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewRGBA函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewDrawableSize
// NewDrawableSize returns a new draw.Image with the same type as p and the given bounds.
// If p is not a draw.Image, another type is used.
func NewDrawableSize(p image.Image, r image.Rectangle) draw.Image {
switch p := p.(type) {
case *image.RGBA:
return image.NewRGBA(r)
case *image.RGBA64:
return image.NewRGBA64(r)
case *image.NRGBA:
return image.NewNRGBA(r)
case *image.NRGBA64:
return image.NewNRGBA64(r)
case *image.Alpha:
return image.NewAlpha(r)
case *image.Alpha16:
return image.NewAlpha16(r)
case *image.Gray:
return image.NewGray(r)
case *image.Gray16:
return image.NewGray16(r)
case *image.Paletted:
pl := make(color.Palette, len(p.Palette))
copy(pl, p.Palette)
return image.NewPaletted(r, pl)
case *image.CMYK:
return image.NewCMYK(r)
default:
return image.NewRGBA(r)
}
}
示例2: Save
// Save saves the texture as a PNG image.
func (a *TextureAtlas) Save(file string) (err error) {
fd, err := os.Create(file)
if err != nil {
return
}
defer fd.Close()
rect := image.Rect(0, 0, a.width, a.height)
switch a.depth {
case 1:
img := image.NewAlpha(rect)
copy(img.Pix, a.data)
err = png.Encode(fd, img)
case 3:
img := image.NewRGBA(rect)
copy(img.Pix, a.data)
err = png.Encode(fd, img)
case 4:
img := image.NewRGBA(rect)
copy(img.Pix, a.data)
err = png.Encode(fd, img)
}
return
}
示例3: smartCrop
// Smart crop given image file & write it to io.Writer
func smartCrop(w io.Writer, r io.Reader, size []int) error {
img, mimetype, err := image.Decode(r)
if size == nil || err != nil {
io.Copy(w, r)
return nil
}
size = setMaxSize(fitToActualSize(&img, size))
crop, err := smartcrop.SmartCrop(&img, size[0], size[1])
if err != nil {
io.Copy(w, r)
return nil
}
croppedBuffer := image.NewRGBA(image.Rect(0, 0, crop.Width, crop.Height))
draw.Draw(
croppedBuffer,
croppedBuffer.Bounds(),
img,
image.Point{crop.X, crop.Y},
draw.Src,
)
dst := image.NewRGBA(image.Rect(0, 0, size[0], size[1]))
graphics.Scale(dst, croppedBuffer)
return writeByMimetype(w, dst, mimetype)
}
示例4: main
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var templatePath string
if len(os.Args) == 1 {
templatePath = "tux.png"
} else {
templatePath = os.Args[1]
}
template, err := readTemplate(templatePath)
if err != nil {
panic(err)
}
templateBounds := template.Bounds()
sourceHeight := templateBounds.Max.Y
sourceWidth := templateBounds.Max.X
cypherHeight := sourceHeight * 2
cypherWidth := sourceWidth * 2
image1 := image.NewRGBA(image.Rect(0, 0, cypherWidth, cypherHeight))
image2 := image.NewRGBA(image.Rect(0, 0, cypherWidth, cypherHeight))
for x := 0; x <= sourceWidth; x++ {
for y := 0; y <= sourceHeight; y++ {
setPixel(x, y, template.At(x, y), image1, image2)
}
}
writeCypers(image1, image2)
}
示例5: NewPPU
func NewPPU(console *Console) *PPU {
ppu := PPU{Memory: NewPPUMemory(console), console: console}
ppu.front = image.NewRGBA(image.Rect(0, 0, 256, 240))
ppu.back = image.NewRGBA(image.Rect(0, 0, 256, 240))
ppu.Reset()
return &ppu
}
示例6: makePngShield
func makePngShield(w io.Writer, d Data) {
// render text to determine how wide the image has to be
// we leave 6 pixels at the start and end, and 3 for each in the middle
v, vw := renderString(d.Vendor, c)
s, sw := renderString(d.Status, c)
imageWidth := op + vw + ip*2 + sw + op
img := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
draw.Draw(img, img.Bounds(), &image.Uniform{C: d.Color}, image.ZP, draw.Src)
rect := image.Rect(0, 0, op+vw+ip, h)
draw.Draw(img, rect, &image.Uniform{C: Grey}, image.ZP, draw.Src)
dst := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
mask := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
buildMask(mask, imageWidth, edge, draw.Src)
draw.DrawMask(dst, dst.Bounds(), img, image.ZP, mask, image.ZP, draw.Over)
buildMask(dst, imageWidth, gradient, draw.Over)
draw.Draw(dst, dst.Bounds(), v, image.Point{-op, 0}, draw.Over)
draw.Draw(dst, dst.Bounds(), s, image.Point{-(op + vw + ip*2), 0}, draw.Over)
png.Encode(w, dst)
}
示例7: GetImage
func (ff *FontFace) GetImage(text string) (img draw.Image, err error) {
var (
src image.Image
bg image.Image
dst draw.Image
pt fixed.Point26_6
w int
h int
)
src = image.NewUniform(ff.fg)
bg = image.NewUniform(ff.bg)
w = int(float32(len(text)) * ff.charw)
h = int(ff.charh)
dst = image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(dst, dst.Bounds(), bg, image.ZP, draw.Src)
ff.context.SetSrc(src)
ff.context.SetDst(dst)
ff.context.SetClip(dst.Bounds())
pt = freetype.Pt(0, int(ff.charh+ff.offy))
if pt, err = ff.context.DrawString(text, pt); err != nil {
return
}
img = image.NewRGBA(image.Rect(0, 0, int(pt.X/64), int(pt.Y/64)))
draw.Draw(img, img.Bounds(), dst, image.Pt(0, -int(ff.offy)), draw.Src)
return
}
示例8: GetText
func (ff *FontFace) GetText(text string) (t *Texture, err error) {
var (
src image.Image
bg image.Image
dst draw.Image
shortened draw.Image
pt fixed.Point26_6
w int
h int
)
src = image.NewUniform(ff.fg)
bg = image.NewUniform(ff.bg)
w = int(float32(len(text)) * ff.charw)
h = int(ff.charh)
dst = image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(dst, dst.Bounds(), bg, image.ZP, draw.Src)
ff.context.SetSrc(src)
ff.context.SetDst(dst)
ff.context.SetClip(dst.Bounds())
pt = freetype.Pt(0, int(ff.charh))
if pt, err = ff.context.DrawString(text, pt); err != nil {
return
}
// if err = WritePNG("hello.png", dst); err != nil {
// return
// }
shortened = image.NewRGBA(image.Rect(0, 0, int(pt.X/64), h))
draw.Draw(shortened, shortened.Bounds(), dst, image.ZP, draw.Src)
t, err = GetTexture(shortened, gl.NEAREST)
return
}
示例9: TestNegativeWeights
// TestNegativeWeights tests that scaling by a kernel that produces negative
// weights, such as the Catmull-Rom kernel, doesn't produce an invalid color
// according to Go's alpha-premultiplied model.
func TestNegativeWeights(t *testing.T) {
check := func(m *image.RGBA) error {
b := m.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
if c := m.RGBAAt(x, y); c.R > c.A || c.G > c.A || c.B > c.A {
return fmt.Errorf("invalid color.RGBA at (%d, %d): %v", x, y, c)
}
}
}
return nil
}
src := image.NewRGBA(image.Rect(0, 0, 16, 16))
for y := 0; y < 16; y++ {
for x := 0; x < 16; x++ {
a := y * 0x11
src.Set(x, y, color.RGBA{
R: uint8(x * 0x11 * a / 0xff),
A: uint8(a),
})
}
}
if err := check(src); err != nil {
t.Fatalf("src image: %v", err)
}
dst := image.NewRGBA(image.Rect(0, 0, 32, 32))
CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), Over, nil)
if err := check(dst); err != nil {
t.Fatalf("dst image: %v", err)
}
}
示例10: main
func main() {
m := image.NewRGBA(image.Rect(0, 0, 500, 500))
blue := color.RGBA{59, 148, 217, 255}
white := color.RGBA{255, 255, 255, 255}
black := color.RGBA{0, 0, 0, 255}
draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
filename := "egg_mask.png"
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
src, err := png.Decode(f)
if err != nil {
panic(err)
}
mask := image.NewRGBA(image.Rect(0, 0, 500, 500))
draw.Draw(mask, mask.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
draw.DrawMask(m, m.Bounds(), src, image.ZP, mask, image.ZP, draw.Over)
for i := m.Bounds().Min.X; i < m.Bounds().Max.X; i++ {
m.Set(i, m.Bounds().Max.Y/2, black) // to change a single pixel
}
w, _ := os.Create("defaultegg.png")
defer w.Close()
png.Encode(w, m)
}
示例11: TestSrcMask
func TestSrcMask(t *testing.T) {
srcMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
srcMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
srcMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
srcMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
srcMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
red := image.NewUniform(color.RGBA{0xff, 0x00, 0x00, 0xff})
blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
dst := image.NewRGBA(image.Rect(0, 0, 6, 1))
Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
NearestNeighbor.Scale(dst, dst.Bounds(), red, image.Rect(0, 0, 3, 1), Over, &Options{
SrcMask: srcMask,
SrcMaskP: image.Point{20, 0},
})
got := [6]color.RGBA{
dst.RGBAAt(0, 0),
dst.RGBAAt(1, 0),
dst.RGBAAt(2, 0),
dst.RGBAAt(3, 0),
dst.RGBAAt(4, 0),
dst.RGBAAt(5, 0),
}
want := [6]color.RGBA{
{0xff, 0x00, 0x00, 0xff},
{0xff, 0x00, 0x00, 0xff},
{0x3f, 0x00, 0xc0, 0xff},
{0x3f, 0x00, 0xc0, 0xff},
{0x00, 0x00, 0xff, 0xff},
{0x00, 0x00, 0xff, 0xff},
}
if got != want {
t.Errorf("\ngot %v\nwant %v", got, want)
}
}
示例12: ImportOverlays
func (jw *JsonWed) ImportOverlays(wed *Wed) error {
jw.Overlays = make([]jsonWedOverlay, 0)
jw.TileIndices = make([]int, len(wed.TileIndices))
for idx, overlay := range wed.Overlays {
if overlay.Name.String() != "" {
ov := jsonWedOverlay{}
ov.Width = int(overlay.Width)
ov.Height = int(overlay.Height)
ov.Name = overlay.Name.String()
ov.Flags = int(overlay.LayerFlags)
ov.Tilemap = make([]jsonWedTilemap, len(wed.Tilemaps[idx]))
for tmIdx, tilemap := range wed.Tilemaps[idx] {
ov.Tilemap[tmIdx].Id = int(tilemap.TileIndexLookupIndex)
ov.Tilemap[tmIdx].Count = int(tilemap.TileIndexLookupCount)
ov.Tilemap[tmIdx].Alt = int(tilemap.AlternateTileIndex)
ov.Tilemap[tmIdx].Flags = int(tilemap.Flags)
ov.Tilemap[tmIdx].AnimSpeed = int(tilemap.AnimSpeed)
ov.Tilemap[tmIdx].WFlags = int(tilemap.WFlags)
}
tisFile, err := os.Open(overlay.Name.String() + ".tis")
if err != nil {
return fmt.Errorf("unable to open overlay: %s %v", overlay.Name.String(), err)
}
defer tisFile.Close()
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get working directory: %v", err)
}
tis, err := OpenTis(tisFile, overlay.Name.String(), cwd)
if err != nil {
return fmt.Errorf("unable to open tis: %v", err)
}
ov.Tis = tis
img := image.NewRGBA(image.Rect(0, 0, 64*ov.Width, 64*ov.Height))
closedimg := image.NewRGBA(image.Rect(0, 0, 64*ov.Width, 64*ov.Height))
for y := 0; y < int(ov.Height); y++ {
for x := 0; x < int(ov.Width); x++ {
tileNum := y*int(ov.Width) + x
tileImg := tis.SubImage(tileNum)
draw.Draw(img, image.Rect(x*64, y*64, x*64+64, y*64+64), tileImg, image.Pt(0, 0), draw.Src)
if ov.Tilemap[tileNum].Alt != -1 {
tileImg = tis.SubImage(ov.Tilemap[tileNum].Alt)
draw.Draw(closedimg, image.Rect(x*64, y*64, x*64+64, y*64+64), tileImg, image.Pt(0, 0), draw.Src)
}
}
}
ov.BackgroundImg = img
ov.ClosedImage = closedimg
jw.Overlays = append(jw.Overlays, ov)
}
}
for idx, ti := range wed.TileIndices {
jw.TileIndices[idx] = int(ti)
}
return nil
}
示例13: maskFor
func maskFor(in image.Image) image.Image {
b := in.Bounds()
// thumb size, corresponds to first convert command
thumbWidth := int(b.Dx() / 5)
thumbHeight := int(b.Dy() / 5)
thumb := image.NewRGBA(image.Rect(0, 0, thumbWidth, thumbHeight))
final := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
// fill black (xc:black)
// draw white rectangle (1,1 : thumbWidth-1,thumbHeight-1)
for y := 0; y < thumbHeight; y++ {
for x := 0; x < thumbWidth; x++ {
if (y > 0 && y < thumbHeight-1) && (x > 0 && x < thumbWidth-1) {
thumb.Set(x, y, color.White)
} else {
thumb.Set(x, y, color.Black)
}
}
}
// apply Gaussian blur with radius=7, sigma=15
graphics.Blur(thumb, thumb, &graphics.BlurOptions{2, 7})
// now resize to original image size
resized := resize.Resize(uint(b.Dx()), uint(b.Dy()), thumb, resize.Bilinear)
// with gaussian blur radius=0, sigma=5
graphics.Blur(final, resized, &graphics.BlurOptions{5, 0})
return final
}
示例14: Adjust
// Adjust adjusts the two images aligning ther centers. The background
// of the smaller image is filled with FillColor. The returned images
// are of the same size.
func (c Centerer) Adjust(img1, img2 image.Image) (image.Image, image.Image) {
img1Rect := img1.Bounds()
img2Rect := img2.Bounds()
backgroundRect := img1Rect.Union(img2Rect)
dstImg1 := image.NewRGBA(backgroundRect)
dstImg2 := image.NewRGBA(backgroundRect)
// Fill destination images with FillColor
draw.Draw(dstImg1, dstImg1.Bounds(), &image.Uniform{c.FillColor}, image.ZP, draw.Src)
draw.Draw(dstImg2, dstImg2.Bounds(), &image.Uniform{c.FillColor}, image.ZP, draw.Src)
// Copy img1 to the center of dstImg1
dp := image.Point{
(backgroundRect.Max.X-backgroundRect.Min.X)/2 - (img1Rect.Max.X-img1Rect.Min.X)/2,
(backgroundRect.Max.Y-backgroundRect.Min.Y)/2 - (img1Rect.Max.Y-img1Rect.Min.Y)/2,
}
r := image.Rectangle{dp, dp.Add(img1Rect.Size())}
draw.Draw(dstImg1, r, img1, image.ZP, draw.Src)
// Copy img2 to the center of dstImg2
dp = image.Point{
(backgroundRect.Max.X-backgroundRect.Min.X)/2 - (img2Rect.Max.X-img2Rect.Min.X)/2,
(backgroundRect.Max.Y-backgroundRect.Min.Y)/2 - (img2Rect.Max.Y-img2Rect.Min.Y)/2,
}
r = image.Rectangle{dp, dp.Add(img2Rect.Size())}
draw.Draw(dstImg2, r, img2, image.ZP, draw.Src)
return dstImg1, dstImg2
}
示例15: TestGMProcess
func TestGMProcess(t *testing.T) {
buffer := new(bytes.Buffer)
png.Encode(buffer, image.NewRGBA(image.Rect(0, 0, 200, 200)))
gm := NewGMProcessor()
params, _ := ParseParams("r_100x100,c_50x50,g_c,q_50")
img, err := gm.Process(params, "text.png", buffer)
assert.NoError(t, err)
assert.NotNil(t, img)
assert.True(t, img.Len() > 0)
buffer = new(bytes.Buffer)
png.Encode(buffer, image.NewRGBA(image.Rect(0, 0, 200, 200)))
params, _ = ParseParams("r_100x0,c_50x50,g_c,q_50")
img, err = gm.Process(params, "text.png", buffer)
assert.NoError(t, err)
assert.NotNil(t, img)
assert.True(t, img.Len() > 0)
buffer = new(bytes.Buffer)
png.Encode(buffer, image.NewRGBA(image.Rect(0, 0, 200, 200)))
params, _ = ParseParams("r_0x100,c_50x50,g_c,q_50")
img, err = gm.Process(params, "text.png", buffer)
assert.NoError(t, err)
assert.NotNil(t, img)
assert.True(t, img.Len() > 0)
}