func BenchmarkProcessorHighQuality(b *testing.B) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		b.Fatal(err)
	}
	prc := NewProcessor(2.2, true)
	params := imageserver.Params{}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		prc.Process(nim, params)
	}
}
Example #2
0
func TestProcessorProcess(t *testing.T) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	for _, highQuality := range []bool{false, true} {
		prc := NewProcessor(2.2, highQuality)
		_, err = prc.Process(nim, imageserver.Params{})
		if err != nil {
			t.Fatal(err)
		}
	}
}
Example #3
0
// BenchmarkEncoder is a helper to benchmark Encoder.
func BenchmarkEncoder(b *testing.B, enc imageserver_image.Encoder, im *imageserver.Image, params imageserver.Params) {
	nim, err := imageserver_image.Decode(im)
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := enc.Encode(ioutil.Discard, nim, params)
		if err != nil {
			b.Fatal(err)
		}
	}
}
func BenchmarkProcessorHighQuality(b *testing.B) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		b.Fatal(err)
	}
	prc := NewProcessor(2.2, true)
	params := imageserver.Params{}
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			prc.Process(nim, params)
		}
	})
}
Example #5
0
// BenchmarkEncoder is a helper to benchmark Encoder.
func BenchmarkEncoder(b *testing.B, enc imageserver_image.Encoder, im *imageserver.Image, params imageserver.Params) {
	nim, err := imageserver_image.Decode(im)
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			err := enc.Encode(ioutil.Discard, nim, params)
			if err != nil {
				b.Fatal(err)
			}
		}
	})
}
func BenchmarkCorrectionProcessor(b *testing.B) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		b.Fatal(err)
	}
	prc := NewCorrectionProcessor(
		imageserver_image.ProcessorFunc(func(nim image.Image, params imageserver.Params) (image.Image, error) {
			return nim, nil
		}),
		true,
	)
	params := imageserver.Params{}
	for i := 0; i < b.N; i++ {
		prc.Process(nim, params)
	}
}
func benchmarkRotateProcessor(b *testing.B, params imageserver.Params) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		b.Fatal(err)
	}
	params = imageserver.Params{
		rotateParam: params,
	}
	prc := &RotateProcessor{}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := prc.Process(nim, params)
		if err != nil {
			b.Fatal(err)
		}
	}
}
func BenchmarkCorrectionProcessor(b *testing.B) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		b.Fatal(err)
	}
	prc := NewCorrectionProcessor(
		imageserver_image.ProcessorFunc(func(nim image.Image, params imageserver.Params) (image.Image, error) {
			return nim, nil
		}),
		true,
	)
	params := imageserver.Params{}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			prc.Process(nim, params)
		}
	})
}
Example #9
0
func benchmark(b *testing.B, im *imageserver.Image, params imageserver.Params) {
	nim, err := imageserver_image.Decode(im)
	if err != nil {
		b.Fatal(err)
	}
	params.Set("width", 100)
	params = imageserver.Params{
		Param: params,
	}
	proc := &Processor{}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := proc.Process(nim, params)
		if err != nil {
			b.Fatal(err)
		}
	}
}
func benchmarkResizeProcessor(b *testing.B, name string, im *imageserver.Image, params imageserver.Params) {
	nim, err := imageserver_image.Decode(im)
	if err != nil {
		b.Fatal(err)
	}
	params.Set("width", 100)
	params = imageserver.Params{
		resizeParam: params,
	}
	prc := &ResizeProcessor{}
	b.Run(name, func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			_, err := prc.Process(nim, params)
			if err != nil {
				b.Fatal(err)
			}
		}
	})
}
func benchmark(b *testing.B, im *imageserver.Image, params imageserver.Params) {
	nim, err := imageserver_image.Decode(im)
	if err != nil {
		b.Fatal(err)
	}
	params.Set("width", 100)
	params = imageserver.Params{
		Param: params,
	}
	proc := &Processor{}
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			_, err := proc.Process(nim, params)
			if err != nil {
				b.Fatal(err)
			}
		}
	})
}
Example #12
0
func TestProcessor(t *testing.T) {
	nim, err := imageserver_image.Decode(imageserver_testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	for _, tc := range []struct {
		name               string
		processor          *Processor
		params             imageserver.Params
		expectedWidth      int
		expectedHeight     int
		expectedParamError string
	}{
		// no size
		{
			name:           "Empty",
			params:         imageserver.Params{},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name:           "ParamEmpty",
			params:         imageserver.Params{param: imageserver.Params{}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name: "SizeZero",
			params: imageserver.Params{param: imageserver.Params{
				"width":  0,
				"height": 0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		// with size
		{
			name: "Width",
			params: imageserver.Params{param: imageserver.Params{
				"width": 100,
			}},
			expectedWidth: 100,
		},
		{
			name: "Height",
			params: imageserver.Params{param: imageserver.Params{
				"height": 100,
			}},
			expectedHeight: 100,
		},
		{
			name: "WidthHeight",
			params: imageserver.Params{param: imageserver.Params{
				"width":  100,
				"height": 100,
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		// mode
		{
			name: "ModeResize",
			params: imageserver.Params{param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "resize",
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		{
			name: "ModeThumbnail",
			params: imageserver.Params{param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "thumbnail",
			}},
			expectedWidth:  100,
			expectedHeight: 79,
		},
		// interpolation
		{
			name: "InterpolationNearestNeighbor",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "nearest_neighbor",
			}},
			expectedWidth: 100,
		},
		{
			name: "InterpolationBilinear",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "bilinear",
			}},
			expectedWidth: 100,
		},
		{
			name: "InterpolationBicubic",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "bicubic",
			}},
			expectedWidth: 100,
		},
		{
			name: "InterpolationMitchelNetravali",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "mitchell_netravali",
			}},
			expectedWidth: 100,
		},
		{
			name: "InterpolationLanczos2",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "lanczos2",
			}},
			expectedWidth: 100,
		},
		{
			name: "InterpolationLanczos3",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "lanczos3",
			}},
			expectedWidth: 100,
		},
		// error
		{
			name:               "ParamInvalid",
			params:             imageserver.Params{param: "invalid"},
			expectedParamError: param,
		},
		{
			name: "WidthInvalidType",
			params: imageserver.Params{param: imageserver.Params{
				"width": "invalid",
			}},
			expectedParamError: param + ".width",
		},
		{
			name: "HeightInvalidType",
			params: imageserver.Params{param: imageserver.Params{
				"height": "invalid",
			}},
			expectedParamError: param + ".height",
		},
		{
			name: "WidthInvalidNegative",
			params: imageserver.Params{param: imageserver.Params{
				"width": -1,
			}},
			expectedParamError: param + ".width",
		},
		{
			name: "HeightInvalidNegative",
			params: imageserver.Params{param: imageserver.Params{
				"height": -1,
			}},
			expectedParamError: param + ".height",
		},
		{
			name:      "WidthInvalidTooLarge",
			processor: &Processor{MaxWidth: 500},
			params: imageserver.Params{param: imageserver.Params{
				"width": 1000,
			}},
			expectedParamError: param + ".width",
		},
		{
			name:      "HeightInvalidTooLarge",
			processor: &Processor{MaxHeight: 500},
			params: imageserver.Params{param: imageserver.Params{
				"height": 1000,
			}},
			expectedParamError: param + ".height",
		},
		{
			name: "InterpolationInvalidType",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": false,
			}},
			expectedParamError: param + ".interpolation",
		},
		{
			name: "InterpolationInvalidUnknown",
			params: imageserver.Params{param: imageserver.Params{
				"width":         100,
				"interpolation": "invalid",
			}},
			expectedParamError: param + ".interpolation",
		},
		{
			name: "ModeInvalidType",
			params: imageserver.Params{param: imageserver.Params{
				"width": 100,
				"mode":  false,
			}},
			expectedParamError: param + ".mode",
		},
		{
			name: "ModeInvalidUnknown",
			params: imageserver.Params{param: imageserver.Params{
				"width": 100,
				"mode":  "invalid",
			}},
			expectedParamError: param + ".mode",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			prc := tc.processor
			if prc == nil {
				prc = &Processor{}
			}
			nim, err := prc.Process(nim, tc.params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && err.Param == tc.expectedParamError {
					return
				}
				t.Fatal(err)
			}
			if tc.expectedParamError != "" {
				t.Fatal("no error")
			}
			if tc.expectedWidth != 0 && nim.Bounds().Dx() != tc.expectedWidth {
				t.Fatalf("unexpected width: got %d, want %d", nim.Bounds().Dx(), tc.expectedWidth)
			}
			if tc.expectedHeight != 0 && nim.Bounds().Dy() != tc.expectedHeight {
				t.Fatalf("unexpected height: got %d, want %d", nim.Bounds().Dy(), tc.expectedHeight)
			}
		})
	}
}
Example #13
0
func TestCorrectionProcessor(t *testing.T) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	simplePrc := imageserver_image.ProcessorFunc(func(nim image.Image, params imageserver.Params) (image.Image, error) {
		return nim, nil
	})
	errPrc := imageserver_image.ProcessorFunc(func(nim image.Image, params imageserver.Params) (image.Image, error) {
		return nil, fmt.Errorf("error")
	})
	for _, tc := range []struct {
		name          string
		processor     imageserver_image.Processor
		enabled       bool
		params        imageserver.Params
		errorExpected bool
	}{
		{
			name:      "DefaultEnabled",
			processor: simplePrc,
			enabled:   true,
		},
		{
			name:      "DefaultDisabled",
			processor: simplePrc,
			enabled:   false,
		},
		{
			name:      "ParamDisabled",
			processor: simplePrc,
			enabled:   true,
			params: imageserver.Params{
				"gamma_correction": false,
			},
		},
		{
			name:      "ParamEnabled",
			processor: simplePrc,
			enabled:   false,
			params: imageserver.Params{
				"gamma_correction": true,
			},
		},
		{
			name:          "Error",
			processor:     errPrc,
			enabled:       true,
			errorExpected: true,
		},
		{
			name:      "Invalid",
			processor: simplePrc,
			enabled:   true,
			params: imageserver.Params{
				"gamma_correction": "invalid",
			},
			errorExpected: true,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			prc := NewCorrectionProcessor(tc.processor, tc.enabled)
			params := tc.params
			if params == nil {
				params = imageserver.Params{}
			}
			_, err = prc.Process(nim, params)
			if tc.errorExpected && err == nil {
				t.Fatal("no error")
			} else if !tc.errorExpected && err != nil {
				t.Fatal(err)
			}
		})
	}
}
Example #14
0
func TestProcess(t *testing.T) {
	nim, err := imageserver_image.Decode(imageserver_testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	type TC struct {
		processor          *Processor
		params             imageserver.Params
		expectedWidth      int
		expectedHeight     int
		expectedParamError string
	}
	for _, tc := range []TC{
		// no size
		{
			params:         imageserver.Params{},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			params:         imageserver.Params{Param: imageserver.Params{}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  0,
				"height": 0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		// with size
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": 100,
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"height": 100,
			}},
			expectedHeight: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		// mode
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "fit",
			}},
			expectedWidth:  100,
			expectedHeight: 80,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "fill",
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": 100,
				"mode":  "fill",
			}},
			expectedWidth:  100,
			expectedHeight: 80,
		},
		// resampling
		{
			processor: &Processor{DefaultResampling: gift.NearestNeighborResampling},
			params: imageserver.Params{Param: imageserver.Params{
				"width": 100,
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": "nearest_neighbor",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": "box",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": "linear",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": "cubic",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": "lanczos",
			}},
			expectedWidth: 100,
		},
		// error
		{
			params:             imageserver.Params{Param: "invalid"},
			expectedParamError: Param,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": "invalid",
			}},
			expectedParamError: Param + ".width",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"height": "invalid",
			}},
			expectedParamError: Param + ".height",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": -1,
			}},
			expectedParamError: Param + ".width",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"height": -1,
			}},
			expectedParamError: Param + ".height",
		},
		{
			processor: &Processor{MaxWidth: 500},
			params: imageserver.Params{Param: imageserver.Params{
				"width": 1000,
			}},
			expectedParamError: Param + ".width",
		},
		{
			processor: &Processor{MaxHeight: 500},
			params: imageserver.Params{Param: imageserver.Params{
				"height": 1000,
			}},
			expectedParamError: Param + ".height",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   666,
			}},
			expectedParamError: Param + ".mode",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "invalid",
			}},
			expectedParamError: Param + ".mode",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": 666,
			}},
			expectedParamError: Param + ".resampling",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":      100,
				"resampling": "invalid",
			}},
			expectedParamError: Param + ".resampling",
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			prc := tc.processor
			if prc == nil {
				prc = &Processor{}
			}
			nim, err := prc.Process(nim, tc.params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && err.Param == tc.expectedParamError {
					return
				}
				t.Fatal(err)
			}
			if tc.expectedParamError != "" {
				t.Fatal("no error")
			}
			if tc.expectedWidth != 0 && nim.Bounds().Dx() != tc.expectedWidth {
				t.Fatalf("unexpected width: got %d, want %d", nim.Bounds().Dx(), tc.expectedWidth)
			}
			if tc.expectedHeight != 0 && nim.Bounds().Dy() != tc.expectedHeight {
				t.Fatalf("unexpected height: got %d, want %d", nim.Bounds().Dy(), tc.expectedHeight)
			}
		}()
	}
}
Example #15
0
func TestCorrectionProcessor(t *testing.T) {
	nim, err := imageserver_image.Decode(testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	simplePrc := imageserver_image.ProcessorFunc(func(nim image.Image, params imageserver.Params) (image.Image, error) {
		return nim, nil
	})
	errPrc := imageserver_image.ProcessorFunc(func(nim image.Image, params imageserver.Params) (image.Image, error) {
		return nil, fmt.Errorf("error")
	})
	type TC struct {
		processor     imageserver_image.Processor
		enabled       bool
		params        imageserver.Params
		errorExpected bool
	}
	for _, tc := range []TC{
		{
			processor: simplePrc,
			enabled:   true,
		},
		{
			processor: simplePrc,
			enabled:   false,
		},
		{
			processor: simplePrc,
			enabled:   true,
			params: imageserver.Params{
				"gamma_correction": false,
			},
		},
		{
			processor: simplePrc,
			enabled:   false,
			params: imageserver.Params{
				"gamma_correction": true,
			},
		},
		{
			processor:     errPrc,
			enabled:       true,
			errorExpected: true,
		},
		{
			processor: simplePrc,
			enabled:   true,
			params: imageserver.Params{
				"gamma_correction": "invalid",
			},
			errorExpected: true,
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			prc := NewCorrectionProcessor(tc.processor, tc.enabled)
			params := tc.params
			if params == nil {
				params = imageserver.Params{}
			}
			_, err = prc.Process(nim, params)
			if tc.errorExpected && err == nil {
				t.Fatal("no error")
			} else if !tc.errorExpected && err != nil {
				t.Fatal(err)
			}
		}()

	}
}
Example #16
0
func TestResizeProcessorProcess(t *testing.T) {
	nim, err := imageserver_image.Decode(imageserver_testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	for _, tc := range []struct {
		name               string
		processor          *ResizeProcessor
		params             imageserver.Params
		expectedWidth      int
		expectedHeight     int
		expectedParamError string
	}{
		// no size
		{
			name:           "Empty",
			params:         imageserver.Params{},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name:           "EmptyParam",
			params:         imageserver.Params{resizeParam: imageserver.Params{}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name: "SizeZero",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":  0,
				"height": 0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		// with size
		{
			name: "Width",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width": 100,
			}},
			expectedWidth: 100,
		},
		{
			name: "Height",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"height": 100,
			}},
			expectedHeight: 100,
		},
		{
			name: "WidthHeight",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":  100,
				"height": 100,
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		// mode
		{
			name: "ModeFit",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "fit",
			}},
			expectedWidth:  100,
			expectedHeight: 80,
		},
		{
			name: "ModeFillWidthHeight",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "fill",
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		{
			name: "ModeFill",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width": 100,
				"mode":  "fill",
			}},
			expectedWidth:  100,
			expectedHeight: 80,
		},
		// resampling
		{
			name:      "ResamplingDefault",
			processor: &ResizeProcessor{DefaultResampling: gift.NearestNeighborResampling},
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width": 100,
			}},
			expectedWidth: 100,
		},
		{
			name: "ResamplingNearestNeighbor",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": "nearest_neighbor",
			}},
			expectedWidth: 100,
		},
		{
			name: "ResamplingBox",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": "box",
			}},
			expectedWidth: 100,
		},
		{
			name: "ResamplingLinear",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": "linear",
			}},
			expectedWidth: 100,
		},
		{
			name: "ResamplingCubic",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": "cubic",
			}},
			expectedWidth: 100,
		},
		{
			name: "ResamplingLanczos",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": "lanczos",
			}},
			expectedWidth: 100,
		},
		// error
		{
			name:               "ParamInvalid",
			params:             imageserver.Params{resizeParam: "invalid"},
			expectedParamError: resizeParam,
		},
		{
			name: "WidthInvalidType",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width": "invalid",
			}},
			expectedParamError: resizeParam + ".width",
		},
		{
			name: "HeightInvalidInvalid",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"height": "invalid",
			}},
			expectedParamError: resizeParam + ".height",
		},
		{
			name: "WidthInvalidNegative",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width": -1,
			}},
			expectedParamError: resizeParam + ".width",
		},
		{
			name: "HeightInvalidNegative",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"height": -1,
			}},
			expectedParamError: resizeParam + ".height",
		},
		{
			name:      "WidthInvalidTooLarge",
			processor: &ResizeProcessor{MaxWidth: 500},
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width": 1000,
			}},
			expectedParamError: resizeParam + ".width",
		},
		{
			name:      "HeightInvalidTooLarge",
			processor: &ResizeProcessor{MaxHeight: 500},
			params: imageserver.Params{resizeParam: imageserver.Params{
				"height": 1000,
			}},
			expectedParamError: resizeParam + ".height",
		},
		{
			name: "ModeInvalidType",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   666,
			}},
			expectedParamError: resizeParam + ".mode",
		},
		{
			name: "ModeInvalidUnknown",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "invalid",
			}},
			expectedParamError: resizeParam + ".mode",
		},
		{
			name: "ResamplingInvalidType",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": 666,
			}},
			expectedParamError: resizeParam + ".resampling",
		},
		{
			name: "ResamplingInvalidUnknown",
			params: imageserver.Params{resizeParam: imageserver.Params{
				"width":      100,
				"resampling": "invalid",
			}},
			expectedParamError: resizeParam + ".resampling",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			prc := tc.processor
			if prc == nil {
				prc = &ResizeProcessor{}
			}
			nim, err := prc.Process(nim, tc.params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && err.Param == tc.expectedParamError {
					return
				}
				t.Fatal(err)
			}
			if tc.expectedParamError != "" {
				t.Fatal("no error")
			}
			if tc.expectedWidth != 0 && nim.Bounds().Dx() != tc.expectedWidth {
				t.Fatalf("unexpected width: got %d, want %d", nim.Bounds().Dx(), tc.expectedWidth)
			}
			if tc.expectedHeight != 0 && nim.Bounds().Dy() != tc.expectedHeight {
				t.Fatalf("unexpected height: got %d, want %d", nim.Bounds().Dy(), tc.expectedHeight)
			}
		})
	}
}
Example #17
0
func TestRotateProcessorProcess(t *testing.T) {
	nim, err := imageserver_image.Decode(imageserver_testdata.Medium)
	if err != nil {
		t.Fatal(err)
	}
	prc := &RotateProcessor{}
	for _, tc := range []struct {
		name               string
		params             imageserver.Params
		expectedWidth      int
		expectedHeight     int
		expectedParamError string
	}{
		// no rotation
		{
			name:           "Empty",
			params:         imageserver.Params{},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name:           "EmptyParam",
			params:         imageserver.Params{rotateParam: imageserver.Params{}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		// with rotation
		{
			name: "Rotation0",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 0.0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name: "Rotation360",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 360.0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name: "Rotation90",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 90.0,
			}},
			expectedWidth:  819,
			expectedHeight: 1024,
		},
		{
			name: "Rotation180",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 180.0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			name: "Rotation270",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 270.0,
			}},
			expectedWidth:  819,
			expectedHeight: 1024,
		},
		{
			name: "Rotation45",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": 45.0,
			}},
			expectedWidth:  1304,
			expectedHeight: 1304,
		},
		// background
		{
			name: "Background",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":   45.0,
				"background": "FF0000",
			}},
		},
		// interpolation
		{
			name: "InterpolationNearestNeighbor",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":      45.0,
				"interpolation": "nearest_neighbor",
			}},
		},
		{
			name: "InterpolationLinear",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":      45.0,
				"interpolation": "linear",
			}},
		},
		{
			name: "InterpolationCubic",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":      45.0,
				"interpolation": "cubic",
			}},
		},
		// error
		{
			name:               "ParamInvalid",
			params:             imageserver.Params{rotateParam: "invalid"},
			expectedParamError: rotateParam,
		},
		{
			name: "RotationInvalid",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation": "invalid",
			}},
			expectedParamError: rotateParam + ".rotation",
		},
		{
			name: "BackgroundInvalidColor",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":   45.0,
				"background": "invalid",
			}},
			expectedParamError: rotateParam + ".background",
		},
		{
			name: "BackgroundInvalidType",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":   45.0,
				"background": 666,
			}},
			expectedParamError: rotateParam + ".background",
		},
		{
			name: "InterpolationInvalidUnknown",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":      45.0,
				"interpolation": "invalid",
			}},
			expectedParamError: rotateParam + ".interpolation",
		},
		{
			name: "InteprolationInvalidType",
			params: imageserver.Params{rotateParam: imageserver.Params{
				"rotation":      45.0,
				"interpolation": 666,
			}},
			expectedParamError: rotateParam + ".interpolation",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			nim, err := prc.Process(nim, tc.params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && err.Param == tc.expectedParamError {
					return
				}
				t.Fatal(err)
			}
			if tc.expectedParamError != "" {
				t.Fatal("no error")
			}
			if tc.expectedWidth != 0 && nim.Bounds().Dx() != tc.expectedWidth {
				t.Fatalf("unexpected width: got %d, want %d", nim.Bounds().Dx(), tc.expectedWidth)
			}
			if tc.expectedHeight != 0 && nim.Bounds().Dy() != tc.expectedHeight {
				t.Fatalf("unexpected height: got %d, want %d", nim.Bounds().Dy(), tc.expectedHeight)
			}
		})
	}
}
Example #18
0
func TestProcessor(t *testing.T) {
	prc := &Processor{}
	type TC struct {
		params             imageserver.Params
		expectedWidth      int
		expectedHeight     int
		expectedParamError string
	}
	for _, tc := range []TC{
		// no size
		{
			params:         imageserver.Params{},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			params:         imageserver.Params{Param: imageserver.Params{}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  0,
				"height": 0,
			}},
			expectedWidth:  1024,
			expectedHeight: 819,
		},
		// with size
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": 100,
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"height": 100,
			}},
			expectedHeight: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		// interpolation
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "nearest_neighbor",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "bilinear",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "bicubic",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "mitchell_netravali",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "lanczos2",
			}},
			expectedWidth: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "lanczos3",
			}},
			expectedWidth: 100,
		},
		// mode
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "resize",
			}},
			expectedWidth:  100,
			expectedHeight: 100,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":  100,
				"height": 100,
				"mode":   "thumbnail",
			}},
			expectedWidth:  100,
			expectedHeight: 79, // 819 * 100 / 1024
		},
		// error
		{
			params:             imageserver.Params{Param: "invalid"},
			expectedParamError: Param,
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": "invalid",
			}},
			expectedParamError: Param + ".width",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"height": "invalid",
			}},
			expectedParamError: Param + ".height",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": -1,
			}},
			expectedParamError: Param + ".width",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"height": -1,
			}},
			expectedParamError: Param + ".height",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": false,
			}},
			expectedParamError: Param + ".interpolation",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width":         100,
				"interpolation": "invalid",
			}},
			expectedParamError: Param + ".interpolation",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": 100,
				"mode":  false,
			}},
			expectedParamError: Param + ".mode",
		},
		{
			params: imageserver.Params{Param: imageserver.Params{
				"width": 100,
				"mode":  "invalid",
			}},
			expectedParamError: Param + ".mode",
		},
	} {
		func() {
			defer func() {
				if t.Failed() {
					t.Logf("%#v", tc)
				}
			}()
			im, err := imageserver_image.Decode(imageserver_testdata.Medium)
			if err != nil {
				t.Fatal(err)
			}
			im, err = prc.Process(im, tc.params)
			if err != nil {
				if err, ok := err.(*imageserver.ParamError); ok && err.Param == tc.expectedParamError {
					return
				}
				t.Fatal(err)
			}
			if tc.expectedWidth != 0 && im.Bounds().Dx() != tc.expectedWidth {
				t.Fatalf("unexpected width: got %d, want %d", im.Bounds().Dx(), tc.expectedWidth)
			}
			if tc.expectedHeight != 0 && im.Bounds().Dy() != tc.expectedHeight {
				t.Fatalf("unexpected height: got %d, want %d", im.Bounds().Dy(), tc.expectedHeight)
			}
		}()
	}
}