本文整理匯總了Golang中image.NewRGBA64函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewRGBA64函數的具體用法?Golang NewRGBA64怎麽用?Golang NewRGBA64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewRGBA64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
var outFile *os.File
var err error
if outFile, err = os.Create("create.png"); err != nil {
println("Error", err)
return
}
defer outFile.Close()
rect := image.Rect(0, 0, 100, 100)
rgba := image.NewRGBA64(rect)
// #golangとか書きたいけど、とりあえず#だけ
for i := 0; i < 10; i++ {
rgba.Set(60, (10 + i), image.Black.At(0, 0))
rgba.Set(65, (10 + i), image.Black.At(0, 0))
rgba.Set((58 + i), 13, image.Black.At(0, 0))
rgba.Set((58 + i), 16, image.Black.At(0, 0))
}
outImage := rgba.SubImage(rect)
if err = png.Encode(outFile, outImage); err != nil {
println("Error", err)
return
}
}
示例2: Resize
// Resize an image to new width and height using the interpolation function interp.
// A new image with the given dimensions will be returned.
// If one of the parameters width or height is set to 0, its size will be calculated so that
// the aspect ratio is that of the originating image.
// The resizing algorithm uses channels for parallel computation.
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
oldBounds := img.Bounds()
oldWidth := float32(oldBounds.Dx())
oldHeight := float32(oldBounds.Dy())
scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
tempImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
b := tempImg.Bounds()
adjust := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
n := numJobs(b.Dy())
c := make(chan int, n)
for i := 0; i < n; i++ {
slice := image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n)
go resizeSlice(img, tempImg, interp, scaleX, adjust, float32(oldBounds.Min.X), slice, c)
}
for i := 0; i < n; i++ {
<-c
}
resultImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
b = resultImg.Bounds()
adjust = 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))
for i := 0; i < n; i++ {
slice := image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n)
go resizeSlice(tempImg, resultImg, interp, scaleY, adjust, float32(oldBounds.Min.Y), slice, c)
}
for i := 0; i < n; i++ {
<-c
}
return resultImg
}
示例3: 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")
}
示例4: orient
func orient(r io.Reader, i image.Image) (image.Image, error) {
t := log.Start()
defer log.End(t)
e, err := exif.Decode(r)
if err != nil {
log.Printf("fail to decode exif: %v", err)
return nil, err
}
tag, err := e.Get(exif.Orientation)
// Orientationタグが存在しない場合、処理を完了する
if err != nil {
log.Println("oritentation tag doesn't exist")
return nil, err
}
o, err := tag.Int(0)
if err != nil {
log.Println("oritentation tag is't int")
return nil, err
}
rect := i.Bounds()
// orientation=5~8 なら畫像サイズの縦橫を入れ替える
if o >= 5 && o <= 8 {
rect = RotateRect(rect)
}
d := image.NewRGBA64(rect)
a := affines[o]
a.TransformCenter(d, i, interp.Bilinear)
return d, nil
}
示例5: 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)
}
}
示例6: Resample
// Resample returns a resampled copy of the image slice r of m.
// The returned image has width w and height h.
func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
if w < 0 || h < 0 {
return nil
}
if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
return image.NewRGBA64(image.Rect(0, 0, w, h))
}
img := image.NewRGBA(image.Rect(0, 0, w, h))
xStep := float64(r.Dx()) / float64(w)
yStep := float64(r.Dy()) / float64(h)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
xSrc := int(float64(r.Min.X) + float64(x)*xStep)
ySrc := int(float64(r.Min.Y) + float64(y)*yStep)
r, g, b, a := m.At(xSrc, ySrc).RGBA()
img.SetRGBA(x, y, color.RGBA{
R: uint8(r >> 8),
G: uint8(g >> 8),
B: uint8(b >> 8),
A: uint8(a >> 8),
})
}
}
return img
}
示例7: Crop
func Crop(m image.Image, r image.Rectangle, w, h int) image.Image {
if w < 0 || h < 0 {
return nil
}
if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
return image.NewRGBA64(image.Rect(0, 0, w, h))
}
curw, curh := r.Min.X, r.Min.Y
img := image.NewRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
// Get a source pixel.
subx := curw + x
suby := curh + y
r32, g32, b32, a32 := m.At(subx, suby).RGBA()
r := uint8(r32 >> 8)
g := uint8(g32 >> 8)
b := uint8(b32 >> 8)
a := uint8(a32 >> 8)
img.SetRGBA(x, y, color.RGBA{r, g, b, a})
}
}
return img
}
示例8: TestIsHighQuality
func TestIsHighQuality(t *testing.T) {
r := image.Rect(0, 0, 1, 1)
for _, tc := range []struct {
name string
p image.Image
expected bool
}{
{
name: "RGBA64",
p: image.NewRGBA64(r),
expected: true,
},
{
name: "NRGBA64",
p: image.NewNRGBA64(r),
expected: true,
},
{
name: "RGBA",
p: image.NewRGBA(r),
expected: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
res := isHighQuality(tc.p)
if res != tc.expected {
t.Fatalf("unexpected result for %T: got %t, want %t", tc.p, res, tc.expected)
}
})
}
}
示例9: TestIsHighQuality
func TestIsHighQuality(t *testing.T) {
r := image.Rect(0, 0, 1, 1)
type TC struct {
p image.Image
expected bool
}
for _, tc := range []TC{
{
p: image.NewRGBA64(r),
expected: true,
},
{
p: image.NewNRGBA64(r),
expected: true,
},
{
p: image.NewRGBA(r),
expected: false,
},
} {
res := isHighQuality(tc.p)
if res != tc.expected {
t.Fatalf("unexpected result for %T: got %t, want %t", tc.p, res, tc.expected)
}
}
}
示例10: createRandomImage
func createRandomImage(maxPaletteSize int) image.Image {
r := image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{gen.Rand(32) + 1, gen.Rand(32) + 1}}
var img Image
switch gen.Rand(10) {
case 0:
img = image.NewAlpha(r)
case 1:
img = image.NewAlpha16(r)
case 2:
img = image.NewCMYK(r)
case 3:
img = image.NewGray(r)
case 4:
img = image.NewGray16(r)
case 5:
img = image.NewNRGBA(r)
case 6:
img = image.NewNRGBA64(r)
case 7:
img = image.NewPaletted(r, randPalette(maxPaletteSize))
case 8:
img = image.NewRGBA(r)
case 9:
img = image.NewRGBA64(r)
default:
panic("bad")
}
fill := gen.Rand(19)
var palette []color.Color
if fill == 17 {
palette = randPalette(maxPaletteSize)
}
for y := 0; y < r.Max.Y; y++ {
for x := 0; x < r.Max.X; x++ {
switch {
case fill <= 15:
img.Set(x, y, color.RGBA64{
^uint16(0) * uint16((fill>>0)&1),
^uint16(0) * uint16((fill>>1)&1),
^uint16(0) * uint16((fill>>2)&1),
^uint16(0) * uint16((fill>>3)&1),
})
case fill == 16:
img.Set(x, y, randColor())
case fill == 17:
img.Set(x, y, palette[gen.Rand(len(palette))])
case fill == 18:
if gen.Rand(3) != 0 {
img.Set(x, y, color.RGBA64{})
} else {
img.Set(x, y, randColor())
}
default:
panic("bad")
}
}
}
return img.(image.Image)
}
示例11: main
func main() {
var cpus *int
if runtime.GOMAXPROCS(0) == 1 {
cpus = flag.Int("cpus", runtime.NumCPU(), "the number of processor cores to use at any given time")
} else {
cpus = flag.Int("cpus", runtime.GOMAXPROCS(0), "the number of processor cores to use at any given time")
}
flag.Parse()
if *cpuprof != "" {
f, err := os.Create(*cpuprof)
if err != nil {
panic(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
runtime.GOMAXPROCS(*cpus)
noise0 = NewNoiseGen(0, *oct)
noise1 = NewNoiseGen(1, *oct)
noise2 = NewNoiseGen(2, *oct)
var wg sync.WaitGroup
dim := *ppi * *ppp
img := image.NewRGBA64(image.Rect(0, 0, dim, dim))
ch := make(chan [2]int, 16)
for i := 0; i < *cpus; i++ {
go Worker(ch, img, &wg)
}
for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
wg.Add(1)
ch <- [2]int{x, y}
}
}
close(ch)
wg.Wait()
f, err := os.Create(*output)
if err != nil {
panic(err)
}
defer f.Close()
err = png.Encode(f, img)
if err != nil {
panic(err)
}
}
示例12: ConvertToRGBA64
// ConvertToRGBA64 returns an *image.RGBA64 instance by asserting the given
// ImageReader has that type or, if it does not, using Copy to concurrently
// set the color.Color values of a new *image.RGBA64 instance with the same
// bounds.
func ConvertToRGBA64(src ImageReader) *image.RGBA64 {
if dst, ok := src.(*image.RGBA64); ok {
return dst
}
dst := image.NewRGBA64(src.Bounds())
Copy(dst, src)
return dst
}
示例13: tToRGBA64
func tToRGBA64(m image.Image) *image.RGBA64 {
if p, ok := m.(*image.RGBA64); ok {
return p
}
p := image.NewRGBA64(m.Bounds())
xdraw.Draw(p, p.Bounds(), m, image.Pt(0, 0), xdraw.Src)
return p
}
示例14: BenchmarkEncodeRGBA64
func BenchmarkEncodeRGBA64(b *testing.B) {
img := image.NewRGBA64(image.Rect(0, 0, 1<<10, 1<<10))
io.ReadFull(rand.Reader, img.Pix)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img)
}
}
示例15: TestSubImage
func TestSubImage(t *testing.T) {
testData := []struct {
desc string
img draw.Image
ok bool
}{
{
"sub image (Gray)",
image.NewGray(image.Rect(0, 0, 10, 10)),
true,
},
{
"sub image (Gray16)",
image.NewGray16(image.Rect(0, 0, 10, 10)),
true,
},
{
"sub image (RGBA)",
image.NewRGBA(image.Rect(0, 0, 10, 10)),
true,
},
{
"sub image (RGBA64)",
image.NewRGBA64(image.Rect(0, 0, 10, 10)),
true,
},
{
"sub image (NRGBA)",
image.NewNRGBA(image.Rect(0, 0, 10, 10)),
true,
},
{
"sub image (NRGBA64)",
image.NewNRGBA64(image.Rect(0, 0, 10, 10)),
true,
},
{
"sub image (fake)",
fakeDrawImage{image.Rect(0, 0, 10, 10)},
false,
},
}
for _, d := range testData {
simg, ok := getSubImage(d.img, image.Pt(3, 3))
if ok != d.ok {
t.Errorf("test [%s] failed: expected %#v, got %#v", d.desc, d.ok, ok)
} else if ok {
simg.Set(5, 5, color.NRGBA{255, 255, 255, 255})
r, g, b, a := d.img.At(5, 5).RGBA()
if r != 0xffff || g != 0xffff || b != 0xffff || a != 0xffff {
t.Errorf("test [%s] failed: expected (0xffff, 0xffff, 0xffff, 0xffff), got (%d, %d, %d, %d)", d.desc, r, g, b, a)
}
}
}
}