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 }
func main() { n := 150 steps := 10000 im := gif.GIF{LoopCount: 5, Config: image.Config{ColorModel: conway.BoardPalette, Width: n, Height: n}} game := conway.NewGame(n, n*n/2) var counts []int im.Image = append(im.Image, game.ToImage()) im.Delay = append(im.Delay, 20) fmt.Printf("step = %d Count = %d\n", 0, game.Count) for i := 0; i < steps; i++ { game.TakeTurn() counts = append(counts, game.Count) fmt.Printf("step = %d Count = %d\n", i+1, game.Count) im.Image = append(im.Image, game.ToImage()) im.Delay = append(im.Delay, 20) if i > 4 { countset := make(map[int]bool) for _, val := range counts[i-5:] { countset[val] = true } if len(countset) == 1 { break } } } f, err := os.Create("/home/pyoung/conway.gif") if err != nil { log.Fatal(err) } defer f.Close() err = gif.EncodeAll(f, &im) if err != nil { log.Fatal(err) } // for i := 0; i < 20; i++ { // fmt.Println(game) // game.TakeTurn() // } // fmt.Println(game) }
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 }
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) }
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 }
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") } }
// 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 }
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) } }
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 }
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 }
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 }
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 }
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) }
func lissajous(out io.Writer, cycles int) { if cycles == 0 { cycles = 5 } const ( 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) color := rand.Intn(216) 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(color)) } phase += 0.1 anim.Delay = append(anim.Delay, delay) anim.Image = append(anim.Image, img) } gif.EncodeAll(out, &anim) }
func lissajous(out io.Writer, config map[string]int) { cycles := 5 // number of complete x oscillator revolutions res := 0.001 // angular resolution size := 100 // image canvas covers [-size..+size] nframes := 63 // number of animation frames delay := 8 // delay between frames in 10ms units if v, ok := config["cycles"]; ok { cycles = v } if v, ok := config["size"]; ok { size = v } 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*float64(size)+0.5), size+int(y*float64(size)+0.5), greenIndex) } phase += 0.1 anim.Delay = append(anim.Delay, delay) anim.Image = append(anim.Image, img) } gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors }
// Writter writes a Gif // @param out a Writter // @param cycles number of complete x oscillator revolutions func Writter(out io.Writer, cycles float64) { 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 ) freq := rand.Float64() * 12.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, pallete) for t := 0.0; t < cycles*2*math.Pi; t += res { x := math.Sin(t) y := math.Sin(t*freq + phase) if i%2 == 0 { img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), greenIndex) } else { img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blueIndex) } } phase += 0.1 anim.Delay = append(anim.Delay, delay) anim.Image = append(anim.Image, img) } gif.EncodeAll(out, &anim) }
func composite(input *gif.GIF, lgtm image.Image) (*gif.GIF, error) { output := gif.GIF{ Delay: input.Delay, LoopCount: input.LoopCount, Disposal: input.Disposal, Config: image.Config{ Width: input.Config.Width, Height: input.Config.Height, }, } x := input.Config.Width/2 - lgtm.Bounds().Dx()/2 y := input.Config.Height/2 - lgtm.Bounds().Dy()/2 fmt.Print("compositting frame...") for i, frame := range input.Image { fmt.Printf("%d ", i+1) draw.Draw(frame, frame.Bounds(), lgtm, image.Point{-x, -y}, draw.Over) output.Image = append(output.Image, frame) } fmt.Println("done") return &output, nil }
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) }
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 // gif.GIF is a struct type. Somehow this allows the struct to be initialized with nframes, // the rest are 0 due to some defaulting. 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) // BobK: I have no idea what int(x*size+0.5) is doing 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 }
func lissajous(out io.Writer) { const ( cycles = 5 res = 0.001 size = 100 nframes = 128 delay = 24 ) genGradientPalette(nframes) 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), uint8(i+1)) } phase += 0.1 anim.Delay = append(anim.Delay, delay) anim.Image = append(anim.Image, img) } gif.EncodeAll(out, &anim) }
func lissajous(out http.ResponseWriter, r *http.Request) { const ( res = 0.001 // angular resolution size = 500 // image canvas covers [-size..+size] nframes = 64 // number of animation frames delay = 8 // delay between frames in 10ms units ) if err := r.ParseForm(); err != nil { os.Exit(1) } for k, v := range r.Form { fmt.Printf("%s:%s\n", k, v) } cyclesInt, _ := strconv.Atoi(r.Form.Get("cycles")) // number of complete x oscillator revolutions cycles := float64(cyclesInt) fmt.Printf("Float is: %f\n", cycles) 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 }
func lissajous(out io.Writer) { const ( cycles = 5 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 < 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) } gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors }
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) }
func lissajous(out io.Writer, cycles int) { palette := []color.Color{color.White, color.Black} const ( res = 0.001 size = 100 nframes = 64 delay = 8 whiteIndex = 0 blackIndex = 1 ) 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) 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) }
func SaveGIF(path string, m, w, h int, points []Point) error { g := gif.GIF{} g.Image = append(g.Image, CreateFrame(m, w, h, nil)) g.Delay = append(g.Delay, 10) for i := range points { g.Image = append(g.Image, CreateFrame(m, w, h, points[:i+1])) g.Delay = append(g.Delay, 10) } g.Image = append(g.Image, CreateFrame(m, w, h, points)) g.Delay = append(g.Delay, 100) file, err := os.Create(path) if err != nil { return err } defer file.Close() return gif.EncodeAll(file, &g) }
func (gs *GIFSpark) MakeGif() error { g := new(gif.GIF) for l := 0; l <= 100; l++ { frame := gs.makeFrame(l) g.Image = append(g.Image, frame) g.Delay = append(g.Delay, 0) } return }
func makeGif(c appengine.Context, m *movie) ([]byte, error) { palette := m.palette() delay := int(100 / m.Speed) anim := gif.GIF{LoopCount: 0} for _, pix := range m.Frames { bounds := image.Rect(0, 0, m.Width*pixelsize, m.Height*pixelsize) scaled := make([]byte, bounds.Max.X*bounds.Max.Y) idx := 0 for y := 0; y < m.Height; y++ { for i := 0; i < pixelsize; i++ { for x := 0; x < m.Width; x++ { pixel := pix[x+y*m.Width] - startColorChar for j := 0; j < pixelsize; j++ { scaled[idx] = pixel idx++ } } } } img := image.Paletted{ Pix: scaled, Stride: m.Width * pixelsize, Rect: bounds, Palette: palette, } anim.Image = append(anim.Image, &img) anim.Delay = append(anim.Delay, delay) } var buf bytes.Buffer if err := gifencoder.EncodeAll(&buf, anim); err != nil { c.Errorf("can't encode gif: %v", err) return nil, err } if appengine.IsDevAppServer() { // Validate gif by decoding it r := bytes.NewReader(buf.Bytes()) size := r.Len() if _, err := gif.DecodeAll(r); err != nil { remaining := r.Len() offset := size - remaining c.Errorf("created an invalid gif: %v", err) c.Errorf("error at offset %v (%x)", offset, offset) } } return buf.Bytes(), nil }
func main() { args := os.Args[1:] if len(args) > 2 || len(args) == 0 { showUsage() return } input := args[0] fmt.Println("Reading file", input) file, err := os.Open(input) if err != nil { fmt.Println(err) return } img, err := gif.DecodeAll(file) if err != nil { fmt.Println(err) return } reversed := new(gif.GIF) reversed.Delay = img.Delay reversed.LoopCount = img.LoopCount reversed.Image = reverse(img.Image) fmt.Println("Reversing..") var output string if len(args) == 2 { output = strings.TrimSuffix(args[1], filepath.Ext(input)) + ".gif" } else { output = strings.TrimSuffix(input, filepath.Ext(input)) + "_reversed.gif" } result, err := os.Create(output) if err != nil { fmt.Println(err) return } gif.EncodeAll(result, reversed) fmt.Println("✓ Saved", output) }
func lissajous(out io.Writer, arr map[string]int) { var ( cycles = 5 res = 0.001 size = 200 nframes = 64 delay = 8 ) if arr["cycles"] > 0 { cycles = arr["cycles"] } if arr["res"] > 0 { res = float64(arr["res"]) } if arr["size"] > 0 { size = arr["size"] } if arr["nframes"] > 0 { nframes = arr["nframes"] } if arr["delay"] > 0 { delay = arr["delay"] } 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 < float64(cycles)*2*math.Pi; t += res { x := math.Sin(t) y := math.Sin(t*freq + phase) img.SetColorIndex(size+int(x*float64(size)+0.5), size+int(y*float64(size)+0.5), colorIndex) } phase += 0.1 anim.Delay = append(anim.Delay, delay) anim.Image = append(anim.Image, img) } gif.EncodeAll(out, &anim) }
func main() { dir, _ := ioutil.ReadDir(".") var imgs []*image.Paletted for _, file := range dir { f, _ := os.Open(file.Name()) defer f.Close() if ext := path.Ext(file.Name()); ext != ".jpg" && ext != ".JPG" && ext != ".jpeg" && ext != ".png" { fmt.Printf("File not an image: %s\n", file.Name()) } else { img, err := rs.ResizePixels(f, 400, 400) if err != nil { fmt.Println(err) } var opt gif.Options opt.NumColors = 256 buf := new(bytes.Buffer) gif.Encode(buf, img, &opt) g, _ := gif.DecodeAll(buf) imgs = append(imgs, g.Image[0]) } } g := new(gif.GIF) g.Image = imgs g.Delay = evenDelay(100, len(g.Image)) g.LoopCount = 1000 var opt gif.Options opt.NumColors = 256 out, err := os.Create("./output.gif") if err != nil { fmt.Println(err) os.Exit(1) } err = gif.EncodeAll(out, g) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println("Generated image to output.gif \n") }