示例#1
0
func main() {
	defer common.LogPanic()
	common.Init()
	if flag.NArg() != 2 {
		log.Fatal("Usage: imagediff [--out filename] imagepath1.png imagepath2.png\n")
	}
	a, err := diff.OpenImage(flag.Arg(0))
	if err != nil {
		log.Fatal(err)
	}
	b, err := diff.OpenImage(flag.Arg(1))
	if err != nil {
		log.Fatal(err)
	}
	metrics, d := diff.Diff(a, b)
	fmt.Printf("Dimensions are different: %v\n", metrics.DimDiffer)
	fmt.Printf("Number of pixels different: %v\n", metrics.NumDiffPixels)
	fmt.Printf("Pixel diff percent: %v\n", metrics.PixelDiffPercent)
	if *out == "" {
		return
	} else {
		fmt.Println("Writing image diff.")
	}
	f, err := os.Create(*out)
	if err != nil {
		log.Fatal(err)
	}
	if err := png.Encode(f, d); err != nil {
		log.Fatal(err)
	}
}
// getDigestImage returns the image corresponding to the digest either from
// RAM or disk.
func (fs *FileDiffStore) getDigestImage(d string) (image.Image, error) {
	var err error
	var img image.Image
	if obj, ok := fs.imageCache.Get(d); ok {
		return obj.(image.Image), nil
	}
	// TODO Should be changed to a safe write that writes to a tmp file then renames it.
	img, err = diff.OpenImage(fs.getDigestImagePath(d))
	if err == nil {
		fs.imageCache.Add(d, img)
		return img, nil
	}

	// Mark the image as unavailable since we were not able to decode it.
	fs.unavailableChan <- d

	return nil, fmt.Errorf("Unable to read image for %s: %s", d, err)
}