d9c23a5a3af2 — Sean Russell 12 years ago
Removes a bunch of embedded printing and encodes the comparison results better
4 files changed, 39 insertions(+), 34 deletions(-)

M compareargs.go
M metric.go
M perceptualdiff/main.go
M rgbaimage.go
M compareargs.go +10 -0
@@ 40,8 40,18 @@ type CompareArgs struct {
   ColorFactor float64
   // How much to down sample image before comparing, in powers of 2.
   DownSample  int
+  Result      int
+  Differing   uint
 }
 
+const (
+	BINARY_IDENTICAL = iota
+	DIMENSION_MISMATCH
+	PERCEPTUALLY_INDISTINGUISHABLE
+	VISIBLY_DIFFERENT
+)
+
+
 
 func NewCompareArgs() *CompareArgs {
 	c := new(CompareArgs)

          
M metric.go +9 -24
@@ 22,6 22,7 @@ import (
 	. "math"
 	"fmt"
 	"image/color"
+	"os"
 )
 
 /*

          
@@ 119,9 120,6 @@ func (xyz *XYZ) XYZToLAB(x, y, z float64
 
 func Yee_Compare(args *CompareArgs) bool {
 	for i := 0; i < args.DownSample; i++ {
-		if args.Verbose {
-			fmt.Printf("Downsampling by %d\n", 1 << uint(i+1))
-		}
 		tmp, err := args.ImgA.DownSample()
 		if err == nil {
 			args.ImgA = tmp

          
@@ 135,7 133,7 @@ func Yee_Compare(args *CompareArgs) bool
 	bounds := args.ImgA.Bounds()
 	w, h := bounds.Dx(), bounds.Dy()
 	if ((w != args.ImgB.Bounds().Dx()) || (h != args.ImgB.Bounds().Dy())) {
-		fmt.Printf("Image dimensions do not match\n")
+		args.Result = DIMENSION_MISMATCH
 		return false
 	}
 

          
@@ 152,7 150,7 @@ func Yee_Compare(args *CompareArgs) bool
 		}
 	}
 	if identical {
-		fmt.Printf("Images are binary identical\n")
+		args.Result = BINARY_IDENTICAL
 		return true
 	}
 

          
@@ 171,10 169,6 @@ func Yee_Compare(args *CompareArgs) bool
 	aB := make([]float64, dim)
 	bB := make([]float64, dim)
 
-	if args.Verbose {
-		fmt.Printf("Converting RGB to XYZ\n")
-	}
-
 	var x, y int
 	xyz := NewXYZ()
 	for y = 0; y < h; y++ {

          
@@ 202,20 196,12 @@ func Yee_Compare(args *CompareArgs) bool
 		}
 	}
 
-	if args.Verbose {
-		fmt.Printf("Constructing Laplacian Pyramids\n")
-	}
-
 	la := NewLPyramid(aLum, w, h)
 	lb := NewLPyramid(bLum, w, h)
 
 	num_one_degree_pixels := (2.0 * Tan(args.FieldOfView * 0.5 * Pi / 180.0) * 180.0 / Pi)
 	pixels_per_degree := float64(w) / num_one_degree_pixels
 
-	if (args.Verbose) {
-		fmt.Printf("Performing test\n")
-	}
-
 	num_pixels := 1.0
 	var adaptation_level uint
 	for i := uint(0); i < MAX_PYR_LEVELS; i++ {

          
@@ 306,23 292,22 @@ func Yee_Compare(args *CompareArgs) bool
 	  }
   }
 
-	different := fmt.Sprintf("%d pixels are different", pixels_failed)
+  args.Differing = pixels_failed
 
   // Always output image difference if requested.
   if args.ImgDiff != nil {
-	  if args.ImgDiff.WriteToFile(args.ImgDiff.Name) {
-		  fmt.Printf("Wrote difference image to %s\n", args.ImgDiff.Name)
-	  } else {
-		  fmt.Printf("Could not write difference image to %s\n", args.ImgDiff.Name)
+		err := args.ImgDiff.WriteToFile(args.ImgDiff.Name)
+	  if err != nil {
+		  fmt.Fprintf(os.Stderr, "Could not write difference image to %s, %v\n", args.ImgDiff.Name, err.Error())
 	  }
   }
 
   if pixels_failed < args.ThresholdPixels {
-	  fmt.Printf("Images are perceptually indistinguishable\n%s\n", different)
+	  args.Result = PERCEPTUALLY_INDISTINGUISHABLE
 	  return true
   }
 
-  fmt.Printf("Images are visibly different\n%s\n", different)
+  args.Result = VISIBLY_DIFFERENT
 
   return false
 }

          
M perceptualdiff/main.go +14 -0
@@ 47,6 47,20 @@ func main() {
 
 	passed := perceptualdiff.Yee_Compare(args)
 
+	switch args.Result {
+	default:
+		fmt.Printf("library returned unknown result %d\n", args.Result)
+	case perceptualdiff.BINARY_IDENTICAL:
+		fmt.Println("images are binary identical")
+	case perceptualdiff.DIMENSION_MISMATCH:
+		fmt.Println("image dimensions do not match: %v, %v", args.ImgA.Bounds(), args.ImgB.Bounds())
+	case perceptualdiff.PERCEPTUALLY_INDISTINGUISHABLE:
+		fmt.Printf("images are perceptually indistinguishable; %d pixels differ\n", args.Differing)
+	case perceptualdiff.VISIBLY_DIFFERENT:
+		fmt.Printf("images are visually different; %d pixels differ\n", args.Differing)
+	}
+
+
 	if passed {
 		os.Exit(0)
 	} else {

          
M rgbaimage.go +6 -10
@@ 1,7 1,7 @@ 
 package perceptualdiff
 
 import (
-	"fmt"
+	"errors"
 	"image"
 	"image/color"
 	"image/draw"

          
@@ 87,28 87,24 @@ func (r *RGBAImage) DownSample() (*RGBAI
 	return img, nil
 }
 
-func (r *RGBAImage) WriteToFile(filename string) bool {
+func (r *RGBAImage) WriteToFile(filename string) error {
 	fout, err := os.Create(filename)
 	if err != nil {
-		fmt.Print(err)
-		return false
+		return err
 	}
 	defer fout.Close()
 	switch path.Ext(filename) {
 	default:
-		fmt.Printf("Can't save to unknown filetype %s\n", filename)
-		fmt.Printf("Supported extensions are png, jpg, and jpeg\n")
-		return false
+		return errors.New("unknown filetype "+path.Ext(filename))
 	case "png", "PNG":
 		err = png.Encode(fout, r)
 	case "jpg", "jpeg", "JPG", "JPEG":
 		err = jpeg.Encode(fout, r, &jpeg.Options{100})
 	}
 	if err != nil {
-		fmt.Print(err)
-		return false
+		return err
 	}
-	return true
+	return nil
 }
 
 func ReadFromFile(filename string) (*RGBAImage, error) {