本文整理匯總了Golang中image.NewNRGBA函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewNRGBA函數的具體用法?Golang NewNRGBA怎麽用?Golang NewNRGBA使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewNRGBA函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: waterMark
//水印
func waterMark(picBytes []byte) []byte {
// 打開水印圖並解碼
img, fileType, _ := image.Decode(bytes.NewBuffer(picBytes))
//讀取水印圖片
watermark, _ := png.Decode(bytes.NewBuffer(wm))
//原始圖界限
origin_size := img.Bounds()
//創建新圖層
canvas := image.NewNRGBA(origin_size)
//貼原始圖
draw.Draw(canvas, origin_size, img, image.ZP, draw.Src)
//貼水印圖
draw.Draw(canvas, watermark.Bounds().Add(image.Pt(origin_size.Dx()-watermark.Bounds().Dx(), origin_size.Dy()-watermark.Bounds().Dy()-4)), watermark, image.ZP, draw.Over)
//生成新圖片
buff := bytes.NewBuffer([]byte{})
switch fileType {
case "jpeg":
jpeg.Encode(buff, canvas, &jpeg.Options{95})
default:
png.Encode(buff, canvas)
}
return buff.Bytes()
}
示例2: GetCube
// Sets skin.Processed to an isometric render of the head from a top-left angle (showing 3 sides).
func (skin *mcSkin) GetCube(width int) error {
// Crop out the top of the head
topFlat := imaging.Crop(skin.Image, image.Rect(8, 0, 16, 8))
// Resize appropriately, so that it fills the `width` when rotated 45 def.
topFlat = imaging.Resize(topFlat, int(float64(width)*math.Sqrt(2)/3+1), 0, imaging.NearestNeighbor)
// Create the Gift filter
filter := gift.New(
gift.Rotate(45, color.Transparent, gift.LinearInterpolation),
)
bounds := filter.Bounds(topFlat.Bounds())
top := image.NewNRGBA(bounds)
// Draw it on the filter, then smush it!
filter.Draw(top, topFlat)
top = imaging.Resize(top, width+2, width/3, imaging.NearestNeighbor)
// Skew the front and sides at 15 degree angles to match up with the
// head that has been smushed
front := skin.cropHead(skin.Image).(*image.NRGBA)
side := imaging.Crop(skin.Image, image.Rect(0, 8, 8, 16))
front = imaging.Resize(front, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
side = imaging.Resize(side, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
front = skewVertical(front, math.Pi/12)
side = skewVertical(imaging.FlipH(side), math.Pi/-12)
// Create a new image to assemble upon
skin.Processed = image.NewNRGBA(image.Rect(0, 0, width, width))
// Draw each side
draw.Draw(skin.Processed.(draw.Image), image.Rect(0, width/6, width/2, width), side, image.Pt(0, 0), draw.Src)
draw.Draw(skin.Processed.(draw.Image), image.Rect(width/2, width/6, width, width), front, image.Pt(0, 0), draw.Src)
// Draw the top we created
draw.Draw(skin.Processed.(draw.Image), image.Rect(-1, 0, width+1, width/3), top, image.Pt(0, 0), draw.Over)
return nil
}
示例3: rotate
func rotate(im image.Image, angle int) image.Image {
var rotated *image.NRGBA
// trigonometric (i.e counter clock-wise)
switch angle {
case 90:
newH, newW := im.Bounds().Dx(), im.Bounds().Dy()
rotated = image.NewNRGBA(image.Rect(0, 0, newW, newH))
for y := 0; y < newH; y++ {
for x := 0; x < newW; x++ {
rotated.Set(x, y, im.At(newH-1-y, x))
}
}
case -90:
newH, newW := im.Bounds().Dx(), im.Bounds().Dy()
rotated = image.NewNRGBA(image.Rect(0, 0, newW, newH))
for y := 0; y < newH; y++ {
for x := 0; x < newW; x++ {
rotated.Set(x, y, im.At(y, newW-1-x))
}
}
case 180, -180:
newW, newH := im.Bounds().Dx(), im.Bounds().Dy()
rotated = image.NewNRGBA(image.Rect(0, 0, newW, newH))
for y := 0; y < newH; y++ {
for x := 0; x < newW; x++ {
rotated.Set(x, y, im.At(newW-1-x, newH-1-y))
}
}
default:
return im
}
return rotated
}
示例4: GetMoreLineImage
func (this *Signer) GetMoreLineImage(text string, text_color image.Image, l int, fix_top, fix_bottom int) (image.Image, error) {
r := image.Rect(0, 0, 414, 96)
img := image.NewNRGBA(r)
c := freetype.NewContext()
c.SetDPI(this.Dpi)
c.SetFont(this.font)
c.SetFontSize(this.FontSize)
c.SetClip(img.Bounds())
c.SetDst(img)
c.SetSrc(text_color)
//pt := freetype.Pt(0, 0)
pt := freetype.Pt(0, 0+int(c.PointToFix32(this.FontSize)>>8))
var err error
limit := c.PixToFix32(l)
str := strings.Split(text, "\r\n")
for i, s := range str {
pt, err = c.DrawString(s, pt, limit)
if err != nil {
fmt.Println("c.DrawString(%s) error(%v)", s, err)
return nil, err
}
if i < len(str)-1 {
pt.Y += c.PointToFix32(this.FontSize * 1.5)
}
}
x, y := l, c.Fix32ToPix(pt.Y)
sub_r := image.Rect(0, 0, x+20, y+(fix_bottom-fix_top))
sub_img := image.NewNRGBA(sub_r)
draw.Draw(sub_img, sub_r, img, image.Point{0, fix_top}, draw.Src)
return sub_img, nil
}
示例5: Slide
// imgの片方の辺をずらして変形する
// (option=trueで縦/falseで橫に伸ばす/option2=trueで左上固定/falseで右下固定)
// _ _
// | | → / /
//  ̄  ̄
//
func Slide(img *image.Image, length int, option bool, option2 bool) image.Image {
size := (*img).Bounds().Size()
//橫を伸ばす場合は橫の長さがx+x/lenになる
img2 := image.NewNRGBA(image.Rect(0, 0, size.X+int(float64(size.X)/float64(length)), size.Y))
if option {
//縦を伸ばす場合は縦の長さがx+x/lenになる
img2 = image.NewNRGBA(image.Rect(0, 0, size.X, size.Y+int(float64(size.Y)/float64(length))))
}
size2 := (*img2).Bounds().Size()
//統一で扱うために長い方を取得
sizeB := size2.Y
if size2.X > size2.Y {
sizeB = size2.X
}
offset := 0
for x, x2 := 0, 0; x < sizeB; x++ {
//逆スタートの場合は反転
x2 = x
if option2 {
if option {
x2 = size2.X - x
} else {
x2 = size2.Y - x
}
}
for y, y2 := 0, 0; y < sizeB; y++ {
//逆スタートの場合は反転
y2 = y
if option2 {
if option {
y2 = size2.Y - y
} else {
y2 = size2.X - y
}
}
c := (*img).At(x2, y2)
r, g, b, _ := c.RGBA()
if !(r == 0 && g == 0 && b == 0) {
//縦橫の入れ替えの場合
if option {
img2.Set(x2, y2+offset, c)
} else {
img2.Set(y2+offset, x2, c)
}
}
}
//ずらす分
if x2%length == 0 {
offset++
}
}
return img2
}
示例6: BenchmarkPCompare
func BenchmarkPCompare(b *testing.B) {
m1 := image.NewNRGBA(image.Rect(0, 0, 100, 100))
m2 := image.NewNRGBA(image.Rect(0, 0, 100, 100))
d := NewDefaultPerceptual()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
d.Compare(m1, m2)
}
})
}
示例7: TestEncodeDecode
func TestEncodeDecode(t *testing.T) {
imgWithAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3))
imgWithAlpha.Pix = []uint8{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
244, 245, 246, 247, 248, 249, 250, 252, 252, 253, 254, 255,
}
imgWithoutAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3))
imgWithoutAlpha.Pix = []uint8{
0, 1, 2, 255, 4, 5, 6, 255, 8, 9, 10, 255,
127, 128, 129, 255, 131, 132, 133, 255, 135, 136, 137, 255,
244, 245, 246, 255, 248, 249, 250, 255, 252, 253, 254, 255,
}
for _, format := range []Format{JPEG, PNG, GIF, BMP, TIFF} {
img := imgWithoutAlpha
if format == PNG {
img = imgWithAlpha
}
buf := &bytes.Buffer{}
err := Encode(buf, img, format)
if err != nil {
t.Errorf("fail encoding format %s", format)
continue
}
img2, err := Decode(buf)
if err != nil {
t.Errorf("fail decoding format %s", format)
continue
}
img2cloned := Clone(img2)
delta := 0
if format == JPEG {
delta = 3
} else if format == GIF {
delta = 16
}
if !compareNRGBA(img, img2cloned, delta) {
t.Errorf("test [DecodeEncode %s] failed: %#v %#v", format, img, img2cloned)
continue
}
}
buf := &bytes.Buffer{}
err := Encode(buf, imgWithAlpha, Format(100))
if err != ErrUnsupportedFormat {
t.Errorf("expected ErrUnsupportedFormat")
}
}
示例8: TestNrgbaPlanes
func TestNrgbaPlanes(t *testing.T) {
w, h := 256, 256
src := readImage(t, "testdata/nrgba.png")
ref := image.NewNRGBA(src.Bounds())
err := Convert(ref, src, nil)
expect(t, err, nil)
raw := image.NewNRGBA(image.Rect(0, 0, w*2, h*2))
dst := raw.SubImage(image.Rect(7, 7, 7+w, 7+h))
err = Convert(dst, src, NewBicubicFilter())
expect(t, err, nil)
err = Convert(src, dst, NewBicubicFilter())
expect(t, err, nil)
checkPsnrs(t, ref, src, image.Rectangle{}, []float64{39})
}
示例9: NewImageOfTypeRect
func NewImageOfTypeRect(src image.Image, bounds image.Rectangle) image.Image {
switch i := src.(type) {
case *image.Alpha:
return image.NewAlpha(bounds)
case *image.Alpha16:
return image.NewAlpha16(bounds)
case *image.Gray:
return image.NewGray(bounds)
case *image.Gray16:
return image.NewGray16(bounds)
case *image.NRGBA:
return image.NewNRGBA(bounds)
case *image.NRGBA64:
return image.NewNRGBA64(bounds)
case *image.Paletted:
return image.NewPaletted(bounds, i.Palette)
case *image.RGBA:
return image.NewRGBA(bounds)
case *image.RGBA64:
return image.NewRGBA64(bounds)
case *image.YCbCr:
return image.NewYCbCr(bounds, i.SubsampleRatio)
}
panic("Unknown image type")
}
示例10: RoundTrip
func (t testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var raw string
switch req.URL.Path {
case "/ok":
raw = "HTTP/1.1 200 OK\n\n"
case "/error":
return nil, errors.New("http protocol error")
case "/nocontent":
raw = "HTTP/1.1 204 No Content\n\n"
case "/etag":
raw = "HTTP/1.1 200 OK\nEtag: \"tag\"\n\n"
case "/png":
m := image.NewNRGBA(image.Rect(0, 0, 1, 1))
img := new(bytes.Buffer)
png.Encode(img, m)
raw = fmt.Sprintf("HTTP/1.1 200 OK\nContent-Length: %d\n\n%s", len(img.Bytes()), img.Bytes())
default:
raw = "HTTP/1.1 404 Not Found\n\n"
}
buf := bufio.NewReader(bytes.NewBufferString(raw))
return http.ReadResponse(buf, req)
}
示例11: createSwatch
func createSwatch(col color.Color, size int) image.Image {
log.Printf("Creating swatch for %v\n", col)
bounds := image.Rect(0, 0, size, size)
img := image.NewNRGBA(bounds)
draw.Draw(img, img.Bounds(), image.NewUniform(col), img.Bounds().Min, draw.Src)
return img
}
示例12: cut
// cut out the image and return individual channels with image.Image
// no encoding of JPEG
func cut(original image.Image, db *map[string][3]float64, tileSize, x1, y1, x2, y2 int) <-chan image.Image {
c := make(chan image.Image)
sp := image.Point{0, 0}
go func() {
newimage := image.NewNRGBA(image.Rect(x1, y1, x2, y2))
for y := y1; y < y2; y = y + tileSize {
for x := x1; x < x2; x = x + tileSize {
r, g, b, _ := original.At(x, y).RGBA()
color := [3]float64{float64(r), float64(g), float64(b)}
nearest := nearest(color, db)
file, err := os.Open(nearest)
if err == nil {
img, _, err := image.Decode(file)
if err == nil {
t := resize(img, tileSize)
tile := t.SubImage(t.Bounds())
tileBounds := image.Rect(x, y, x+tileSize, y+tileSize)
draw.Draw(newimage, tileBounds, tile, sp, draw.Src)
} else {
fmt.Println("error in decoding nearest", err, nearest)
}
} else {
fmt.Println("error opening file when creating mosaic:", nearest)
}
file.Close()
}
}
c <- newimage.SubImage(newimage.Rect)
}()
return c
}
示例13: draw_image
func draw_image(filename string, plot_map map[Key]Point, width int, height int, gradient string) {
build_gradient(gradient)
fill_palette()
bounds := image.Rect(0, 0, width, height)
b := image.NewNRGBA(bounds)
draw.Draw(b, bounds, image.NewUniform(color.Black), image.ZP, draw.Src)
for x := 0; x < width; x += 1 {
for y := 0; y < height; y += 1 {
var p = plot_map[Key{x, y}]
b.Set(p.X, p.Y, get_colour(p.Escape))
}
}
file, err := os.Create(filename)
if err != nil {
fmt.Println(err)
}
if err = jpeg.Encode(file, b, &jpeg.Options{jpeg.DefaultQuality}); err != nil {
fmt.Println(err)
}
if err = file.Close(); err != nil {
fmt.Println(err)
}
}
示例14: AdjustFunc
// AdjustFunc applies the fn function to each pixel of the img image and returns the adjusted image.
//
// Example:
//
// dstImage = imaging.AdjustFunc(
// srcImage,
// func(c color.NRGBA) color.NRGBA {
// // shift the red channel by 16
// r := int(c.R) + 16
// if r > 255 {
// r = 255
// }
// return color.NRGBA{uint8(r), c.G, c.B, c.A}
// }
// )
//
func AdjustFunc(img image.Image, fn func(c color.NRGBA) color.NRGBA) *image.NRGBA {
src := toNRGBA(img)
width := src.Bounds().Max.X
height := src.Bounds().Max.Y
dst := image.NewNRGBA(image.Rect(0, 0, width, height))
parallel(height, func(partStart, partEnd int) {
for y := partStart; y < partEnd; y++ {
for x := 0; x < width; x++ {
i := y*src.Stride + x*4
j := y*dst.Stride + x*4
r := src.Pix[i+0]
g := src.Pix[i+1]
b := src.Pix[i+2]
a := src.Pix[i+3]
c := fn(color.NRGBA{r, g, b, a})
dst.Pix[j+0] = c.R
dst.Pix[j+1] = c.G
dst.Pix[j+2] = c.B
dst.Pix[j+3] = c.A
}
}
})
return dst
}
示例15: RenderToFile
func (ir *ImageRenderer) RenderToFile(filename string) {
img := image.NewNRGBA(image.Rect(0, 0, ir.Width, ir.Height))
for y := 0; y < ir.Height; y++ {
for x := 0; x < ir.Width; x++ {
i := y*ir.Width*4 + x*4
r := uint8(ir.Pixels[i] * 255.0)
g := uint8(ir.Pixels[i+1] * 255.0)
b := uint8(ir.Pixels[i+2] * 255.0)
a := uint8(ir.Pixels[i+3] * 255.0)
c := color.NRGBA{r, g, b, a}
img.Set(x, y, c)
}
}
imgWriter, _ := os.Create(filename)
defer imgWriter.Close()
var err error
switch filepath.Ext(filename) {
case ".png":
err = png.Encode(imgWriter, img)
case ".jpg", ".jpeg":
err = jpeg.Encode(imgWriter, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
}
if err != nil {
fmt.Printf("Error writing out %v: %v", filename, err)
}
}