// shrink a frame, keeping its size proportional to the starting image bounds, firstFrame func shrinkFrameToCorrectSize(frame image.Image, w, h int, firstBounds *image.Rectangle) *image.RGBA { frameBounds := frame.Bounds() scaleW := float64(w) / float64(firstBounds.Dx()) scaleH := float64(h) / float64(firstBounds.Dy()) innerW := int(scaleW * float64(frameBounds.Dx())) innerH := int(scaleH * float64(frameBounds.Dy())) x := int(float64(frameBounds.Min.X) * scaleW) y := int(float64(frameBounds.Min.Y) * scaleH) s := resize.Resizer{frame, innerW, innerH, x, y} return s.ResizeNearestNeighbor() }
// for debugging // prints the GIF union, then returns the frame pixmap func testEncode(g *gif.GIF, idx, w, h int, pal []*ascii.TextColor) image.Image { // set up frame accumulator originalBounds := g.Image[0].Bounds() origSizer := resize.Resizer{g.Image[0], w, h, 0, 0} compiledImage := origSizer.ResizeNearestNeighbor() bg := image.NewUniform(color.RGBA{255, 255, 0, 255}) compiledImage = image.NewRGBA(compiledImage.Bounds()) frame := shrinkFrameToCorrectSize(g.Image[idx], w, h, &originalBounds) copyImageOver(compiledImage, bg) copyImageOver(compiledImage, frame) fmt.Fprintln(os.Stderr, ascii.Convert(frame, pal).String()) fmt.Fprintln(os.Stderr, frame.Bounds()) return compiledImage }
func encodeFramesSync(g *gif.GIF, w, h int, pal []*ascii.TextColor) (frames [][]string) { frames = make([][]string, len(g.Image)) // set up frame accumulator originalBounds := g.Image[0].Bounds() origSizer := resize.Resizer{g.Image[0], w, h, 0, 0} compiledImage := origSizer.ResizeNearestNeighbor() // timestamp! ts := time.Now() for i, frame := range g.Image { // resize the current frame partial smallFrame := shrinkFrameToCorrectSize(frame, w, h, &originalBounds) if *verbose { fmt.Fprintf(os.Stdout, "Shrank frame from bounds %s to new smaller bounds %s\n", frame.Bounds(), smallFrame.Bounds()) } // copy the current frame over the previous frame copyImageOver(compiledImage, smallFrame) // convert to ascii textImage := ascii.ConvertSync(compiledImage, pal) // convert to []string and store frames[i] = textImage.StringSlice() // print status info if done if *verbose { fmt.Fprintf(os.Stderr, "Finished encoding frame %d (SYNC)\n", i) } } if *verbose { fmt.Fprintf(os.Stderr, "Rendered %d frames in %v seconds (%d FPS, SYNC)\n", len(g.Image), time.Since(ts), float64(time.Since(ts))/float64(len(g.Image))) } return }