cropParams calculates crop rectangle parameters to keep it in image bounds
(m image.Image, opt Options)
| 172 | |
| 173 | // cropParams calculates crop rectangle parameters to keep it in image bounds |
| 174 | func cropParams(m image.Image, opt Options) image.Rectangle { |
| 175 | if !opt.SmartCrop && opt.CropX == 0 && opt.CropY == 0 && opt.CropWidth == 0 && opt.CropHeight == 0 { |
| 176 | return m.Bounds() |
| 177 | } |
| 178 | |
| 179 | // width and height of image |
| 180 | imgW := m.Bounds().Dx() |
| 181 | imgH := m.Bounds().Dy() |
| 182 | |
| 183 | if opt.SmartCrop { |
| 184 | w := evaluateFloat(opt.Width, imgW) |
| 185 | h := evaluateFloat(opt.Height, imgH) |
| 186 | r, err := smartcropAnalyzer.FindBestCrop(m, w, h) |
| 187 | if err != nil { |
| 188 | log.Printf("smartcrop error finding best crop: %v", err) |
| 189 | } else { |
| 190 | return r |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // top left coordinate of crop |
| 195 | x0 := evaluateFloat(math.Abs(opt.CropX), imgW) |
| 196 | if opt.CropX < 0 { |
| 197 | x0 = imgW - x0 // measure from right |
| 198 | } |
| 199 | y0 := evaluateFloat(math.Abs(opt.CropY), imgH) |
| 200 | if opt.CropY < 0 { |
| 201 | y0 = imgH - y0 // measure from bottom |
| 202 | } |
| 203 | |
| 204 | // width and height of crop |
| 205 | w := evaluateFloat(opt.CropWidth, imgW) |
| 206 | if w == 0 { |
| 207 | w = imgW |
| 208 | } |
| 209 | h := evaluateFloat(opt.CropHeight, imgH) |
| 210 | if h == 0 { |
| 211 | h = imgH |
| 212 | } |
| 213 | |
| 214 | // bottom right coordinate of crop |
| 215 | x1 := min(x0+w, imgW) |
| 216 | y1 := min(y0+h, imgH) |
| 217 | |
| 218 | return image.Rect(x0, y0, x1, y1) |
| 219 | } |
| 220 | |
| 221 | // read EXIF orientation tag from r and adjust opt to orient image correctly. |
| 222 | func exifOrientation(r io.Reader) (opt Options) { |