github.com/dreamCodeMan/smartcrop@v0.2.0/smartcrop_test.go (about) 1 /* 2 * Copyright (c) 2014-2017 Christian Muehlhaeuser 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in all 12 * copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 * 22 * Authors: 23 * Christian Muehlhaeuser <muesli@gmail.com> 24 * Michael Wendland <michael@michiwend.com> 25 */ 26 27 package smartcrop 28 29 import ( 30 "errors" 31 "fmt" 32 "image" 33 _ "image/jpeg" 34 _ "image/png" 35 "io/ioutil" 36 "os" 37 "strings" 38 "testing" 39 ) 40 41 var ( 42 testFile = "./examples/gopher.jpg" 43 ) 44 45 type SubImager interface { 46 SubImage(r image.Rectangle) image.Image 47 } 48 49 func TestCrop(t *testing.T) { 50 fi, _ := os.Open(testFile) 51 defer fi.Close() 52 53 img, _, err := image.Decode(fi) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 topCrop, err := SmartCrop(img, 250, 250) 59 if err != nil { 60 t.Fatal(err) 61 } 62 expected := image.Rect(464, 24, 719, 279) 63 if topCrop != expected { 64 t.Fatalf("expected %v, got %v", expected, topCrop) 65 } 66 67 sub, ok := img.(SubImager) 68 if ok { 69 cropImage := sub.SubImage(topCrop) 70 // cropImage := sub.SubImage(image.Rect(topCrop.X, topCrop.Y, topCrop.Width+topCrop.X, topCrop.Height+topCrop.Y)) 71 writeImage("jpeg", cropImage, "./smartcrop.jpg") 72 } else { 73 t.Error(errors.New("No SubImage support")) 74 } 75 } 76 77 func BenchmarkCrop(b *testing.B) { 78 fi, err := os.Open(testFile) 79 if err != nil { 80 b.Fatal(err) 81 } 82 defer fi.Close() 83 84 img, _, err := image.Decode(fi) 85 if err != nil { 86 b.Fatal(err) 87 } 88 89 b.ResetTimer() 90 for i := 0; i < b.N; i++ { 91 if _, err := SmartCrop(img, 250, 250); err != nil { 92 b.Error(err) 93 } 94 } 95 } 96 97 func BenchmarkEdge(b *testing.B) { 98 fi, err := os.Open(testFile) 99 if err != nil { 100 b.Fatal(err) 101 } 102 defer fi.Close() 103 104 img, _, err := image.Decode(fi) 105 if err != nil { 106 b.Fatal(err) 107 } 108 109 rgbaImg := toRGBA(img) 110 b.ResetTimer() 111 for i := 0; i < b.N; i++ { 112 o := image.NewRGBA(img.Bounds()) 113 edgeDetect(rgbaImg, o) 114 } 115 } 116 117 func BenchmarkImageDir(b *testing.B) { 118 files, err := ioutil.ReadDir("./examples") 119 if err != nil { 120 b.Fatal(err) 121 } 122 123 b.ResetTimer() 124 for _, file := range files { 125 if strings.Contains(file.Name(), ".jpg") { 126 fi, _ := os.Open("./examples/" + file.Name()) 127 defer fi.Close() 128 129 img, _, err := image.Decode(fi) 130 if err != nil { 131 b.Error(err) 132 continue 133 } 134 135 topCrop, err := SmartCrop(img, 220, 220) 136 if err != nil { 137 b.Error(err) 138 continue 139 } 140 fmt.Printf("Top crop: %+v\n", topCrop) 141 142 sub, ok := img.(SubImager) 143 if ok { 144 cropImage := sub.SubImage(topCrop) 145 // cropImage := sub.SubImage(image.Rect(topCrop.X, topCrop.Y, topCrop.Width+topCrop.X, topCrop.Height+topCrop.Y)) 146 writeImage("jpeg", cropImage, "/tmp/smartcrop/smartcrop-"+file.Name()) 147 } else { 148 b.Error(errors.New("No SubImage support")) 149 } 150 } 151 } 152 // fmt.Println("average time/image:", b.t) 153 }