Ejemplo n.º 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)
	}
}
Ejemplo n.º 2
0
// Calculate the DiffMetrics for the provided digests.
func (fs *FileDiffStore) diff(d1, d2 string) (*diff.DiffMetrics, error) {
	img1, err := fs.getDigestImage(d1)
	if err != nil {
		return nil, err
	}

	img2, err := fs.getDigestImage(d2)
	if err != nil {
		return nil, err
	}
	dm, resultImg := diff.Diff(img1, img2)

	baseName := getDiffBasename(d1, d2)

	// Write the diff image to a temporary file.
	tempOut, err := ioutil.TempFile(fs.localTempFileDir, fmt.Sprintf("tempfile-%s", baseName))
	if err != nil {
		return nil, fmt.Errorf("Unable to create temp file: %s", err)
	}

	encoder := png.Encoder{CompressionLevel: png.BestSpeed}
	if err := encoder.Encode(tempOut, resultImg); err != nil {
		return nil, err
	}

	err = tempOut.Close()
	if err != nil {
		return nil, fmt.Errorf("Error closing temp file: %s", err)
	}

	diffImageFilename := fmt.Sprintf("%s.%s", baseName, IMG_EXTENSION)
	outputFileName, err := fs.createRadixPath(fs.localDiffDir, diffImageFilename)
	if err != nil {
		return nil, err
	}

	fs.diffDirLock.Lock()
	defer fs.diffDirLock.Unlock()
	if err := os.Rename(tempOut.Name(), outputFileName); err != nil {
		return nil, fmt.Errorf("Unable to move file: %s", err)
	}

	// This sets a logical path for this file.
	dm.PixelDiffFilePath = diffImageFilename
	return dm, nil
}