Exemplo n.º 1
0
func gifAnimate(out *os.File, g *gif.GIF, opts *derper.Options) {
	frames := derper.DerpGif(g, opts)

	if *profile != "" {
		return
	}

	derper.Playback(out, g, frames)
}
Exemplo n.º 2
0
func main() {
	flag.Parse()

	// FIRST - engage profiling if required
	if *profile != "" {
		proFILE, err := os.Create(*profile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(proFILE)
		defer pprof.StopCPUProfile()
	}

	// SECOND - set derper verbosity
	derper.SetVerbose(*verbose)

	// parse and handle arguments
	var in, out *os.File
	//var out_name string
	//args := flag.Args()
	//if len(args) >= 2 {
	//out_name = args[1]
	//}

	var opts derper.Options = derper.Options{
		TargetWidth:  *targetWidth,
		TargetHeight: *targetHeight,
		PixelRatio:   *pixelRatio,

		CharSet:     *charSet,
		CharSetName: *charSetName,
		Invert:      *invertCharSet,
	}

	// open infile
	in, err := getInputFile()
	if err != nil {
		log.Fatal(err)
	}
	defer in.Close()

	// decode image
	img, ft, err := image.Decode(in)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}

	// verbose -- print image information
	if *verbose {
		paletted, ok := img.(*image.Paletted)
		if ok {
			showPaletteInfo(out, paletted.Palette)
		}
	}

	var result [][]string

	// handle gifs since they need to be decoded differently
	if ft == "gif" {
		in.Seek(0, 0)
		gifs, err := gif.DecodeAll(in)

		if err != nil {
			fmt.Fprintf(os.Stderr, "Animated gif decoding error: %s\n", err)
			return
		}

		// display the gif as an amination instead of writing it to the output file
		if *animate {
			gifAnimate(os.Stdout, gifs, &opts)
			return
		}

		result = derper.DerpGif(gifs, &opts)
	} else {
		result = derper.Derp(img, &opts)
	}

	writeOutput(result)

	// TODO: note the operation
	// if *verbose {
	// 	fmt.Fprintf(os.Stderr, "Resizing image of type %s to [%d, %d] ", ft, opts.targetWidth, opts.targetHeight)
	// 	if len(out_name) > 0 {
	// 		fmt.Fprintf(os.Stderr, "at '%s'\n", out_name)
	// 	} else {
	// 		fmt.Fprintf(os.Stderr, "\n")
	// 	}
	// }
}