示例#1
0
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
}
示例#2
0
// 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
}
示例#3
0
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)
}
示例#4
0
// Generate writes to outFile an animated GIF
func (g *GifGenerator) Generate(input string, output string) (err error) {
	// virtual terminal
	var state = terminal.State{}
	vt, err := terminal.Create(&state, ioutil.NopCloser(bytes.NewBuffer([]byte{})))
	if err != nil {
		return
	}
	defer vt.Close()
	vt.Resize(g.Col, g.Row)

	// play and capture
	var (
		images []*image.Paletted
		delays []int
	)
	err = g.TtyPlay(input, vt, func(diff int32) (err error) {
		delay := int(float64(diff)/g.Speed) / 10000
		if delay > 0 {
			var img *image.Paletted
			img, err = g.Capture(&state)
			if err != nil {
				return
			}
			images = append(images, img)
			delays = append(delays, delay)
		}
		return nil
	})
	if err != nil {
		return
	}

	// generate gif file
	outFile, err := os.Create(output)
	if err != nil {
		return
	}
	defer outFile.Close()
	opts := gif.GIF{
		Image: images,
		Delay: delays,
	}
	if g.NoLoop {
		opts.LoopCount = 1
	}
	err = gif.EncodeAll(outFile, &opts)
	if err != nil {
		return
	}
	return nil
}
示例#5
0
文件: encode.go 项目: brydavis/maxgif
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")
}
示例#6
0
文件: encode.go 项目: brydavis/gum
func main() {

	out, err := os.Create("./output.gif")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	dir, _ := ioutil.ReadDir(".")

	var imgs []*image.Paletted

	for _, file := range dir {
		switch ext := path.Ext(file.Name()); ext {
		case ".jpg":

			f, _ := os.Open(file.Name())
			img, _ := jpeg.Decode(f)
			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])

		default:
			fmt.Println(ext)
		}
	}

	g := new(gif.GIF)

	g.Image = imgs

	var opt gif.Options
	opt.NumColors = 256
	g.Delay = []int{100, 100, 100}
	g.LoopCount = 1000

	err = gif.EncodeAll(out, g)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("Generated image to output.gif \n")
}
示例#7
0
文件: writer.go 项目: qmdx/gogif
// EncodeAll writes the images in g to w in GIF format with the
// given loop count and delay between frames.
func EncodeAll(w io.Writer, g *gif.GIF) error {
	if len(g.Image) == 0 {
		return errors.New("gif: must provide at least one image")
	}

	if len(g.Image) != len(g.Delay) {
		return errors.New("gif: mismatched image and delay lengths")
	}
	if g.LoopCount < 0 {
		g.LoopCount = 0
	}

	e := newEncoder(w)
	e.g = g
	e.writeHeader()
	for i, pm := range g.Image {
		e.writeImageBlock(pm, g.Delay[i])
	}
	e.writeByte(sTrailer)
	e.flush()
	return e.err
}
示例#8
0
// EncodeAll encodes a gif to an io.Writer.
func EncodeAll(w io.Writer, g *gif.GIF) (err error) {
	if len(g.Image) == 0 {
		return errors.New("Can't encode zero images.")
	}

	if len(g.Image) != len(g.Delay) {
		return errors.New(fmt.Sprintf("Number of images and delays must be equal (%s x %s)", len(g.Image), len(g.Delay)))
	}

	if g.LoopCount < 0 {
		g.LoopCount = 0
	}

	var e encoder
	e.w = w
	e.g = g

	err = e.writeHeader()
	if err != nil {
		return
	}

	for i, _ := range e.g.Image {
		err = e.writeFrame(i)
		if err != nil {
			return
		}
	}

	_, err = w.Write([]byte{';'})
	if err != nil {
		return
	}

	return nil
}
示例#9
0
// Generate writes to outFile an animated GIF
func (g *GifGenerator) Generate(inFile string, outFile string) (err error) {
	tempDir, err := ioutil.TempDir("", "ttygif")
	if err != nil {
		return
	}
	defer os.RemoveAll(tempDir)

	var (
		delays    []int
		timestamp ttyread.TimeVal
	)
	worker := NewWorker()
	// play and capture
	err = Play(inFile, func(diff ttyread.TimeVal) (err error) {
		delay := int(float64(diff.Sec*1000000+diff.Usec)/g.Speed) / 10000
		if delay == 0 {
			return nil
		}
		delays = append(delays, delay)
		timestamp = timestamp.Add(diff)

		// capture and append to images
		imgPath := filepath.Join(tempDir, fmt.Sprintf("%03d_%06d", timestamp.Sec, timestamp.Usec))
		fileType, err := CaptureImage(imgPath)
		if err != nil {
			return
		}
		worker.AddTargetFile(imgPath, fileType)
		return nil
	})
	if err != nil {
		return
	}
	// get paletted images from capture files
	progress := make(chan struct{})
	go func() {
	Loop:
		for {
			select {
			case <-time.Tick(time.Millisecond * 500):
				print(".")
			case <-progress:
				break Loop
			}
		}
		print("\r")
	}()
	images, err := worker.GetAllImages()
	if err != nil {
		return
	}
	close(progress)

	// generate GIF file
	file, err := os.Create(outFile)
	if err != nil {
		return
	}
	defer file.Close()
	opts := gif.GIF{
		Image: images,
		Delay: delays,
	}
	if g.NoLoop {
		opts.LoopCount = 1
	}
	err = gif.EncodeAll(file, &opts)
	if err != nil {
		return
	}
	return nil
}