本文整理匯總了Golang中image/gif.GIF類的典型用法代碼示例。如果您正苦於以下問題:Golang GIF類的具體用法?Golang GIF怎麽用?Golang GIF使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了GIF類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: lissajous
// handler that displays lissajous figures in the browser
func lissajous(out io.Writer) {
hit() // Increment 'total hits' metric
l_start := time.Now()
const (
cycles = 5 // number of complete x oscillator revolutions
res = 0.001 // angular resolution
size = 400 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
l_secs := time.Since(l_start).Seconds()
stats.Timing("lissajous.load.time", int64(l_secs)) // Metric to record time to load a figure
stats.Incr("lissajous.pageview.count", 1) // Metric to record hits per cycle for this handler
}
示例2: lissajous
func lissajous(out io.Writer, c float64, col color.RGBA) {
var palette = []color.Color{color.White, col}
const (
res = 0.01 // angular resolution
size = 100 // image canvas covers [-size ... +size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
freq := rand.Float64() * 3
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
cycles := float64(c)
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}
示例3: lissajous
func lissajous(out io.Writer) {
const (
cycles = 5 // number of complete x oscillator revolutions
res = 0.001 // angular resolution
size = 100 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), uint8(i)%3+1)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
示例4: lissajous
func lissajous(out io.Writer) {
const (
cycles = 5
res = 0.001
size = 100
nframes = 64
delay = 8
)
freq := rand.Float64() * 3.0
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
for i := 0; i < nframes; i++ {
colorIndex := uint8(blackIndex)
switch {
case i%9 <= 2:
colorIndex = greenIndex
case i%9 >= 3 && i%9 <= 5:
colorIndex = redIndex
case i%9 >= 6 && i%9 <= 8:
colorIndex = blueIndex
}
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), colorIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}
示例5: lissajous
func lissajous(out io.Writer) {
palette := []color.Color{color.Black}
for i := 0; i < nFrames; i++ {
red := (uint8)((i * 255) / nFrames)
green := (uint8)(((i + nFrames/2) * 255) / nFrames)
blue := (uint8)(((nFrames/2 - i) * 255) / nFrames)
palette = append(palette, color.RGBA{red, green, blue, 0xff})
}
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nFrames}
phase := 0.0
for i := 0; i < nFrames; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
xcoord := size + int(x*(float64)(size)+0.5)
ycoord := size + int(y*(float64)(size)+0.5)
img.SetColorIndex(xcoord, ycoord, (uint8)(i+1))
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}
示例6: Process
// Process implements Processor.
func (prc *SimpleProcessor) Process(g *gif.GIF, params imageserver.Params) (*gif.GIF, error) {
out := new(gif.GIF)
var err error
out.Image, err = prc.processImages(g.Image, params)
if err != nil {
return nil, err
}
out.Delay = make([]int, len(g.Delay))
copy(out.Delay, g.Delay)
out.LoopCount = g.LoopCount
if g.Disposal != nil {
out.Disposal = make([]byte, len(g.Disposal))
copy(out.Disposal, g.Disposal)
}
out.Config.ColorModel = g.Config.ColorModel
for _, p := range out.Image {
if p.Rect.Max.X > out.Config.Width {
out.Config.Width = p.Rect.Max.X
}
if p.Rect.Max.Y > out.Config.Height {
out.Config.Height = p.Rect.Max.Y
}
}
out.BackgroundIndex = g.BackgroundIndex
return out, nil
}
示例7: generateGIF
func generateGIF(filenames []string, outPath string) error {
fmt.Printf("Generating GIF in %s\n", outPath)
uiprogress.Start()
bar := uiprogress.AddBar(len(filenames)).AppendCompleted()
anim := gif.GIF{LoopCount: len(filenames)}
for _, filename := range filenames {
reader, err := os.Open(filename)
if err != nil {
return err
}
defer reader.Close()
img, _, err := image.Decode(reader)
if err != nil {
return err
}
bounds := img.Bounds()
drawer := draw.FloydSteinberg
palettedImg := image.NewPaletted(bounds, palette.Plan9)
drawer.Draw(palettedImg, bounds, img, image.ZP)
anim.Image = append(anim.Image, palettedImg)
anim.Delay = append(anim.Delay, *delay)
bar.Incr()
}
file, err := os.Create(outPath)
defer file.Close()
if err != nil {
return err
}
encodeErr := gif.EncodeAll(file, &anim)
if encodeErr != nil {
return encodeErr
}
return nil
}
示例8: lissajous
func lissajous(out io.Writer, cycles int) {
rand.Seed(time.Now().UTC().UnixNano())
const (
res = 0.001
size = 100
nframes = 64
delay = 8
)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < float64(cycles)*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
uint8(1+rand.Intn(4-1))) // randomly generated color
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
示例9: lissajous
func lissajous(out io.Writer, user_vals map[string]Float) {
const (
res = 0.001 // angular resolution
)
vals := combineUserAndDefaultVals(user_vals)
cycles := vals["cycles"]
size := vals["size"]
nframes := vals["nframes"]
delay := vals["delay"]
fmt.Printf("%d %d %d %d\n", cycles, size, nframes, delay)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
linesIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
示例10: animate
func animate(animdata []byte, frames []*image.Paletted) *gif.GIF {
var g gif.GIF
var loop, clock int
loop:
for pc := 0; ; pc += 2 {
//log.Println("PC", pc)
switch animdata[pc] {
case 0xFF:
break loop
case 0xFE:
loop = int(animdata[pc+1])
case 0xFD:
if loop > 0 {
pc = int(animdata[pc+1]) * 2
pc -= 2
loop--
}
default:
delay := int(animdata[pc+1])
g.Image = append(g.Image, frames[animdata[pc]])
//g.Delay = append(g.Delay, delay*100/60)
g.Delay = append(g.Delay, (clock+delay)*100/60-clock*100/60)
clock += delay
}
}
g.Image = append(g.Image, frames[0])
g.Delay = append(g.Delay, clock*2*100/60-clock)
return &g
}
示例11: lissajous
func lissajous(write http.ResponseWriter, read *http.Request) {
const (
res = 0.001 // angular resolution
size = 100 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
cycles, _ := strconv.Atoi(read.URL.Query()["cycles"][0])
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < float64(cycles)*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(write, &anim) // NOTE: ignoring encoding errors
}
示例12: lissajous
func lissajous(out io.Writer) {
const (
cycles = 5
res = 0.001
size = 100
nframes = 64
delay = 8
)
freq := rand.Float64() * 3.0
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), greenIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
err := gif.EncodeAll(out, &anim)
if err != nil {
panic("Gif encoding error")
}
}
示例13: MakeGif
func (bam *BAM) MakeGif(outputPath string, name string) error {
for idx, seq := range bam.Sequences {
if seq.Start >= 0 && seq.Count > 0 {
pathname := filepath.Join(outputPath, fmt.Sprintf("%s_%03d.gif", name, idx))
g := gif.GIF{}
g.Image = make([]*image.Paletted, seq.Count)
g.Delay = make([]int, seq.Count)
g.LoopCount = 0
for iIdx := seq.Start; iIdx < seq.Start+seq.Count; iIdx++ {
imgIdx := int(bam.SequenceToImage[iIdx])
g.Image[iIdx-seq.Start] = &bam.Image[imgIdx]
g.Delay[iIdx-seq.Start] = 10
}
outFile, err := os.Create(pathname)
if err != nil {
return fmt.Errorf("Unable to create file %s: %v", pathname, err)
}
gif.EncodeAll(outFile, &g)
outFile.Close()
}
}
return nil
}
示例14: lissajous
func lissajous(out http.ResponseWriter, r *http.Request) {
const (
cycles = 5
res = 0.001
size = 100
nframes = 64
delay = 8
)
freq := rand.Float64() * 3.0
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}
示例15: lissajous
func lissajous(out io.Writer) {
const (
cycles = 5 // number of complete x oscillator revolutions
res = 0.001 // angular resolution
size = 100 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
index := uint8(rand.Int() % len(palette)) // random palette index color
if index == 0 {
index = 1 // change the index if the color is the same as the background color
}
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), index)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
if err := gif.EncodeAll(out, &anim); err != nil {
log.Fatal(err)
}
}