mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-30 22:34:18 -05:00
vendor: add github.com/muesli/smartcrop and deps
This commit is contained in:
parent
ba2da75102
commit
20c0a50a31
20 changed files with 11444 additions and 0 deletions
21
vendor/github.com/muesli/smartcrop/LICENSE
generated
vendored
Normal file
21
vendor/github.com/muesli/smartcrop/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Christian Muehlhaeuser
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
69
vendor/github.com/muesli/smartcrop/README.md
generated
vendored
Normal file
69
vendor/github.com/muesli/smartcrop/README.md
generated
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
smartcrop
|
||||
=========
|
||||
|
||||
smartcrop finds good image crops for arbitrary sizes. It is a pure Go implementation, based on Jonas Wagner's [smartcrop.js](https://github.com/jwagner/smartcrop.js)
|
||||
|
||||
![Example](./examples/gopher.jpg)
|
||||
Image: [https://www.flickr.com/photos/usfwspacific/8182486789](https://www.flickr.com/photos/usfwspacific/8182486789) CC BY U.S. Fish & Wildlife
|
||||
|
||||
![Example](./examples/goodtimes.jpg)
|
||||
Image: [https://www.flickr.com/photos/endogamia/5682480447](https://www.flickr.com/photos/endogamia/5682480447) by N. Feans
|
||||
|
||||
## Installation
|
||||
|
||||
Make sure you have a working Go environment. See the [install instructions](http://golang.org/doc/install.html).
|
||||
|
||||
To install smartcrop, simply run:
|
||||
|
||||
go get github.com/muesli/smartcrop
|
||||
|
||||
To compile it from source:
|
||||
|
||||
cd $GOPATH/src/github.com/muesli/smartcrop
|
||||
go get -u -v
|
||||
go build && go test -v
|
||||
|
||||
## Example
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"os"
|
||||
|
||||
"github.com/muesli/smartcrop"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, _ := os.Open("image.png")
|
||||
img, _, _ := image.Decode(f)
|
||||
|
||||
analyzer := smartcrop.NewAnalyzer()
|
||||
topCrop, _ := analyzer.FindBestCrop(img, 250, 250)
|
||||
|
||||
// The crop will have the requested aspect ratio, but you need to copy/scale it yourself
|
||||
fmt.Printf("Top crop: %+v\n", topCrop)
|
||||
|
||||
type SubImager interface {
|
||||
SubImage(r image.Rectangle) image.Image
|
||||
}
|
||||
croppedimg := img.(SubImager).SubImage(topCrop)
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Also see the test cases in smartcrop_test.go for further working examples.
|
||||
|
||||
## Sample Data
|
||||
You can find a bunch of test images for the algorithm [here](https://github.com/muesli/smartcrop-samples).
|
||||
|
||||
## Development
|
||||
API docs can be found [here](http://godoc.org/github.com/muesli/smartcrop).
|
||||
|
||||
Join us on IRC: irc.freenode.net/#smartcrop
|
||||
|
||||
[![Build Status](https://travis-ci.org/muesli/smartcrop.svg?branch=master)](https://travis-ci.org/muesli/smartcrop)
|
||||
[![Coverage Status](https://coveralls.io/repos/github/muesli/smartcrop/badge.svg?branch=master)](https://coveralls.io/github/muesli/smartcrop?branch=master)
|
||||
[![Go ReportCard](http://goreportcard.com/badge/muesli/smartcrop)](http://goreportcard.com/report/muesli/smartcrop)
|
107
vendor/github.com/muesli/smartcrop/debug.go
generated
vendored
Normal file
107
vendor/github.com/muesli/smartcrop/debug.go
generated
vendored
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Copyright (c) 2014 Christian Muehlhaeuser
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* Authors:
|
||||
* Christian Muehlhaeuser <muesli@gmail.com>
|
||||
* Michael Wendland <michael@michiwend.com>
|
||||
*/
|
||||
|
||||
/*
|
||||
Package smartcrop implements a content aware image cropping library based on
|
||||
Jonas Wagner's smartcrop.js https://github.com/jwagner/smartcrop.js
|
||||
*/
|
||||
package smartcrop
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func debugOutput(debug bool, img *image.RGBA, debugType string) {
|
||||
if debug {
|
||||
writeImage("png", img, "./smartcrop_"+debugType+".png")
|
||||
}
|
||||
}
|
||||
|
||||
func writeImage(imgtype string, img image.Image, name string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(name), 0755); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
switch imgtype {
|
||||
case "png":
|
||||
return writeImageToPng(img, name)
|
||||
case "jpeg":
|
||||
return writeImageToJpeg(img, name)
|
||||
}
|
||||
|
||||
return errors.New("Unknown image type")
|
||||
}
|
||||
|
||||
func writeImageToJpeg(img image.Image, name string) error {
|
||||
fso, err := os.Create(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fso.Close()
|
||||
|
||||
return jpeg.Encode(fso, img, &jpeg.Options{Quality: 100})
|
||||
}
|
||||
|
||||
func writeImageToPng(img image.Image, name string) error {
|
||||
fso, err := os.Create(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fso.Close()
|
||||
|
||||
return png.Encode(fso, img)
|
||||
}
|
||||
|
||||
func drawDebugCrop(topCrop Crop, o *image.RGBA) {
|
||||
width := o.Bounds().Dx()
|
||||
height := o.Bounds().Dy()
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
r, g, b, _ := o.At(x, y).RGBA()
|
||||
r8 := float64(r >> 8)
|
||||
g8 := float64(g >> 8)
|
||||
b8 := uint8(b >> 8)
|
||||
|
||||
imp := importance(topCrop, x, y)
|
||||
|
||||
if imp > 0 {
|
||||
g8 += imp * 32
|
||||
} else if imp < 0 {
|
||||
r8 += imp * -64
|
||||
}
|
||||
|
||||
nc := color.RGBA{uint8(bounds(r8)), uint8(bounds(g8)), b8, 255}
|
||||
o.SetRGBA(x, y, nc)
|
||||
}
|
||||
}
|
||||
}
|
482
vendor/github.com/muesli/smartcrop/smartcrop.go
generated
vendored
Normal file
482
vendor/github.com/muesli/smartcrop/smartcrop.go
generated
vendored
Normal file
|
@ -0,0 +1,482 @@
|
|||
/*
|
||||
* Copyright (c) 2014-2017 Christian Muehlhaeuser
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* Authors:
|
||||
* Christian Muehlhaeuser <muesli@gmail.com>
|
||||
* Michael Wendland <michael@michiwend.com>
|
||||
*/
|
||||
|
||||
/*
|
||||
Package smartcrop implements a content aware image cropping library based on
|
||||
Jonas Wagner's smartcrop.js https://github.com/jwagner/smartcrop.js
|
||||
*/
|
||||
package smartcrop
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"golang.org/x/image/draw"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidDimensions gets returned when the supplied dimensions are invalid
|
||||
ErrInvalidDimensions = errors.New("Expect either a height or width")
|
||||
|
||||
skinColor = [3]float64{0.78, 0.57, 0.44}
|
||||
)
|
||||
|
||||
const (
|
||||
detailWeight = 0.2
|
||||
skinBias = 0.01
|
||||
skinBrightnessMin = 0.2
|
||||
skinBrightnessMax = 1.0
|
||||
skinThreshold = 0.8
|
||||
skinWeight = 1.8
|
||||
saturationBrightnessMin = 0.05
|
||||
saturationBrightnessMax = 0.9
|
||||
saturationThreshold = 0.4
|
||||
saturationBias = 0.2
|
||||
saturationWeight = 0.3
|
||||
scoreDownSample = 8 // step * minscale rounded down to the next power of two should be good
|
||||
step = 8
|
||||
scaleStep = 0.1
|
||||
minScale = 0.9
|
||||
maxScale = 1.0
|
||||
edgeRadius = 0.4
|
||||
edgeWeight = -20.0
|
||||
outsideImportance = -0.5
|
||||
ruleOfThirds = true
|
||||
prescale = true
|
||||
prescaleMin = 400.00
|
||||
)
|
||||
|
||||
// Analyzer interface analyzes its struct and returns the best possible crop with the given
|
||||
// width and height returns an error if invalid
|
||||
type Analyzer interface {
|
||||
FindBestCrop(img image.Image, width, height int) (image.Rectangle, error)
|
||||
}
|
||||
|
||||
// Score contains values that classify matches
|
||||
type Score struct {
|
||||
Detail float64
|
||||
Saturation float64
|
||||
Skin float64
|
||||
}
|
||||
|
||||
// Crop contains results
|
||||
type Crop struct {
|
||||
image.Rectangle
|
||||
Score Score
|
||||
}
|
||||
|
||||
// CropSettings contains options to change cropping behaviour
|
||||
type CropSettings struct {
|
||||
InterpolationType resize.InterpolationFunction
|
||||
DebugMode bool
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
type smartcropAnalyzer struct {
|
||||
cropSettings CropSettings
|
||||
}
|
||||
|
||||
// NewAnalyzer returns a new analyzer with default settings
|
||||
func NewAnalyzer() Analyzer {
|
||||
cropSettings := CropSettings{
|
||||
InterpolationType: resize.Bicubic,
|
||||
DebugMode: false,
|
||||
}
|
||||
|
||||
return NewAnalyzerWithCropSettings(cropSettings)
|
||||
}
|
||||
|
||||
// NewAnalyzerWithCropSettings returns a new analyzer with the given settings
|
||||
func NewAnalyzerWithCropSettings(cropSettings CropSettings) Analyzer {
|
||||
if cropSettings.Log == nil {
|
||||
cropSettings.Log = log.New(ioutil.Discard, "", 0)
|
||||
}
|
||||
return &smartcropAnalyzer{cropSettings: cropSettings}
|
||||
}
|
||||
|
||||
func (o smartcropAnalyzer) FindBestCrop(img image.Image, width, height int) (image.Rectangle, error) {
|
||||
if width == 0 && height == 0 {
|
||||
return image.Rectangle{}, ErrInvalidDimensions
|
||||
}
|
||||
|
||||
// resize image for faster processing
|
||||
scale := math.Min(float64(img.Bounds().Dx())/float64(width), float64(img.Bounds().Dy())/float64(height))
|
||||
var lowimg *image.RGBA
|
||||
var prescalefactor = 1.0
|
||||
|
||||
if prescale {
|
||||
// if f := 1.0 / scale / minScale; f < 1.0 {
|
||||
// prescalefactor = f
|
||||
// }
|
||||
if f := prescaleMin / math.Min(float64(img.Bounds().Dx()), float64(img.Bounds().Dy())); f < 1.0 {
|
||||
prescalefactor = f
|
||||
}
|
||||
o.cropSettings.Log.Println(prescalefactor)
|
||||
|
||||
smallimg := resize.Resize(
|
||||
uint(float64(img.Bounds().Dx())*prescalefactor),
|
||||
0,
|
||||
img,
|
||||
o.cropSettings.InterpolationType)
|
||||
lowimg = toRGBA(smallimg)
|
||||
} else {
|
||||
lowimg = toRGBA(img)
|
||||
}
|
||||
|
||||
if o.cropSettings.DebugMode {
|
||||
writeImage("png", lowimg, "./smartcrop_prescale.png")
|
||||
}
|
||||
|
||||
cropWidth, cropHeight := chop(float64(width)*scale*prescalefactor), chop(float64(height)*scale*prescalefactor)
|
||||
realMinScale := math.Min(maxScale, math.Max(1.0/scale, minScale))
|
||||
|
||||
o.cropSettings.Log.Printf("original resolution: %dx%d\n", img.Bounds().Dx(), img.Bounds().Dy())
|
||||
o.cropSettings.Log.Printf("scale: %f, cropw: %f, croph: %f, minscale: %f\n", scale, cropWidth, cropHeight, realMinScale)
|
||||
|
||||
topCrop, err := analyse(o.cropSettings, lowimg, cropWidth, cropHeight, realMinScale)
|
||||
if err != nil {
|
||||
return topCrop, err
|
||||
}
|
||||
|
||||
if prescale == true {
|
||||
topCrop.Min.X = int(chop(float64(topCrop.Min.X) / prescalefactor))
|
||||
topCrop.Min.Y = int(chop(float64(topCrop.Min.Y) / prescalefactor))
|
||||
topCrop.Max.X = int(chop(float64(topCrop.Max.X) / prescalefactor))
|
||||
topCrop.Max.Y = int(chop(float64(topCrop.Max.Y) / prescalefactor))
|
||||
}
|
||||
|
||||
return topCrop.Canon(), nil
|
||||
}
|
||||
|
||||
func (c Crop) totalScore() float64 {
|
||||
return (c.Score.Detail*detailWeight + c.Score.Skin*skinWeight + c.Score.Saturation*saturationWeight) / float64(c.Dx()) / float64(c.Dy())
|
||||
}
|
||||
|
||||
func chop(x float64) float64 {
|
||||
if x < 0 {
|
||||
return math.Ceil(x)
|
||||
}
|
||||
return math.Floor(x)
|
||||
}
|
||||
|
||||
func thirds(x float64) float64 {
|
||||
x = (math.Mod(x-(1.0/3.0)+1.0, 2.0)*0.5 - 0.5) * 16.0
|
||||
return math.Max(1.0-x*x, 0.0)
|
||||
}
|
||||
|
||||
func bounds(l float64) float64 {
|
||||
return math.Min(math.Max(l, 0.0), 255)
|
||||
}
|
||||
|
||||
func importance(crop Crop, x, y int) float64 {
|
||||
if crop.Min.X > x || x >= crop.Max.X || crop.Min.Y > y || y >= crop.Max.Y {
|
||||
return outsideImportance
|
||||
}
|
||||
|
||||
xf := float64(x-crop.Min.X) / float64(crop.Dx())
|
||||
yf := float64(y-crop.Min.Y) / float64(crop.Dy())
|
||||
|
||||
px := math.Abs(0.5-xf) * 2.0
|
||||
py := math.Abs(0.5-yf) * 2.0
|
||||
|
||||
dx := math.Max(px-1.0+edgeRadius, 0.0)
|
||||
dy := math.Max(py-1.0+edgeRadius, 0.0)
|
||||
d := (dx*dx + dy*dy) * edgeWeight
|
||||
|
||||
s := 1.41 - math.Sqrt(px*px+py*py)
|
||||
if ruleOfThirds {
|
||||
s += (math.Max(0.0, s+d+0.5) * 1.2) * (thirds(px) + thirds(py))
|
||||
}
|
||||
|
||||
return s + d
|
||||
}
|
||||
|
||||
func score(output *image.RGBA, crop Crop) Score {
|
||||
width := output.Bounds().Dx()
|
||||
height := output.Bounds().Dy()
|
||||
score := Score{}
|
||||
|
||||
// same loops but with downsampling
|
||||
//for y := 0; y < height; y++ {
|
||||
//for x := 0; x < width; x++ {
|
||||
for y := 0; y <= height-scoreDownSample; y += scoreDownSample {
|
||||
for x := 0; x <= width-scoreDownSample; x += scoreDownSample {
|
||||
|
||||
c := output.RGBAAt(x, y)
|
||||
r8 := float64(c.R)
|
||||
g8 := float64(c.G)
|
||||
b8 := float64(c.B)
|
||||
|
||||
imp := importance(crop, int(x), int(y))
|
||||
det := g8 / 255.0
|
||||
|
||||
score.Skin += r8 / 255.0 * (det + skinBias) * imp
|
||||
score.Detail += det * imp
|
||||
score.Saturation += b8 / 255.0 * (det + saturationBias) * imp
|
||||
}
|
||||
}
|
||||
|
||||
return score
|
||||
}
|
||||
|
||||
func analyse(settings CropSettings, img *image.RGBA, cropWidth, cropHeight, realMinScale float64) (image.Rectangle, error) {
|
||||
o := image.NewRGBA(img.Bounds())
|
||||
|
||||
now := time.Now()
|
||||
edgeDetect(img, o)
|
||||
settings.Log.Println("Time elapsed edge:", time.Since(now))
|
||||
debugOutput(settings.DebugMode, o, "edge")
|
||||
|
||||
now = time.Now()
|
||||
skinDetect(img, o)
|
||||
settings.Log.Println("Time elapsed skin:", time.Since(now))
|
||||
debugOutput(settings.DebugMode, o, "skin")
|
||||
|
||||
now = time.Now()
|
||||
saturationDetect(img, o)
|
||||
settings.Log.Println("Time elapsed sat:", time.Since(now))
|
||||
debugOutput(settings.DebugMode, o, "saturation")
|
||||
|
||||
now = time.Now()
|
||||
var topCrop Crop
|
||||
topScore := -1.0
|
||||
cs := crops(o, cropWidth, cropHeight, realMinScale)
|
||||
settings.Log.Println("Time elapsed crops:", time.Since(now), len(cs))
|
||||
|
||||
now = time.Now()
|
||||
for _, crop := range cs {
|
||||
nowIn := time.Now()
|
||||
crop.Score = score(o, crop)
|
||||
settings.Log.Println("Time elapsed single-score:", time.Since(nowIn))
|
||||
if crop.totalScore() > topScore {
|
||||
topCrop = crop
|
||||
topScore = crop.totalScore()
|
||||
}
|
||||
}
|
||||
settings.Log.Println("Time elapsed score:", time.Since(now))
|
||||
|
||||
if settings.DebugMode {
|
||||
drawDebugCrop(topCrop, o)
|
||||
debugOutput(true, o, "final")
|
||||
}
|
||||
|
||||
return topCrop.Rectangle, nil
|
||||
}
|
||||
|
||||
func saturation(c color.RGBA) float64 {
|
||||
cMax, cMin := uint8(0), uint8(255)
|
||||
if c.R > cMax {
|
||||
cMax = c.R
|
||||
}
|
||||
if c.R < cMin {
|
||||
cMin = c.R
|
||||
}
|
||||
if c.G > cMax {
|
||||
cMax = c.G
|
||||
}
|
||||
if c.G < cMin {
|
||||
cMin = c.G
|
||||
}
|
||||
if c.B > cMax {
|
||||
cMax = c.B
|
||||
}
|
||||
if c.B < cMin {
|
||||
cMin = c.B
|
||||
}
|
||||
|
||||
if cMax == cMin {
|
||||
return 0
|
||||
}
|
||||
maximum := float64(cMax) / 255.0
|
||||
minimum := float64(cMin) / 255.0
|
||||
|
||||
l := (maximum + minimum) / 2.0
|
||||
d := maximum - minimum
|
||||
|
||||
if l > 0.5 {
|
||||
return d / (2.0 - maximum - minimum)
|
||||
}
|
||||
|
||||
return d / (maximum + minimum)
|
||||
}
|
||||
|
||||
func cie(c color.RGBA) float64 {
|
||||
return 0.5126*float64(c.B) + 0.7152*float64(c.G) + 0.0722*float64(c.R)
|
||||
}
|
||||
|
||||
func skinCol(c color.RGBA) float64 {
|
||||
r8, g8, b8 := float64(c.R), float64(c.G), float64(c.B)
|
||||
|
||||
mag := math.Sqrt(r8*r8 + g8*g8 + b8*b8)
|
||||
rd := r8/mag - skinColor[0]
|
||||
gd := g8/mag - skinColor[1]
|
||||
bd := b8/mag - skinColor[2]
|
||||
|
||||
d := math.Sqrt(rd*rd + gd*gd + bd*bd)
|
||||
return 1.0 - d
|
||||
}
|
||||
|
||||
func makeCies(img *image.RGBA) []float64 {
|
||||
width := img.Bounds().Dx()
|
||||
height := img.Bounds().Dy()
|
||||
cies := make([]float64, width*height, width*height)
|
||||
i := 0
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
cies[i] = cie(img.RGBAAt(x, y))
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return cies
|
||||
}
|
||||
|
||||
func edgeDetect(i *image.RGBA, o *image.RGBA) {
|
||||
width := i.Bounds().Dx()
|
||||
height := i.Bounds().Dy()
|
||||
cies := makeCies(i)
|
||||
|
||||
var lightness float64
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
if x == 0 || x >= width-1 || y == 0 || y >= height-1 {
|
||||
//lightness = cie((*i).At(x, y))
|
||||
lightness = 0
|
||||
} else {
|
||||
lightness = cies[y*width+x]*4.0 -
|
||||
cies[x+(y-1)*width] -
|
||||
cies[x-1+y*width] -
|
||||
cies[x+1+y*width] -
|
||||
cies[x+(y+1)*width]
|
||||
}
|
||||
|
||||
nc := color.RGBA{0, uint8(bounds(lightness)), 0, 255}
|
||||
o.SetRGBA(x, y, nc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func skinDetect(i *image.RGBA, o *image.RGBA) {
|
||||
width := i.Bounds().Dx()
|
||||
height := i.Bounds().Dy()
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
lightness := cie(i.RGBAAt(x, y)) / 255.0
|
||||
skin := skinCol(i.RGBAAt(x, y))
|
||||
|
||||
c := o.RGBAAt(x, y)
|
||||
if skin > skinThreshold && lightness >= skinBrightnessMin && lightness <= skinBrightnessMax {
|
||||
r := (skin - skinThreshold) * (255.0 / (1.0 - skinThreshold))
|
||||
nc := color.RGBA{uint8(bounds(r)), c.G, c.B, 255}
|
||||
o.SetRGBA(x, y, nc)
|
||||
} else {
|
||||
nc := color.RGBA{0, c.G, c.B, 255}
|
||||
o.SetRGBA(x, y, nc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func saturationDetect(i *image.RGBA, o *image.RGBA) {
|
||||
width := i.Bounds().Dx()
|
||||
height := i.Bounds().Dy()
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
lightness := cie(i.RGBAAt(x, y)) / 255.0
|
||||
saturation := saturation(i.RGBAAt(x, y))
|
||||
|
||||
c := o.RGBAAt(x, y)
|
||||
if saturation > saturationThreshold && lightness >= saturationBrightnessMin && lightness <= saturationBrightnessMax {
|
||||
b := (saturation - saturationThreshold) * (255.0 / (1.0 - saturationThreshold))
|
||||
nc := color.RGBA{c.R, c.G, uint8(bounds(b)), 255}
|
||||
o.SetRGBA(x, y, nc)
|
||||
} else {
|
||||
nc := color.RGBA{c.R, c.G, 0, 255}
|
||||
o.SetRGBA(x, y, nc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func crops(i image.Image, cropWidth, cropHeight, realMinScale float64) []Crop {
|
||||
res := []Crop{}
|
||||
width := i.Bounds().Dx()
|
||||
height := i.Bounds().Dy()
|
||||
|
||||
minDimension := math.Min(float64(width), float64(height))
|
||||
var cropW, cropH float64
|
||||
|
||||
if cropWidth != 0.0 {
|
||||
cropW = cropWidth
|
||||
} else {
|
||||
cropW = minDimension
|
||||
}
|
||||
if cropHeight != 0.0 {
|
||||
cropH = cropHeight
|
||||
} else {
|
||||
cropH = minDimension
|
||||
}
|
||||
|
||||
for scale := maxScale; scale >= realMinScale; scale -= scaleStep {
|
||||
for y := 0; float64(y)+cropH*scale <= float64(height); y += step {
|
||||
for x := 0; float64(x)+cropW*scale <= float64(width); x += step {
|
||||
res = append(res, Crop{
|
||||
Rectangle: image.Rect(x, y, x+int(cropW*scale), y+int(cropH*scale)),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// toRGBA converts an image.Image to an image.RGBA
|
||||
func toRGBA(img image.Image) *image.RGBA {
|
||||
switch img.(type) {
|
||||
case *image.RGBA:
|
||||
return img.(*image.RGBA)
|
||||
}
|
||||
out := image.NewRGBA(img.Bounds())
|
||||
draw.Copy(out, image.Pt(0, 0), img, img.Bounds(), draw.Src, nil)
|
||||
return out
|
||||
}
|
||||
|
||||
// SmartCrop applies the smartcrop algorithms on the the given image and returns
|
||||
// the top crop or an error if something went wrong.
|
||||
// This is still here for legacy/backwards-compat reasons
|
||||
func SmartCrop(img image.Image, width, height int) (image.Rectangle, error) {
|
||||
analyzer := NewAnalyzer()
|
||||
return analyzer.FindBestCrop(img, width, height)
|
||||
}
|
13
vendor/github.com/nfnt/resize/LICENSE
generated
vendored
Normal file
13
vendor/github.com/nfnt/resize/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
149
vendor/github.com/nfnt/resize/README.md
generated
vendored
Normal file
149
vendor/github.com/nfnt/resize/README.md
generated
vendored
Normal file
|
@ -0,0 +1,149 @@
|
|||
Resize
|
||||
======
|
||||
|
||||
Image resizing for the [Go programming language](http://golang.org) with common interpolation methods.
|
||||
|
||||
[![Build Status](https://travis-ci.org/nfnt/resize.svg)](https://travis-ci.org/nfnt/resize)
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
```bash
|
||||
$ go get github.com/nfnt/resize
|
||||
```
|
||||
|
||||
It's that easy!
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
This package needs at least Go 1.1. Import package with
|
||||
|
||||
```go
|
||||
import "github.com/nfnt/resize"
|
||||
```
|
||||
|
||||
The resize package provides 2 functions:
|
||||
|
||||
* `resize.Resize` creates a scaled image with new dimensions (`width`, `height`) using the interpolation function `interp`.
|
||||
If either `width` or `height` is set to 0, it will be set to an aspect ratio preserving value.
|
||||
* `resize.Thumbnail` downscales an image preserving its aspect ratio to the maximum dimensions (`maxWidth`, `maxHeight`).
|
||||
It will return the original image if original sizes are smaller than the provided dimensions.
|
||||
|
||||
```go
|
||||
resize.Resize(width, height uint, img image.Image, interp resize.InterpolationFunction) image.Image
|
||||
resize.Thumbnail(maxWidth, maxHeight uint, img image.Image, interp resize.InterpolationFunction) image.Image
|
||||
```
|
||||
|
||||
The provided interpolation functions are (from fast to slow execution time)
|
||||
|
||||
- `NearestNeighbor`: [Nearest-neighbor interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation)
|
||||
- `Bilinear`: [Bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation)
|
||||
- `Bicubic`: [Bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation)
|
||||
- `MitchellNetravali`: [Mitchell-Netravali interpolation](http://dl.acm.org/citation.cfm?id=378514)
|
||||
- `Lanczos2`: [Lanczos resampling](http://en.wikipedia.org/wiki/Lanczos_resampling) with a=2
|
||||
- `Lanczos3`: [Lanczos resampling](http://en.wikipedia.org/wiki/Lanczos_resampling) with a=3
|
||||
|
||||
Which of these methods gives the best results depends on your use case.
|
||||
|
||||
Sample usage:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/nfnt/resize"
|
||||
"image/jpeg"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// open "test.jpg"
|
||||
file, err := os.Open("test.jpg")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// decode jpeg into image.Image
|
||||
img, err := jpeg.Decode(file)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
file.Close()
|
||||
|
||||
// resize to width 1000 using Lanczos resampling
|
||||
// and preserve aspect ratio
|
||||
m := resize.Resize(1000, 0, img, resize.Lanczos3)
|
||||
|
||||
out, err := os.Create("test_resized.jpg")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// write new image to file
|
||||
jpeg.Encode(out, m, nil)
|
||||
}
|
||||
```
|
||||
|
||||
Caveats
|
||||
-------
|
||||
|
||||
* Optimized access routines are used for `image.RGBA`, `image.NRGBA`, `image.RGBA64`, `image.NRGBA64`, `image.YCbCr`, `image.Gray`, and `image.Gray16` types. All other image types are accessed in a generic way that will result in slow processing speed.
|
||||
* JPEG images are stored in `image.YCbCr`. This image format stores data in a way that will decrease processing speed. A resize may be up to 2 times slower than with `image.RGBA`.
|
||||
|
||||
|
||||
Downsizing Samples
|
||||
-------
|
||||
|
||||
Downsizing is not as simple as it might look like. Images have to be filtered before they are scaled down, otherwise aliasing might occur.
|
||||
Filtering is highly subjective: Applying too much will blur the whole image, too little will make aliasing become apparent.
|
||||
Resize tries to provide sane defaults that should suffice in most cases.
|
||||
|
||||
### Artificial sample
|
||||
|
||||
Original image
|
||||
![Rings](http://nfnt.github.com/img/rings_lg_orig.png)
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th><img src="http://nfnt.github.com/img/rings_300_NearestNeighbor.png" /><br>Nearest-Neighbor</th>
|
||||
<th><img src="http://nfnt.github.com/img/rings_300_Bilinear.png" /><br>Bilinear</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><img src="http://nfnt.github.com/img/rings_300_Bicubic.png" /><br>Bicubic</th>
|
||||
<th><img src="http://nfnt.github.com/img/rings_300_MitchellNetravali.png" /><br>Mitchell-Netravali</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><img src="http://nfnt.github.com/img/rings_300_Lanczos2.png" /><br>Lanczos2</th>
|
||||
<th><img src="http://nfnt.github.com/img/rings_300_Lanczos3.png" /><br>Lanczos3</th>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
### Real-Life sample
|
||||
|
||||
Original image
|
||||
![Original](http://nfnt.github.com/img/IMG_3694_720.jpg)
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th><img src="http://nfnt.github.com/img/IMG_3694_300_NearestNeighbor.png" /><br>Nearest-Neighbor</th>
|
||||
<th><img src="http://nfnt.github.com/img/IMG_3694_300_Bilinear.png" /><br>Bilinear</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><img src="http://nfnt.github.com/img/IMG_3694_300_Bicubic.png" /><br>Bicubic</th>
|
||||
<th><img src="http://nfnt.github.com/img/IMG_3694_300_MitchellNetravali.png" /><br>Mitchell-Netravali</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><img src="http://nfnt.github.com/img/IMG_3694_300_Lanczos2.png" /><br>Lanczos2</th>
|
||||
<th><img src="http://nfnt.github.com/img/IMG_3694_300_Lanczos3.png" /><br>Lanczos3</th>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
Copyright (c) 2012 Jan Schlicht <janschlicht@gmail.com>
|
||||
Resize is released under a MIT style license.
|
438
vendor/github.com/nfnt/resize/converter.go
generated
vendored
Normal file
438
vendor/github.com/nfnt/resize/converter.go
generated
vendored
Normal file
|
@ -0,0 +1,438 @@
|
|||
/*
|
||||
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package resize
|
||||
|
||||
import "image"
|
||||
|
||||
// Keep value in [0,255] range.
|
||||
func clampUint8(in int32) uint8 {
|
||||
// casting a negative int to an uint will result in an overflown
|
||||
// large uint. this behavior will be exploited here and in other functions
|
||||
// to achieve a higher performance.
|
||||
if uint32(in) < 256 {
|
||||
return uint8(in)
|
||||
}
|
||||
if in > 255 {
|
||||
return 255
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Keep value in [0,65535] range.
|
||||
func clampUint16(in int64) uint16 {
|
||||
if uint64(in) < 65536 {
|
||||
return uint16(in)
|
||||
}
|
||||
if in > 65535 {
|
||||
return 65535
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func resizeGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]int64
|
||||
var sum int64
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case xi < 0:
|
||||
xi = 0
|
||||
case xi >= maxX:
|
||||
xi = maxX
|
||||
}
|
||||
|
||||
r, g, b, a := in.At(xi+in.Bounds().Min.X, x+in.Bounds().Min.Y).RGBA()
|
||||
|
||||
rgba[0] += int64(coeff) * int64(r)
|
||||
rgba[1] += int64(coeff) * int64(g)
|
||||
rgba[2] += int64(coeff) * int64(b)
|
||||
rgba[3] += int64(coeff) * int64(a)
|
||||
sum += int64(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
||||
|
||||
value := clampUint16(rgba[0] / sum)
|
||||
out.Pix[offset+0] = uint8(value >> 8)
|
||||
out.Pix[offset+1] = uint8(value)
|
||||
value = clampUint16(rgba[1] / sum)
|
||||
out.Pix[offset+2] = uint8(value >> 8)
|
||||
out.Pix[offset+3] = uint8(value)
|
||||
value = clampUint16(rgba[2] / sum)
|
||||
out.Pix[offset+4] = uint8(value >> 8)
|
||||
out.Pix[offset+5] = uint8(value)
|
||||
value = clampUint16(rgba[3] / sum)
|
||||
out.Pix[offset+6] = uint8(value >> 8)
|
||||
out.Pix[offset+7] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]int32
|
||||
var sum int32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 4
|
||||
case xi >= maxX:
|
||||
xi = 4 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
|
||||
rgba[0] += int32(coeff) * int32(row[xi+0])
|
||||
rgba[1] += int32(coeff) * int32(row[xi+1])
|
||||
rgba[2] += int32(coeff) * int32(row[xi+2])
|
||||
rgba[3] += int32(coeff) * int32(row[xi+3])
|
||||
sum += int32(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
|
||||
|
||||
out.Pix[xo+0] = clampUint8(rgba[0] / sum)
|
||||
out.Pix[xo+1] = clampUint8(rgba[1] / sum)
|
||||
out.Pix[xo+2] = clampUint8(rgba[2] / sum)
|
||||
out.Pix[xo+3] = clampUint8(rgba[3] / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeNRGBA(in *image.NRGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]int32
|
||||
var sum int32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 4
|
||||
case xi >= maxX:
|
||||
xi = 4 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
|
||||
// Forward alpha-premultiplication
|
||||
a := int32(row[xi+3])
|
||||
r := int32(row[xi+0]) * a
|
||||
r /= 0xff
|
||||
g := int32(row[xi+1]) * a
|
||||
g /= 0xff
|
||||
b := int32(row[xi+2]) * a
|
||||
b /= 0xff
|
||||
|
||||
rgba[0] += int32(coeff) * r
|
||||
rgba[1] += int32(coeff) * g
|
||||
rgba[2] += int32(coeff) * b
|
||||
rgba[3] += int32(coeff) * a
|
||||
sum += int32(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
|
||||
|
||||
out.Pix[xo+0] = clampUint8(rgba[0] / sum)
|
||||
out.Pix[xo+1] = clampUint8(rgba[1] / sum)
|
||||
out.Pix[xo+2] = clampUint8(rgba[2] / sum)
|
||||
out.Pix[xo+3] = clampUint8(rgba[3] / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]int64
|
||||
var sum int64
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 8
|
||||
case xi >= maxX:
|
||||
xi = 8 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
|
||||
rgba[0] += int64(coeff) * (int64(row[xi+0])<<8 | int64(row[xi+1]))
|
||||
rgba[1] += int64(coeff) * (int64(row[xi+2])<<8 | int64(row[xi+3]))
|
||||
rgba[2] += int64(coeff) * (int64(row[xi+4])<<8 | int64(row[xi+5]))
|
||||
rgba[3] += int64(coeff) * (int64(row[xi+6])<<8 | int64(row[xi+7]))
|
||||
sum += int64(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
||||
|
||||
value := clampUint16(rgba[0] / sum)
|
||||
out.Pix[xo+0] = uint8(value >> 8)
|
||||
out.Pix[xo+1] = uint8(value)
|
||||
value = clampUint16(rgba[1] / sum)
|
||||
out.Pix[xo+2] = uint8(value >> 8)
|
||||
out.Pix[xo+3] = uint8(value)
|
||||
value = clampUint16(rgba[2] / sum)
|
||||
out.Pix[xo+4] = uint8(value >> 8)
|
||||
out.Pix[xo+5] = uint8(value)
|
||||
value = clampUint16(rgba[3] / sum)
|
||||
out.Pix[xo+6] = uint8(value >> 8)
|
||||
out.Pix[xo+7] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeNRGBA64(in *image.NRGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]int64
|
||||
var sum int64
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 8
|
||||
case xi >= maxX:
|
||||
xi = 8 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
|
||||
// Forward alpha-premultiplication
|
||||
a := int64(uint16(row[xi+6])<<8 | uint16(row[xi+7]))
|
||||
r := int64(uint16(row[xi+0])<<8|uint16(row[xi+1])) * a
|
||||
r /= 0xffff
|
||||
g := int64(uint16(row[xi+2])<<8|uint16(row[xi+3])) * a
|
||||
g /= 0xffff
|
||||
b := int64(uint16(row[xi+4])<<8|uint16(row[xi+5])) * a
|
||||
b /= 0xffff
|
||||
|
||||
rgba[0] += int64(coeff) * r
|
||||
rgba[1] += int64(coeff) * g
|
||||
rgba[2] += int64(coeff) * b
|
||||
rgba[3] += int64(coeff) * a
|
||||
sum += int64(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
||||
|
||||
value := clampUint16(rgba[0] / sum)
|
||||
out.Pix[xo+0] = uint8(value >> 8)
|
||||
out.Pix[xo+1] = uint8(value)
|
||||
value = clampUint16(rgba[1] / sum)
|
||||
out.Pix[xo+2] = uint8(value >> 8)
|
||||
out.Pix[xo+3] = uint8(value)
|
||||
value = clampUint16(rgba[2] / sum)
|
||||
out.Pix[xo+4] = uint8(value >> 8)
|
||||
out.Pix[xo+5] = uint8(value)
|
||||
value = clampUint16(rgba[3] / sum)
|
||||
out.Pix[xo+6] = uint8(value >> 8)
|
||||
out.Pix[xo+7] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[(x-newBounds.Min.X)*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var gray int32
|
||||
var sum int32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case xi < 0:
|
||||
xi = 0
|
||||
case xi >= maxX:
|
||||
xi = maxX
|
||||
}
|
||||
gray += int32(coeff) * int32(row[xi])
|
||||
sum += int32(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
|
||||
out.Pix[offset] = clampUint8(gray / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var gray int64
|
||||
var sum int64
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 2
|
||||
case xi >= maxX:
|
||||
xi = 2 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
gray += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
|
||||
sum += int64(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
|
||||
value := clampUint16(gray / sum)
|
||||
out.Pix[offset+0] = uint8(value >> 8)
|
||||
out.Pix[offset+1] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resizeYCbCr(in *ycc, out *ycc, scale float64, coeffs []int16, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var p [3]int32
|
||||
var sum int32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
coeff := coeffs[ci+i]
|
||||
if coeff != 0 {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 3
|
||||
case xi >= maxX:
|
||||
xi = 3 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
p[0] += int32(coeff) * int32(row[xi+0])
|
||||
p[1] += int32(coeff) * int32(row[xi+1])
|
||||
p[2] += int32(coeff) * int32(row[xi+2])
|
||||
sum += int32(coeff)
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
|
||||
out.Pix[xo+0] = clampUint8(p[0] / sum)
|
||||
out.Pix[xo+1] = clampUint8(p[1] / sum)
|
||||
out.Pix[xo+2] = clampUint8(p[2] / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestYCbCr(in *ycc, out *ycc, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var p [3]float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 3
|
||||
case xi >= maxX:
|
||||
xi = 3 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
p[0] += float32(row[xi+0])
|
||||
p[1] += float32(row[xi+1])
|
||||
p[2] += float32(row[xi+2])
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
|
||||
out.Pix[xo+0] = floatToUint8(p[0] / sum)
|
||||
out.Pix[xo+1] = floatToUint8(p[1] / sum)
|
||||
out.Pix[xo+2] = floatToUint8(p[2] / sum)
|
||||
}
|
||||
}
|
||||
}
|
143
vendor/github.com/nfnt/resize/filters.go
generated
vendored
Normal file
143
vendor/github.com/nfnt/resize/filters.go
generated
vendored
Normal file
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package resize
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func nearest(in float64) float64 {
|
||||
if in >= -0.5 && in < 0.5 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func linear(in float64) float64 {
|
||||
in = math.Abs(in)
|
||||
if in <= 1 {
|
||||
return 1 - in
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func cubic(in float64) float64 {
|
||||
in = math.Abs(in)
|
||||
if in <= 1 {
|
||||
return in*in*(1.5*in-2.5) + 1.0
|
||||
}
|
||||
if in <= 2 {
|
||||
return in*(in*(2.5-0.5*in)-4.0) + 2.0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func mitchellnetravali(in float64) float64 {
|
||||
in = math.Abs(in)
|
||||
if in <= 1 {
|
||||
return (7.0*in*in*in - 12.0*in*in + 5.33333333333) * 0.16666666666
|
||||
}
|
||||
if in <= 2 {
|
||||
return (-2.33333333333*in*in*in + 12.0*in*in - 20.0*in + 10.6666666667) * 0.16666666666
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func sinc(x float64) float64 {
|
||||
x = math.Abs(x) * math.Pi
|
||||
if x >= 1.220703e-4 {
|
||||
return math.Sin(x) / x
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func lanczos2(in float64) float64 {
|
||||
if in > -2 && in < 2 {
|
||||
return sinc(in) * sinc(in*0.5)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func lanczos3(in float64) float64 {
|
||||
if in > -3 && in < 3 {
|
||||
return sinc(in) * sinc(in*0.3333333333333333)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// range [-256,256]
|
||||
func createWeights8(dy, filterLength int, blur, scale float64, kernel func(float64) float64) ([]int16, []int, int) {
|
||||
filterLength = filterLength * int(math.Max(math.Ceil(blur*scale), 1))
|
||||
filterFactor := math.Min(1./(blur*scale), 1)
|
||||
|
||||
coeffs := make([]int16, dy*filterLength)
|
||||
start := make([]int, dy)
|
||||
for y := 0; y < dy; y++ {
|
||||
interpX := scale*(float64(y)+0.5) - 0.5
|
||||
start[y] = int(interpX) - filterLength/2 + 1
|
||||
interpX -= float64(start[y])
|
||||
for i := 0; i < filterLength; i++ {
|
||||
in := (interpX - float64(i)) * filterFactor
|
||||
coeffs[y*filterLength+i] = int16(kernel(in) * 256)
|
||||
}
|
||||
}
|
||||
|
||||
return coeffs, start, filterLength
|
||||
}
|
||||
|
||||
// range [-65536,65536]
|
||||
func createWeights16(dy, filterLength int, blur, scale float64, kernel func(float64) float64) ([]int32, []int, int) {
|
||||
filterLength = filterLength * int(math.Max(math.Ceil(blur*scale), 1))
|
||||
filterFactor := math.Min(1./(blur*scale), 1)
|
||||
|
||||
coeffs := make([]int32, dy*filterLength)
|
||||
start := make([]int, dy)
|
||||
for y := 0; y < dy; y++ {
|
||||
interpX := scale*(float64(y)+0.5) - 0.5
|
||||
start[y] = int(interpX) - filterLength/2 + 1
|
||||
interpX -= float64(start[y])
|
||||
for i := 0; i < filterLength; i++ {
|
||||
in := (interpX - float64(i)) * filterFactor
|
||||
coeffs[y*filterLength+i] = int32(kernel(in) * 65536)
|
||||
}
|
||||
}
|
||||
|
||||
return coeffs, start, filterLength
|
||||
}
|
||||
|
||||
func createWeightsNearest(dy, filterLength int, blur, scale float64) ([]bool, []int, int) {
|
||||
filterLength = filterLength * int(math.Max(math.Ceil(blur*scale), 1))
|
||||
filterFactor := math.Min(1./(blur*scale), 1)
|
||||
|
||||
coeffs := make([]bool, dy*filterLength)
|
||||
start := make([]int, dy)
|
||||
for y := 0; y < dy; y++ {
|
||||
interpX := scale*(float64(y)+0.5) - 0.5
|
||||
start[y] = int(interpX) - filterLength/2 + 1
|
||||
interpX -= float64(start[y])
|
||||
for i := 0; i < filterLength; i++ {
|
||||
in := (interpX - float64(i)) * filterFactor
|
||||
if in >= -0.5 && in < 0.5 {
|
||||
coeffs[y*filterLength+i] = true
|
||||
} else {
|
||||
coeffs[y*filterLength+i] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return coeffs, start, filterLength
|
||||
}
|
318
vendor/github.com/nfnt/resize/nearest.go
generated
vendored
Normal file
318
vendor/github.com/nfnt/resize/nearest.go
generated
vendored
Normal file
|
@ -0,0 +1,318 @@
|
|||
/*
|
||||
Copyright (c) 2014, Charlie Vieth <charlie.vieth@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package resize
|
||||
|
||||
import "image"
|
||||
|
||||
func floatToUint8(x float32) uint8 {
|
||||
// Nearest-neighbor values are always
|
||||
// positive no need to check lower-bound.
|
||||
if x > 0xfe {
|
||||
return 0xff
|
||||
}
|
||||
return uint8(x)
|
||||
}
|
||||
|
||||
func floatToUint16(x float32) uint16 {
|
||||
if x > 0xfffe {
|
||||
return 0xffff
|
||||
}
|
||||
return uint16(x)
|
||||
}
|
||||
|
||||
func nearestGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case xi < 0:
|
||||
xi = 0
|
||||
case xi >= maxX:
|
||||
xi = maxX
|
||||
}
|
||||
r, g, b, a := in.At(xi+in.Bounds().Min.X, x+in.Bounds().Min.Y).RGBA()
|
||||
rgba[0] += float32(r)
|
||||
rgba[1] += float32(g)
|
||||
rgba[2] += float32(b)
|
||||
rgba[3] += float32(a)
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
||||
value := floatToUint16(rgba[0] / sum)
|
||||
out.Pix[offset+0] = uint8(value >> 8)
|
||||
out.Pix[offset+1] = uint8(value)
|
||||
value = floatToUint16(rgba[1] / sum)
|
||||
out.Pix[offset+2] = uint8(value >> 8)
|
||||
out.Pix[offset+3] = uint8(value)
|
||||
value = floatToUint16(rgba[2] / sum)
|
||||
out.Pix[offset+4] = uint8(value >> 8)
|
||||
out.Pix[offset+5] = uint8(value)
|
||||
value = floatToUint16(rgba[3] / sum)
|
||||
out.Pix[offset+6] = uint8(value >> 8)
|
||||
out.Pix[offset+7] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 4
|
||||
case xi >= maxX:
|
||||
xi = 4 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
rgba[0] += float32(row[xi+0])
|
||||
rgba[1] += float32(row[xi+1])
|
||||
rgba[2] += float32(row[xi+2])
|
||||
rgba[3] += float32(row[xi+3])
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
|
||||
out.Pix[xo+0] = floatToUint8(rgba[0] / sum)
|
||||
out.Pix[xo+1] = floatToUint8(rgba[1] / sum)
|
||||
out.Pix[xo+2] = floatToUint8(rgba[2] / sum)
|
||||
out.Pix[xo+3] = floatToUint8(rgba[3] / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestNRGBA(in *image.NRGBA, out *image.NRGBA, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 4
|
||||
case xi >= maxX:
|
||||
xi = 4 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
rgba[0] += float32(row[xi+0])
|
||||
rgba[1] += float32(row[xi+1])
|
||||
rgba[2] += float32(row[xi+2])
|
||||
rgba[3] += float32(row[xi+3])
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
|
||||
out.Pix[xo+0] = floatToUint8(rgba[0] / sum)
|
||||
out.Pix[xo+1] = floatToUint8(rgba[1] / sum)
|
||||
out.Pix[xo+2] = floatToUint8(rgba[2] / sum)
|
||||
out.Pix[xo+3] = floatToUint8(rgba[3] / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 8
|
||||
case xi >= maxX:
|
||||
xi = 8 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
rgba[0] += float32(uint16(row[xi+0])<<8 | uint16(row[xi+1]))
|
||||
rgba[1] += float32(uint16(row[xi+2])<<8 | uint16(row[xi+3]))
|
||||
rgba[2] += float32(uint16(row[xi+4])<<8 | uint16(row[xi+5]))
|
||||
rgba[3] += float32(uint16(row[xi+6])<<8 | uint16(row[xi+7]))
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
||||
value := floatToUint16(rgba[0] / sum)
|
||||
out.Pix[xo+0] = uint8(value >> 8)
|
||||
out.Pix[xo+1] = uint8(value)
|
||||
value = floatToUint16(rgba[1] / sum)
|
||||
out.Pix[xo+2] = uint8(value >> 8)
|
||||
out.Pix[xo+3] = uint8(value)
|
||||
value = floatToUint16(rgba[2] / sum)
|
||||
out.Pix[xo+4] = uint8(value >> 8)
|
||||
out.Pix[xo+5] = uint8(value)
|
||||
value = floatToUint16(rgba[3] / sum)
|
||||
out.Pix[xo+6] = uint8(value >> 8)
|
||||
out.Pix[xo+7] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestNRGBA64(in *image.NRGBA64, out *image.NRGBA64, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var rgba [4]float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 8
|
||||
case xi >= maxX:
|
||||
xi = 8 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
rgba[0] += float32(uint16(row[xi+0])<<8 | uint16(row[xi+1]))
|
||||
rgba[1] += float32(uint16(row[xi+2])<<8 | uint16(row[xi+3]))
|
||||
rgba[2] += float32(uint16(row[xi+4])<<8 | uint16(row[xi+5]))
|
||||
rgba[3] += float32(uint16(row[xi+6])<<8 | uint16(row[xi+7]))
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
||||
value := floatToUint16(rgba[0] / sum)
|
||||
out.Pix[xo+0] = uint8(value >> 8)
|
||||
out.Pix[xo+1] = uint8(value)
|
||||
value = floatToUint16(rgba[1] / sum)
|
||||
out.Pix[xo+2] = uint8(value >> 8)
|
||||
out.Pix[xo+3] = uint8(value)
|
||||
value = floatToUint16(rgba[2] / sum)
|
||||
out.Pix[xo+4] = uint8(value >> 8)
|
||||
out.Pix[xo+5] = uint8(value)
|
||||
value = floatToUint16(rgba[3] / sum)
|
||||
out.Pix[xo+6] = uint8(value >> 8)
|
||||
out.Pix[xo+7] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestGray(in *image.Gray, out *image.Gray, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var gray float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case xi < 0:
|
||||
xi = 0
|
||||
case xi >= maxX:
|
||||
xi = maxX
|
||||
}
|
||||
gray += float32(row[xi])
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
|
||||
out.Pix[offset] = floatToUint8(gray / sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nearestGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []bool, offset []int, filterLength int) {
|
||||
newBounds := out.Bounds()
|
||||
maxX := in.Bounds().Dx() - 1
|
||||
|
||||
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
||||
row := in.Pix[x*in.Stride:]
|
||||
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
||||
var gray float32
|
||||
var sum float32
|
||||
start := offset[y]
|
||||
ci := y * filterLength
|
||||
for i := 0; i < filterLength; i++ {
|
||||
if coeffs[ci+i] {
|
||||
xi := start + i
|
||||
switch {
|
||||
case uint(xi) < uint(maxX):
|
||||
xi *= 2
|
||||
case xi >= maxX:
|
||||
xi = 2 * maxX
|
||||
default:
|
||||
xi = 0
|
||||
}
|
||||
gray += float32(uint16(row[xi+0])<<8 | uint16(row[xi+1]))
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
|
||||
value := floatToUint16(gray / sum)
|
||||
out.Pix[offset+0] = uint8(value >> 8)
|
||||
out.Pix[offset+1] = uint8(value)
|
||||
}
|
||||
}
|
||||
}
|
614
vendor/github.com/nfnt/resize/resize.go
generated
vendored
Normal file
614
vendor/github.com/nfnt/resize/resize.go
generated
vendored
Normal file
|
@ -0,0 +1,614 @@
|
|||
/*
|
||||
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
// Package resize implements various image resizing methods.
|
||||
//
|
||||
// The package works with the Image interface described in the image package.
|
||||
// Various interpolation methods are provided and multiple processors may be
|
||||
// utilized in the computations.
|
||||
//
|
||||
// Example:
|
||||
// imgResized := resize.Resize(1000, 0, imgOld, resize.MitchellNetravali)
|
||||
package resize
|
||||
|
||||
import (
|
||||
"image"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// An InterpolationFunction provides the parameters that describe an
|
||||
// interpolation kernel. It returns the number of samples to take
|
||||
// and the kernel function to use for sampling.
|
||||
type InterpolationFunction int
|
||||
|
||||
// InterpolationFunction constants
|
||||
const (
|
||||
// Nearest-neighbor interpolation
|
||||
NearestNeighbor InterpolationFunction = iota
|
||||
// Bilinear interpolation
|
||||
Bilinear
|
||||
// Bicubic interpolation (with cubic hermite spline)
|
||||
Bicubic
|
||||
// Mitchell-Netravali interpolation
|
||||
MitchellNetravali
|
||||
// Lanczos interpolation (a=2)
|
||||
Lanczos2
|
||||
// Lanczos interpolation (a=3)
|
||||
Lanczos3
|
||||
)
|
||||
|
||||
// kernal, returns an InterpolationFunctions taps and kernel.
|
||||
func (i InterpolationFunction) kernel() (int, func(float64) float64) {
|
||||
switch i {
|
||||
case Bilinear:
|
||||
return 2, linear
|
||||
case Bicubic:
|
||||
return 4, cubic
|
||||
case MitchellNetravali:
|
||||
return 4, mitchellnetravali
|
||||
case Lanczos2:
|
||||
return 4, lanczos2
|
||||
case Lanczos3:
|
||||
return 6, lanczos3
|
||||
default:
|
||||
// Default to NearestNeighbor.
|
||||
return 2, nearest
|
||||
}
|
||||
}
|
||||
|
||||
// values <1 will sharpen the image
|
||||
var blur = 1.0
|
||||
|
||||
// Resize scales an image to new width and height using the interpolation function interp.
|
||||
// A new image with the given dimensions will be returned.
|
||||
// If one of the parameters width or height is set to 0, its size will be calculated so that
|
||||
// the aspect ratio is that of the originating image.
|
||||
// The resizing algorithm uses channels for parallel computation.
|
||||
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
|
||||
scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
|
||||
if width == 0 {
|
||||
width = uint(0.7 + float64(img.Bounds().Dx())/scaleX)
|
||||
}
|
||||
if height == 0 {
|
||||
height = uint(0.7 + float64(img.Bounds().Dy())/scaleY)
|
||||
}
|
||||
|
||||
// Trivial case: return input image
|
||||
if int(width) == img.Bounds().Dx() && int(height) == img.Bounds().Dy() {
|
||||
return img
|
||||
}
|
||||
|
||||
if interp == NearestNeighbor {
|
||||
return resizeNearest(width, height, scaleX, scaleY, img, interp)
|
||||
}
|
||||
|
||||
taps, kernel := interp.kernel()
|
||||
cpus := runtime.GOMAXPROCS(0)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
// Generic access to image.Image is slow in tight loops.
|
||||
// The optimal access has to be determined from the concrete image type.
|
||||
switch input := img.(type) {
|
||||
case *image.RGBA:
|
||||
// 8-bit precision
|
||||
temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.NRGBA:
|
||||
// 8-bit precision
|
||||
temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeNRGBA(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
|
||||
case *image.YCbCr:
|
||||
// 8-bit precision
|
||||
// accessing the YCbCr arrays in a tight loop is slow.
|
||||
// converting the image to ycc increases performance by 2x.
|
||||
temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
|
||||
result := newYCC(image.Rect(0, 0, int(width), int(height)), image.YCbCrSubsampleRatio444)
|
||||
|
||||
coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
in := imageYCbCrToYCC(input)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*ycc)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*ycc)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result.YCbCr()
|
||||
case *image.RGBA64:
|
||||
// 16-bit precision
|
||||
temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.NRGBA64:
|
||||
// 16-bit precision
|
||||
temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeNRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.Gray:
|
||||
// 8-bit precision
|
||||
temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.Gray)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeGray(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.Gray)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeGray(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.Gray16:
|
||||
// 16-bit precision
|
||||
temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.Gray16)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeGray16(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.Gray16)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeGray16(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
default:
|
||||
// 16-bit precision
|
||||
temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeGeneric(img, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
func resizeNearest(width, height uint, scaleX, scaleY float64, img image.Image, interp InterpolationFunction) image.Image {
|
||||
taps, _ := interp.kernel()
|
||||
cpus := runtime.GOMAXPROCS(0)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
switch input := img.(type) {
|
||||
case *image.RGBA:
|
||||
// 8-bit precision
|
||||
temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestRGBA(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.NRGBA:
|
||||
// 8-bit precision
|
||||
temp := image.NewNRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewNRGBA(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.NRGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestNRGBA(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.NRGBA)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestNRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.YCbCr:
|
||||
// 8-bit precision
|
||||
// accessing the YCbCr arrays in a tight loop is slow.
|
||||
// converting the image to ycc increases performance by 2x.
|
||||
temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
|
||||
result := newYCC(image.Rect(0, 0, int(width), int(height)), image.YCbCrSubsampleRatio444)
|
||||
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
in := imageYCbCrToYCC(input)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*ycc)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*ycc)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result.YCbCr()
|
||||
case *image.RGBA64:
|
||||
// 16-bit precision
|
||||
temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.NRGBA64:
|
||||
// 16-bit precision
|
||||
temp := image.NewNRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewNRGBA64(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.NRGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestNRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.NRGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestNRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.Gray:
|
||||
// 8-bit precision
|
||||
temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.Gray)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestGray(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.Gray)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestGray(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
case *image.Gray16:
|
||||
// 16-bit precision
|
||||
temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
|
||||
result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.Gray16)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestGray16(input, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.Gray16)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestGray16(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
default:
|
||||
// 16-bit precision
|
||||
temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
|
||||
result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
|
||||
|
||||
// horizontal filter, results in transposed temporary image
|
||||
coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(temp, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestGeneric(img, slice, scaleX, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// horizontal filter on transposed image, result is not transposed
|
||||
coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
|
||||
wg.Add(cpus)
|
||||
for i := 0; i < cpus; i++ {
|
||||
slice := makeSlice(result, i, cpus).(*image.RGBA64)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
nearestRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Calculates scaling factors using old and new image dimensions.
|
||||
func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
|
||||
if width == 0 {
|
||||
if height == 0 {
|
||||
scaleX = 1.0
|
||||
scaleY = 1.0
|
||||
} else {
|
||||
scaleY = oldHeight / float64(height)
|
||||
scaleX = scaleY
|
||||
}
|
||||
} else {
|
||||
scaleX = oldWidth / float64(width)
|
||||
if height == 0 {
|
||||
scaleY = scaleX
|
||||
} else {
|
||||
scaleY = oldHeight / float64(height)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type imageWithSubImage interface {
|
||||
image.Image
|
||||
SubImage(image.Rectangle) image.Image
|
||||
}
|
||||
|
||||
func makeSlice(img imageWithSubImage, i, n int) image.Image {
|
||||
return img.SubImage(image.Rect(img.Bounds().Min.X, img.Bounds().Min.Y+i*img.Bounds().Dy()/n, img.Bounds().Max.X, img.Bounds().Min.Y+(i+1)*img.Bounds().Dy()/n))
|
||||
}
|
55
vendor/github.com/nfnt/resize/thumbnail.go
generated
vendored
Normal file
55
vendor/github.com/nfnt/resize/thumbnail.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package resize
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
// Thumbnail will downscale provided image to max width and height preserving
|
||||
// original aspect ratio and using the interpolation function interp.
|
||||
// It will return original image, without processing it, if original sizes
|
||||
// are already smaller than provided constraints.
|
||||
func Thumbnail(maxWidth, maxHeight uint, img image.Image, interp InterpolationFunction) image.Image {
|
||||
origBounds := img.Bounds()
|
||||
origWidth := uint(origBounds.Dx())
|
||||
origHeight := uint(origBounds.Dy())
|
||||
newWidth, newHeight := origWidth, origHeight
|
||||
|
||||
// Return original image if it have same or smaller size as constraints
|
||||
if maxWidth >= origWidth && maxHeight >= origHeight {
|
||||
return img
|
||||
}
|
||||
|
||||
// Preserve aspect ratio
|
||||
if origWidth > maxWidth {
|
||||
newHeight = uint(origHeight * maxWidth / origWidth)
|
||||
if newHeight < 1 {
|
||||
newHeight = 1
|
||||
}
|
||||
newWidth = maxWidth
|
||||
}
|
||||
|
||||
if newHeight > maxHeight {
|
||||
newWidth = uint(newWidth * maxHeight / newHeight)
|
||||
if newWidth < 1 {
|
||||
newWidth = 1
|
||||
}
|
||||
newHeight = maxHeight
|
||||
}
|
||||
return Resize(newWidth, newHeight, img, interp)
|
||||
}
|
227
vendor/github.com/nfnt/resize/ycc.go
generated
vendored
Normal file
227
vendor/github.com/nfnt/resize/ycc.go
generated
vendored
Normal file
|
@ -0,0 +1,227 @@
|
|||
/*
|
||||
Copyright (c) 2014, Charlie Vieth <charlie.vieth@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package resize
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
// ycc is an in memory YCbCr image. The Y, Cb and Cr samples are held in a
|
||||
// single slice to increase resizing performance.
|
||||
type ycc struct {
|
||||
// Pix holds the image's pixels, in Y, Cb, Cr order. The pixel at
|
||||
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*3].
|
||||
Pix []uint8
|
||||
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
|
||||
Stride int
|
||||
// Rect is the image's bounds.
|
||||
Rect image.Rectangle
|
||||
// SubsampleRatio is the subsample ratio of the original YCbCr image.
|
||||
SubsampleRatio image.YCbCrSubsampleRatio
|
||||
}
|
||||
|
||||
// PixOffset returns the index of the first element of Pix that corresponds to
|
||||
// the pixel at (x, y).
|
||||
func (p *ycc) PixOffset(x, y int) int {
|
||||
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
|
||||
}
|
||||
|
||||
func (p *ycc) Bounds() image.Rectangle {
|
||||
return p.Rect
|
||||
}
|
||||
|
||||
func (p *ycc) ColorModel() color.Model {
|
||||
return color.YCbCrModel
|
||||
}
|
||||
|
||||
func (p *ycc) At(x, y int) color.Color {
|
||||
if !(image.Point{x, y}.In(p.Rect)) {
|
||||
return color.YCbCr{}
|
||||
}
|
||||
i := p.PixOffset(x, y)
|
||||
return color.YCbCr{
|
||||
p.Pix[i+0],
|
||||
p.Pix[i+1],
|
||||
p.Pix[i+2],
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ycc) Opaque() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SubImage returns an image representing the portion of the image p visible
|
||||
// through r. The returned value shares pixels with the original image.
|
||||
func (p *ycc) SubImage(r image.Rectangle) image.Image {
|
||||
r = r.Intersect(p.Rect)
|
||||
if r.Empty() {
|
||||
return &ycc{SubsampleRatio: p.SubsampleRatio}
|
||||
}
|
||||
i := p.PixOffset(r.Min.X, r.Min.Y)
|
||||
return &ycc{
|
||||
Pix: p.Pix[i:],
|
||||
Stride: p.Stride,
|
||||
Rect: r,
|
||||
SubsampleRatio: p.SubsampleRatio,
|
||||
}
|
||||
}
|
||||
|
||||
// newYCC returns a new ycc with the given bounds and subsample ratio.
|
||||
func newYCC(r image.Rectangle, s image.YCbCrSubsampleRatio) *ycc {
|
||||
w, h := r.Dx(), r.Dy()
|
||||
buf := make([]uint8, 3*w*h)
|
||||
return &ycc{Pix: buf, Stride: 3 * w, Rect: r, SubsampleRatio: s}
|
||||
}
|
||||
|
||||
// YCbCr converts ycc to a YCbCr image with the same subsample ratio
|
||||
// as the YCbCr image that ycc was generated from.
|
||||
func (p *ycc) YCbCr() *image.YCbCr {
|
||||
ycbcr := image.NewYCbCr(p.Rect, p.SubsampleRatio)
|
||||
var off int
|
||||
|
||||
switch ycbcr.SubsampleRatio {
|
||||
case image.YCbCrSubsampleRatio422:
|
||||
for y := ycbcr.Rect.Min.Y; y < ycbcr.Rect.Max.Y; y++ {
|
||||
yy := (y - ycbcr.Rect.Min.Y) * ycbcr.YStride
|
||||
cy := (y - ycbcr.Rect.Min.Y) * ycbcr.CStride
|
||||
for x := ycbcr.Rect.Min.X; x < ycbcr.Rect.Max.X; x++ {
|
||||
xx := (x - ycbcr.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx/2
|
||||
ycbcr.Y[yi] = p.Pix[off+0]
|
||||
ycbcr.Cb[ci] = p.Pix[off+1]
|
||||
ycbcr.Cr[ci] = p.Pix[off+2]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
case image.YCbCrSubsampleRatio420:
|
||||
for y := ycbcr.Rect.Min.Y; y < ycbcr.Rect.Max.Y; y++ {
|
||||
yy := (y - ycbcr.Rect.Min.Y) * ycbcr.YStride
|
||||
cy := (y/2 - ycbcr.Rect.Min.Y/2) * ycbcr.CStride
|
||||
for x := ycbcr.Rect.Min.X; x < ycbcr.Rect.Max.X; x++ {
|
||||
xx := (x - ycbcr.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx/2
|
||||
ycbcr.Y[yi] = p.Pix[off+0]
|
||||
ycbcr.Cb[ci] = p.Pix[off+1]
|
||||
ycbcr.Cr[ci] = p.Pix[off+2]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
case image.YCbCrSubsampleRatio440:
|
||||
for y := ycbcr.Rect.Min.Y; y < ycbcr.Rect.Max.Y; y++ {
|
||||
yy := (y - ycbcr.Rect.Min.Y) * ycbcr.YStride
|
||||
cy := (y/2 - ycbcr.Rect.Min.Y/2) * ycbcr.CStride
|
||||
for x := ycbcr.Rect.Min.X; x < ycbcr.Rect.Max.X; x++ {
|
||||
xx := (x - ycbcr.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx
|
||||
ycbcr.Y[yi] = p.Pix[off+0]
|
||||
ycbcr.Cb[ci] = p.Pix[off+1]
|
||||
ycbcr.Cr[ci] = p.Pix[off+2]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Default to 4:4:4 subsampling.
|
||||
for y := ycbcr.Rect.Min.Y; y < ycbcr.Rect.Max.Y; y++ {
|
||||
yy := (y - ycbcr.Rect.Min.Y) * ycbcr.YStride
|
||||
cy := (y - ycbcr.Rect.Min.Y) * ycbcr.CStride
|
||||
for x := ycbcr.Rect.Min.X; x < ycbcr.Rect.Max.X; x++ {
|
||||
xx := (x - ycbcr.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx
|
||||
ycbcr.Y[yi] = p.Pix[off+0]
|
||||
ycbcr.Cb[ci] = p.Pix[off+1]
|
||||
ycbcr.Cr[ci] = p.Pix[off+2]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
}
|
||||
return ycbcr
|
||||
}
|
||||
|
||||
// imageYCbCrToYCC converts a YCbCr image to a ycc image for resizing.
|
||||
func imageYCbCrToYCC(in *image.YCbCr) *ycc {
|
||||
w, h := in.Rect.Dx(), in.Rect.Dy()
|
||||
r := image.Rect(0, 0, w, h)
|
||||
buf := make([]uint8, 3*w*h)
|
||||
p := ycc{Pix: buf, Stride: 3 * w, Rect: r, SubsampleRatio: in.SubsampleRatio}
|
||||
var off int
|
||||
|
||||
switch in.SubsampleRatio {
|
||||
case image.YCbCrSubsampleRatio422:
|
||||
for y := in.Rect.Min.Y; y < in.Rect.Max.Y; y++ {
|
||||
yy := (y - in.Rect.Min.Y) * in.YStride
|
||||
cy := (y - in.Rect.Min.Y) * in.CStride
|
||||
for x := in.Rect.Min.X; x < in.Rect.Max.X; x++ {
|
||||
xx := (x - in.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx/2
|
||||
p.Pix[off+0] = in.Y[yi]
|
||||
p.Pix[off+1] = in.Cb[ci]
|
||||
p.Pix[off+2] = in.Cr[ci]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
case image.YCbCrSubsampleRatio420:
|
||||
for y := in.Rect.Min.Y; y < in.Rect.Max.Y; y++ {
|
||||
yy := (y - in.Rect.Min.Y) * in.YStride
|
||||
cy := (y/2 - in.Rect.Min.Y/2) * in.CStride
|
||||
for x := in.Rect.Min.X; x < in.Rect.Max.X; x++ {
|
||||
xx := (x - in.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx/2
|
||||
p.Pix[off+0] = in.Y[yi]
|
||||
p.Pix[off+1] = in.Cb[ci]
|
||||
p.Pix[off+2] = in.Cr[ci]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
case image.YCbCrSubsampleRatio440:
|
||||
for y := in.Rect.Min.Y; y < in.Rect.Max.Y; y++ {
|
||||
yy := (y - in.Rect.Min.Y) * in.YStride
|
||||
cy := (y/2 - in.Rect.Min.Y/2) * in.CStride
|
||||
for x := in.Rect.Min.X; x < in.Rect.Max.X; x++ {
|
||||
xx := (x - in.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx
|
||||
p.Pix[off+0] = in.Y[yi]
|
||||
p.Pix[off+1] = in.Cb[ci]
|
||||
p.Pix[off+2] = in.Cr[ci]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Default to 4:4:4 subsampling.
|
||||
for y := in.Rect.Min.Y; y < in.Rect.Max.Y; y++ {
|
||||
yy := (y - in.Rect.Min.Y) * in.YStride
|
||||
cy := (y - in.Rect.Min.Y) * in.CStride
|
||||
for x := in.Rect.Min.X; x < in.Rect.Max.X; x++ {
|
||||
xx := (x - in.Rect.Min.X)
|
||||
yi := yy + xx
|
||||
ci := cy + xx
|
||||
p.Pix[off+0] = in.Y[yi]
|
||||
p.Pix[off+1] = in.Cb[ci]
|
||||
p.Pix[off+2] = in.Cr[ci]
|
||||
off += 3
|
||||
}
|
||||
}
|
||||
}
|
||||
return &p
|
||||
}
|
43
vendor/golang.org/x/image/draw/draw.go
generated
vendored
Normal file
43
vendor/golang.org/x/image/draw/draw.go
generated
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package draw provides image composition functions.
|
||||
//
|
||||
// See "The Go image/draw package" for an introduction to this package:
|
||||
// http://golang.org/doc/articles/image_draw.html
|
||||
//
|
||||
// This package is a superset of and a drop-in replacement for the image/draw
|
||||
// package in the standard library.
|
||||
package draw
|
||||
|
||||
// This file, and the go1_*.go files, just contains the API exported by the
|
||||
// image/draw package in the standard library. Other files in this package
|
||||
// provide additional features.
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
)
|
||||
|
||||
// Draw calls DrawMask with a nil mask.
|
||||
func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op) {
|
||||
draw.Draw(dst, r, src, sp, draw.Op(op))
|
||||
}
|
||||
|
||||
// DrawMask aligns r.Min in dst with sp in src and mp in mask and then
|
||||
// replaces the rectangle r in dst with the result of a Porter-Duff
|
||||
// composition. A nil mask is treated as opaque.
|
||||
func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) {
|
||||
draw.DrawMask(dst, r, src, sp, mask, mp, draw.Op(op))
|
||||
}
|
||||
|
||||
// FloydSteinberg is a Drawer that is the Src Op with Floyd-Steinberg error
|
||||
// diffusion.
|
||||
var FloydSteinberg Drawer = floydSteinberg{}
|
||||
|
||||
type floydSteinberg struct{}
|
||||
|
||||
func (floydSteinberg) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) {
|
||||
draw.FloydSteinberg.Draw(dst, r, src, sp)
|
||||
}
|
1403
vendor/golang.org/x/image/draw/gen.go
generated
vendored
Normal file
1403
vendor/golang.org/x/image/draw/gen.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
49
vendor/golang.org/x/image/draw/go1_8.go
generated
vendored
Normal file
49
vendor/golang.org/x/image/draw/go1_8.go
generated
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !go1.9,!go1.8.typealias
|
||||
|
||||
package draw
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
)
|
||||
|
||||
// Drawer contains the Draw method.
|
||||
type Drawer interface {
|
||||
// Draw aligns r.Min in dst with sp in src and then replaces the
|
||||
// rectangle r in dst with the result of drawing src on dst.
|
||||
Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
|
||||
}
|
||||
|
||||
// Image is an image.Image with a Set method to change a single pixel.
|
||||
type Image interface {
|
||||
image.Image
|
||||
Set(x, y int, c color.Color)
|
||||
}
|
||||
|
||||
// Op is a Porter-Duff compositing operator.
|
||||
type Op int
|
||||
|
||||
const (
|
||||
// Over specifies ``(src in mask) over dst''.
|
||||
Over Op = Op(draw.Over)
|
||||
// Src specifies ``src in mask''.
|
||||
Src Op = Op(draw.Src)
|
||||
)
|
||||
|
||||
// Draw implements the Drawer interface by calling the Draw function with
|
||||
// this Op.
|
||||
func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) {
|
||||
(draw.Op(op)).Draw(dst, r, src, sp)
|
||||
}
|
||||
|
||||
// Quantizer produces a palette for an image.
|
||||
type Quantizer interface {
|
||||
// Quantize appends up to cap(p) - len(p) colors to p and returns the
|
||||
// updated palette suitable for converting m to a paletted image.
|
||||
Quantize(p color.Palette, m image.Image) color.Palette
|
||||
}
|
57
vendor/golang.org/x/image/draw/go1_9.go
generated
vendored
Normal file
57
vendor/golang.org/x/image/draw/go1_9.go
generated
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build go1.9 go1.8.typealias
|
||||
|
||||
package draw
|
||||
|
||||
import (
|
||||
"image/draw"
|
||||
)
|
||||
|
||||
// We use type aliases (new in Go 1.9) for the exported names from the standard
|
||||
// library's image/draw package. This is not merely syntactic sugar for
|
||||
//
|
||||
// type Drawer draw.Drawer
|
||||
//
|
||||
// as aliasing means that the types in this package, such as draw.Image and
|
||||
// draw.Op, are identical to the corresponding draw.Image and draw.Op types in
|
||||
// the standard library. In comparison, prior to Go 1.9, the code in go1_8.go
|
||||
// defines new types that mimic the old but are different types.
|
||||
//
|
||||
// The package documentation, in draw.go, explicitly gives the intent of this
|
||||
// package:
|
||||
//
|
||||
// This package is a superset of and a drop-in replacement for the
|
||||
// image/draw package in the standard library.
|
||||
//
|
||||
// Drop-in replacement means that I can replace all of my "image/draw" imports
|
||||
// with "golang.org/x/image/draw", to access additional features in this
|
||||
// package, and no further changes are required. That's mostly true, but not
|
||||
// completely true unless we use type aliases.
|
||||
//
|
||||
// Without type aliases, users might need to import both "image/draw" and
|
||||
// "golang.org/x/image/draw" in order to convert from two conceptually
|
||||
// equivalent but different (from the compiler's point of view) types, such as
|
||||
// from one draw.Op type to another draw.Op type, to satisfy some other
|
||||
// interface or function signature.
|
||||
|
||||
// Drawer contains the Draw method.
|
||||
type Drawer = draw.Drawer
|
||||
|
||||
// Image is an image.Image with a Set method to change a single pixel.
|
||||
type Image = draw.Image
|
||||
|
||||
// Op is a Porter-Duff compositing operator.
|
||||
type Op = draw.Op
|
||||
|
||||
const (
|
||||
// Over specifies ``(src in mask) over dst''.
|
||||
Over Op = draw.Over
|
||||
// Src specifies ``src in mask''.
|
||||
Src Op = draw.Src
|
||||
)
|
||||
|
||||
// Quantizer produces a palette for an image.
|
||||
type Quantizer = draw.Quantizer
|
6668
vendor/golang.org/x/image/draw/impl.go
generated
vendored
Normal file
6668
vendor/golang.org/x/image/draw/impl.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
527
vendor/golang.org/x/image/draw/scale.go
generated
vendored
Normal file
527
vendor/golang.org/x/image/draw/scale.go
generated
vendored
Normal file
|
@ -0,0 +1,527 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:generate go run gen.go
|
||||
|
||||
package draw
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/image/math/f64"
|
||||
)
|
||||
|
||||
// Copy copies the part of the source image defined by src and sr and writes
|
||||
// the result of a Porter-Duff composition to the part of the destination image
|
||||
// defined by dst and the translation of sr so that sr.Min translates to dp.
|
||||
func Copy(dst Image, dp image.Point, src image.Image, sr image.Rectangle, op Op, opts *Options) {
|
||||
var o Options
|
||||
if opts != nil {
|
||||
o = *opts
|
||||
}
|
||||
dr := sr.Add(dp.Sub(sr.Min))
|
||||
if o.DstMask == nil {
|
||||
DrawMask(dst, dr, src, sr.Min, o.SrcMask, o.SrcMaskP.Add(sr.Min), op)
|
||||
} else {
|
||||
NearestNeighbor.Scale(dst, dr, src, sr, op, opts)
|
||||
}
|
||||
}
|
||||
|
||||
// Scaler scales the part of the source image defined by src and sr and writes
|
||||
// the result of a Porter-Duff composition to the part of the destination image
|
||||
// defined by dst and dr.
|
||||
//
|
||||
// A Scaler is safe to use concurrently.
|
||||
type Scaler interface {
|
||||
Scale(dst Image, dr image.Rectangle, src image.Image, sr image.Rectangle, op Op, opts *Options)
|
||||
}
|
||||
|
||||
// Transformer transforms the part of the source image defined by src and sr
|
||||
// and writes the result of a Porter-Duff composition to the part of the
|
||||
// destination image defined by dst and the affine transform m applied to sr.
|
||||
//
|
||||
// For example, if m is the matrix
|
||||
//
|
||||
// m00 m01 m02
|
||||
// m10 m11 m12
|
||||
//
|
||||
// then the src-space point (sx, sy) maps to the dst-space point
|
||||
// (m00*sx + m01*sy + m02, m10*sx + m11*sy + m12).
|
||||
//
|
||||
// A Transformer is safe to use concurrently.
|
||||
type Transformer interface {
|
||||
Transform(dst Image, m f64.Aff3, src image.Image, sr image.Rectangle, op Op, opts *Options)
|
||||
}
|
||||
|
||||
// Options are optional parameters to Copy, Scale and Transform.
|
||||
//
|
||||
// A nil *Options means to use the default (zero) values of each field.
|
||||
type Options struct {
|
||||
// Masks limit what parts of the dst image are drawn to and what parts of
|
||||
// the src image are drawn from.
|
||||
//
|
||||
// A dst or src mask image having a zero alpha (transparent) pixel value in
|
||||
// the respective coordinate space means that that dst pixel is entirely
|
||||
// unaffected or that src pixel is considered transparent black. A full
|
||||
// alpha (opaque) value means that the dst pixel is maximally affected or
|
||||
// the src pixel contributes maximally. The default values, nil, are
|
||||
// equivalent to fully opaque, infinitely large mask images.
|
||||
//
|
||||
// The DstMask is otherwise known as a clip mask, and its pixels map 1:1 to
|
||||
// the dst image's pixels. DstMaskP in DstMask space corresponds to
|
||||
// image.Point{X:0, Y:0} in dst space. For example, when limiting
|
||||
// repainting to a 'dirty rectangle', use that image.Rectangle and a zero
|
||||
// image.Point as the DstMask and DstMaskP.
|
||||
//
|
||||
// The SrcMask's pixels map 1:1 to the src image's pixels. SrcMaskP in
|
||||
// SrcMask space corresponds to image.Point{X:0, Y:0} in src space. For
|
||||
// example, when drawing font glyphs in a uniform color, use an
|
||||
// *image.Uniform as the src, and use the glyph atlas image and the
|
||||
// per-glyph offset as SrcMask and SrcMaskP:
|
||||
// Copy(dst, dp, image.NewUniform(color), image.Rect(0, 0, glyphWidth, glyphHeight), &Options{
|
||||
// SrcMask: glyphAtlas,
|
||||
// SrcMaskP: glyphOffset,
|
||||
// })
|
||||
DstMask image.Image
|
||||
DstMaskP image.Point
|
||||
SrcMask image.Image
|
||||
SrcMaskP image.Point
|
||||
|
||||
// TODO: a smooth vs sharp edges option, for arbitrary rotations?
|
||||
}
|
||||
|
||||
// Interpolator is an interpolation algorithm, when dst and src pixels don't
|
||||
// have a 1:1 correspondence.
|
||||
//
|
||||
// Of the interpolators provided by this package:
|
||||
// - NearestNeighbor is fast but usually looks worst.
|
||||
// - CatmullRom is slow but usually looks best.
|
||||
// - ApproxBiLinear has reasonable speed and quality.
|
||||
//
|
||||
// The time taken depends on the size of dr. For kernel interpolators, the
|
||||
// speed also depends on the size of sr, and so are often slower than
|
||||
// non-kernel interpolators, especially when scaling down.
|
||||
type Interpolator interface {
|
||||
Scaler
|
||||
Transformer
|
||||
}
|
||||
|
||||
// Kernel is an interpolator that blends source pixels weighted by a symmetric
|
||||
// kernel function.
|
||||
type Kernel struct {
|
||||
// Support is the kernel support and must be >= 0. At(t) is assumed to be
|
||||
// zero when t >= Support.
|
||||
Support float64
|
||||
// At is the kernel function. It will only be called with t in the
|
||||
// range [0, Support).
|
||||
At func(t float64) float64
|
||||
}
|
||||
|
||||
// Scale implements the Scaler interface.
|
||||
func (q *Kernel) Scale(dst Image, dr image.Rectangle, src image.Image, sr image.Rectangle, op Op, opts *Options) {
|
||||
q.newScaler(dr.Dx(), dr.Dy(), sr.Dx(), sr.Dy(), false).Scale(dst, dr, src, sr, op, opts)
|
||||
}
|
||||
|
||||
// NewScaler returns a Scaler that is optimized for scaling multiple times with
|
||||
// the same fixed destination and source width and height.
|
||||
func (q *Kernel) NewScaler(dw, dh, sw, sh int) Scaler {
|
||||
return q.newScaler(dw, dh, sw, sh, true)
|
||||
}
|
||||
|
||||
func (q *Kernel) newScaler(dw, dh, sw, sh int, usePool bool) Scaler {
|
||||
z := &kernelScaler{
|
||||
kernel: q,
|
||||
dw: int32(dw),
|
||||
dh: int32(dh),
|
||||
sw: int32(sw),
|
||||
sh: int32(sh),
|
||||
horizontal: newDistrib(q, int32(dw), int32(sw)),
|
||||
vertical: newDistrib(q, int32(dh), int32(sh)),
|
||||
}
|
||||
if usePool {
|
||||
z.pool.New = func() interface{} {
|
||||
tmp := z.makeTmpBuf()
|
||||
return &tmp
|
||||
}
|
||||
}
|
||||
return z
|
||||
}
|
||||
|
||||
var (
|
||||
// NearestNeighbor is the nearest neighbor interpolator. It is very fast,
|
||||
// but usually gives very low quality results. When scaling up, the result
|
||||
// will look 'blocky'.
|
||||
NearestNeighbor = Interpolator(nnInterpolator{})
|
||||
|
||||
// ApproxBiLinear is a mixture of the nearest neighbor and bi-linear
|
||||
// interpolators. It is fast, but usually gives medium quality results.
|
||||
//
|
||||
// It implements bi-linear interpolation when upscaling and a bi-linear
|
||||
// blend of the 4 nearest neighbor pixels when downscaling. This yields
|
||||
// nicer quality than nearest neighbor interpolation when upscaling, but
|
||||
// the time taken is independent of the number of source pixels, unlike the
|
||||
// bi-linear interpolator. When downscaling a large image, the performance
|
||||
// difference can be significant.
|
||||
ApproxBiLinear = Interpolator(ablInterpolator{})
|
||||
|
||||
// BiLinear is the tent kernel. It is slow, but usually gives high quality
|
||||
// results.
|
||||
BiLinear = &Kernel{1, func(t float64) float64 {
|
||||
return 1 - t
|
||||
}}
|
||||
|
||||
// CatmullRom is the Catmull-Rom kernel. It is very slow, but usually gives
|
||||
// very high quality results.
|
||||
//
|
||||
// It is an instance of the more general cubic BC-spline kernel with parameters
|
||||
// B=0 and C=0.5. See Mitchell and Netravali, "Reconstruction Filters in
|
||||
// Computer Graphics", Computer Graphics, Vol. 22, No. 4, pp. 221-228.
|
||||
CatmullRom = &Kernel{2, func(t float64) float64 {
|
||||
if t < 1 {
|
||||
return (1.5*t-2.5)*t*t + 1
|
||||
}
|
||||
return ((-0.5*t+2.5)*t-4)*t + 2
|
||||
}}
|
||||
|
||||
// TODO: a Kaiser-Bessel kernel?
|
||||
)
|
||||
|
||||
type nnInterpolator struct{}
|
||||
|
||||
type ablInterpolator struct{}
|
||||
|
||||
type kernelScaler struct {
|
||||
kernel *Kernel
|
||||
dw, dh, sw, sh int32
|
||||
horizontal, vertical distrib
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (z *kernelScaler) makeTmpBuf() [][4]float64 {
|
||||
return make([][4]float64, z.dw*z.sh)
|
||||
}
|
||||
|
||||
// source is a range of contribs, their inverse total weight, and that ITW
|
||||
// divided by 0xffff.
|
||||
type source struct {
|
||||
i, j int32
|
||||
invTotalWeight float64
|
||||
invTotalWeightFFFF float64
|
||||
}
|
||||
|
||||
// contrib is the weight of a column or row.
|
||||
type contrib struct {
|
||||
coord int32
|
||||
weight float64
|
||||
}
|
||||
|
||||
// distrib measures how source pixels are distributed over destination pixels.
|
||||
type distrib struct {
|
||||
// sources are what contribs each column or row in the source image owns,
|
||||
// and the total weight of those contribs.
|
||||
sources []source
|
||||
// contribs are the contributions indexed by sources[s].i and sources[s].j.
|
||||
contribs []contrib
|
||||
}
|
||||
|
||||
// newDistrib returns a distrib that distributes sw source columns (or rows)
|
||||
// over dw destination columns (or rows).
|
||||
func newDistrib(q *Kernel, dw, sw int32) distrib {
|
||||
scale := float64(sw) / float64(dw)
|
||||
halfWidth, kernelArgScale := q.Support, 1.0
|
||||
// When shrinking, broaden the effective kernel support so that we still
|
||||
// visit every source pixel.
|
||||
if scale > 1 {
|
||||
halfWidth *= scale
|
||||
kernelArgScale = 1 / scale
|
||||
}
|
||||
|
||||
// Make the sources slice, one source for each column or row, and temporarily
|
||||
// appropriate its elements' fields so that invTotalWeight is the scaled
|
||||
// coordinate of the source column or row, and i and j are the lower and
|
||||
// upper bounds of the range of destination columns or rows affected by the
|
||||
// source column or row.
|
||||
n, sources := int32(0), make([]source, dw)
|
||||
for x := range sources {
|
||||
center := (float64(x)+0.5)*scale - 0.5
|
||||
i := int32(math.Floor(center - halfWidth))
|
||||
if i < 0 {
|
||||
i = 0
|
||||
}
|
||||
j := int32(math.Ceil(center + halfWidth))
|
||||
if j > sw {
|
||||
j = sw
|
||||
if j < i {
|
||||
j = i
|
||||
}
|
||||
}
|
||||
sources[x] = source{i: i, j: j, invTotalWeight: center}
|
||||
n += j - i
|
||||
}
|
||||
|
||||
contribs := make([]contrib, 0, n)
|
||||
for k, b := range sources {
|
||||
totalWeight := 0.0
|
||||
l := int32(len(contribs))
|
||||
for coord := b.i; coord < b.j; coord++ {
|
||||
t := abs((b.invTotalWeight - float64(coord)) * kernelArgScale)
|
||||
if t >= q.Support {
|
||||
continue
|
||||
}
|
||||
weight := q.At(t)
|
||||
if weight == 0 {
|
||||
continue
|
||||
}
|
||||
totalWeight += weight
|
||||
contribs = append(contribs, contrib{coord, weight})
|
||||
}
|
||||
totalWeight = 1 / totalWeight
|
||||
sources[k] = source{
|
||||
i: l,
|
||||
j: int32(len(contribs)),
|
||||
invTotalWeight: totalWeight,
|
||||
invTotalWeightFFFF: totalWeight / 0xffff,
|
||||
}
|
||||
}
|
||||
|
||||
return distrib{sources, contribs}
|
||||
}
|
||||
|
||||
// abs is like math.Abs, but it doesn't care about negative zero, infinities or
|
||||
// NaNs.
|
||||
func abs(f float64) float64 {
|
||||
if f < 0 {
|
||||
f = -f
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// ftou converts the range [0.0, 1.0] to [0, 0xffff].
|
||||
func ftou(f float64) uint16 {
|
||||
i := int32(0xffff*f + 0.5)
|
||||
if i > 0xffff {
|
||||
return 0xffff
|
||||
}
|
||||
if i > 0 {
|
||||
return uint16(i)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// fffftou converts the range [0.0, 65535.0] to [0, 0xffff].
|
||||
func fffftou(f float64) uint16 {
|
||||
i := int32(f + 0.5)
|
||||
if i > 0xffff {
|
||||
return 0xffff
|
||||
}
|
||||
if i > 0 {
|
||||
return uint16(i)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// invert returns the inverse of m.
|
||||
//
|
||||
// TODO: move this into the f64 package, once we work out the convention for
|
||||
// matrix methods in that package: do they modify the receiver, take a dst
|
||||
// pointer argument, or return a new value?
|
||||
func invert(m *f64.Aff3) f64.Aff3 {
|
||||
m00 := +m[3*1+1]
|
||||
m01 := -m[3*0+1]
|
||||
m02 := +m[3*1+2]*m[3*0+1] - m[3*1+1]*m[3*0+2]
|
||||
m10 := -m[3*1+0]
|
||||
m11 := +m[3*0+0]
|
||||
m12 := +m[3*1+0]*m[3*0+2] - m[3*1+2]*m[3*0+0]
|
||||
|
||||
det := m00*m11 - m10*m01
|
||||
|
||||
return f64.Aff3{
|
||||
m00 / det,
|
||||
m01 / det,
|
||||
m02 / det,
|
||||
m10 / det,
|
||||
m11 / det,
|
||||
m12 / det,
|
||||
}
|
||||
}
|
||||
|
||||
func matMul(p, q *f64.Aff3) f64.Aff3 {
|
||||
return f64.Aff3{
|
||||
p[3*0+0]*q[3*0+0] + p[3*0+1]*q[3*1+0],
|
||||
p[3*0+0]*q[3*0+1] + p[3*0+1]*q[3*1+1],
|
||||
p[3*0+0]*q[3*0+2] + p[3*0+1]*q[3*1+2] + p[3*0+2],
|
||||
p[3*1+0]*q[3*0+0] + p[3*1+1]*q[3*1+0],
|
||||
p[3*1+0]*q[3*0+1] + p[3*1+1]*q[3*1+1],
|
||||
p[3*1+0]*q[3*0+2] + p[3*1+1]*q[3*1+2] + p[3*1+2],
|
||||
}
|
||||
}
|
||||
|
||||
// transformRect returns a rectangle dr that contains sr transformed by s2d.
|
||||
func transformRect(s2d *f64.Aff3, sr *image.Rectangle) (dr image.Rectangle) {
|
||||
ps := [...]image.Point{
|
||||
{sr.Min.X, sr.Min.Y},
|
||||
{sr.Max.X, sr.Min.Y},
|
||||
{sr.Min.X, sr.Max.Y},
|
||||
{sr.Max.X, sr.Max.Y},
|
||||
}
|
||||
for i, p := range ps {
|
||||
sxf := float64(p.X)
|
||||
syf := float64(p.Y)
|
||||
dx := int(math.Floor(s2d[0]*sxf + s2d[1]*syf + s2d[2]))
|
||||
dy := int(math.Floor(s2d[3]*sxf + s2d[4]*syf + s2d[5]))
|
||||
|
||||
// The +1 adjustments below are because an image.Rectangle is inclusive
|
||||
// on the low end but exclusive on the high end.
|
||||
|
||||
if i == 0 {
|
||||
dr = image.Rectangle{
|
||||
Min: image.Point{dx + 0, dy + 0},
|
||||
Max: image.Point{dx + 1, dy + 1},
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if dr.Min.X > dx {
|
||||
dr.Min.X = dx
|
||||
}
|
||||
dx++
|
||||
if dr.Max.X < dx {
|
||||
dr.Max.X = dx
|
||||
}
|
||||
|
||||
if dr.Min.Y > dy {
|
||||
dr.Min.Y = dy
|
||||
}
|
||||
dy++
|
||||
if dr.Max.Y < dy {
|
||||
dr.Max.Y = dy
|
||||
}
|
||||
}
|
||||
return dr
|
||||
}
|
||||
|
||||
func clipAffectedDestRect(adr image.Rectangle, dstMask image.Image, dstMaskP image.Point) (image.Rectangle, image.Image) {
|
||||
if dstMask == nil {
|
||||
return adr, nil
|
||||
}
|
||||
// TODO: enable this fast path once Go 1.5 is released, where an
|
||||
// image.Rectangle implements image.Image.
|
||||
// if r, ok := dstMask.(image.Rectangle); ok {
|
||||
// return adr.Intersect(r.Sub(dstMaskP)), nil
|
||||
// }
|
||||
// TODO: clip to dstMask.Bounds() if the color model implies that out-of-bounds means 0 alpha?
|
||||
return adr, dstMask
|
||||
}
|
||||
|
||||
func transform_Uniform(dst Image, dr, adr image.Rectangle, d2s *f64.Aff3, src *image.Uniform, sr image.Rectangle, bias image.Point, op Op) {
|
||||
switch op {
|
||||
case Over:
|
||||
switch dst := dst.(type) {
|
||||
case *image.RGBA:
|
||||
pr, pg, pb, pa := src.C.RGBA()
|
||||
pa1 := (0xffff - pa) * 0x101
|
||||
|
||||
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
||||
dyf := float64(dr.Min.Y+int(dy)) + 0.5
|
||||
d := dst.PixOffset(dr.Min.X+adr.Min.X, dr.Min.Y+int(dy))
|
||||
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx, d = dx+1, d+4 {
|
||||
dxf := float64(dr.Min.X+int(dx)) + 0.5
|
||||
sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X
|
||||
sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y
|
||||
if !(image.Point{sx0, sy0}).In(sr) {
|
||||
continue
|
||||
}
|
||||
dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*pa1/0xffff + pr) >> 8)
|
||||
dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*pa1/0xffff + pg) >> 8)
|
||||
dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*pa1/0xffff + pb) >> 8)
|
||||
dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*pa1/0xffff + pa) >> 8)
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
pr, pg, pb, pa := src.C.RGBA()
|
||||
pa1 := 0xffff - pa
|
||||
dstColorRGBA64 := &color.RGBA64{}
|
||||
dstColor := color.Color(dstColorRGBA64)
|
||||
|
||||
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
||||
dyf := float64(dr.Min.Y+int(dy)) + 0.5
|
||||
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ {
|
||||
dxf := float64(dr.Min.X+int(dx)) + 0.5
|
||||
sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X
|
||||
sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y
|
||||
if !(image.Point{sx0, sy0}).In(sr) {
|
||||
continue
|
||||
}
|
||||
qr, qg, qb, qa := dst.At(dr.Min.X+int(dx), dr.Min.Y+int(dy)).RGBA()
|
||||
dstColorRGBA64.R = uint16(qr*pa1/0xffff + pr)
|
||||
dstColorRGBA64.G = uint16(qg*pa1/0xffff + pg)
|
||||
dstColorRGBA64.B = uint16(qb*pa1/0xffff + pb)
|
||||
dstColorRGBA64.A = uint16(qa*pa1/0xffff + pa)
|
||||
dst.Set(dr.Min.X+int(dx), dr.Min.Y+int(dy), dstColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case Src:
|
||||
switch dst := dst.(type) {
|
||||
case *image.RGBA:
|
||||
pr, pg, pb, pa := src.C.RGBA()
|
||||
pr8 := uint8(pr >> 8)
|
||||
pg8 := uint8(pg >> 8)
|
||||
pb8 := uint8(pb >> 8)
|
||||
pa8 := uint8(pa >> 8)
|
||||
|
||||
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
||||
dyf := float64(dr.Min.Y+int(dy)) + 0.5
|
||||
d := dst.PixOffset(dr.Min.X+adr.Min.X, dr.Min.Y+int(dy))
|
||||
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx, d = dx+1, d+4 {
|
||||
dxf := float64(dr.Min.X+int(dx)) + 0.5
|
||||
sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X
|
||||
sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y
|
||||
if !(image.Point{sx0, sy0}).In(sr) {
|
||||
continue
|
||||
}
|
||||
dst.Pix[d+0] = pr8
|
||||
dst.Pix[d+1] = pg8
|
||||
dst.Pix[d+2] = pb8
|
||||
dst.Pix[d+3] = pa8
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
pr, pg, pb, pa := src.C.RGBA()
|
||||
dstColorRGBA64 := &color.RGBA64{
|
||||
uint16(pr),
|
||||
uint16(pg),
|
||||
uint16(pb),
|
||||
uint16(pa),
|
||||
}
|
||||
dstColor := color.Color(dstColorRGBA64)
|
||||
|
||||
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
||||
dyf := float64(dr.Min.Y+int(dy)) + 0.5
|
||||
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ {
|
||||
dxf := float64(dr.Min.X+int(dx)) + 0.5
|
||||
sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X
|
||||
sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y
|
||||
if !(image.Point{sx0, sy0}).In(sr) {
|
||||
continue
|
||||
}
|
||||
dst.Set(dr.Min.X+int(dx), dr.Min.Y+int(dy), dstColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func opaque(m image.Image) bool {
|
||||
o, ok := m.(interface {
|
||||
Opaque() bool
|
||||
})
|
||||
return ok && o.Opaque()
|
||||
}
|
37
vendor/golang.org/x/image/math/f64/f64.go
generated
vendored
Normal file
37
vendor/golang.org/x/image/math/f64/f64.go
generated
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package f64 implements float64 vector and matrix types.
|
||||
package f64 // import "golang.org/x/image/math/f64"
|
||||
|
||||
// Vec2 is a 2-element vector.
|
||||
type Vec2 [2]float64
|
||||
|
||||
// Vec3 is a 3-element vector.
|
||||
type Vec3 [3]float64
|
||||
|
||||
// Vec4 is a 4-element vector.
|
||||
type Vec4 [4]float64
|
||||
|
||||
// Mat3 is a 3x3 matrix in row major order.
|
||||
//
|
||||
// m[3*r + c] is the element in the r'th row and c'th column.
|
||||
type Mat3 [9]float64
|
||||
|
||||
// Mat4 is a 4x4 matrix in row major order.
|
||||
//
|
||||
// m[4*r + c] is the element in the r'th row and c'th column.
|
||||
type Mat4 [16]float64
|
||||
|
||||
// Aff3 is a 3x3 affine transformation matrix in row major order, where the
|
||||
// bottom row is implicitly [0 0 1].
|
||||
//
|
||||
// m[3*r + c] is the element in the r'th row and c'th column.
|
||||
type Aff3 [6]float64
|
||||
|
||||
// Aff4 is a 4x4 affine transformation matrix in row major order, where the
|
||||
// bottom row is implicitly [0 0 0 1].
|
||||
//
|
||||
// m[4*r + c] is the element in the r'th row and c'th column.
|
||||
type Aff4 [12]float64
|
24
vendor/vendor.json
vendored
24
vendor/vendor.json
vendored
|
@ -346,6 +346,18 @@
|
|||
"revision": "12b6a0f7b3e676d459a9480e75df7efe576cfcb2",
|
||||
"revisionTime": "2017-09-08T20:30:58Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "5Z44bktwBk/azl6X7I/U14GaAes=",
|
||||
"path": "github.com/muesli/smartcrop",
|
||||
"revision": "fe851226066d6f54f7f6ed9c0fbec82f11149618",
|
||||
"revisionTime": "2017-09-02T21:15:18Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "r5eQHkttko6kxroDEENXbmXKrSs=",
|
||||
"path": "github.com/nfnt/resize",
|
||||
"revision": "891127d8d1b52734debe1b3c3d7e747502b6c366",
|
||||
"revisionTime": "2016-07-24T20:39:20Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "GfnXm54E98jxQJMXPZz0LbPVaRc=",
|
||||
"path": "github.com/peterbourgon/diskv",
|
||||
|
@ -376,6 +388,18 @@
|
|||
"revision": "426cfd8eeb6e08ab1932954e09e3c2cb2bc6e36d",
|
||||
"revisionTime": "2017-05-14T06:33:48Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "WZqFyWo6r8KZodsU0doRvel37F0=",
|
||||
"path": "golang.org/x/image/draw",
|
||||
"revision": "e20db36d77bd0cb36cea8fe49d5c37d82d21591f",
|
||||
"revisionTime": "2017-09-04T00:25:37Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "o7VkCGBiKM5HXzVzrIci1YDlpvc=",
|
||||
"path": "golang.org/x/image/math/f64",
|
||||
"revision": "e20db36d77bd0cb36cea8fe49d5c37d82d21591f",
|
||||
"revisionTime": "2017-09-04T00:25:37Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "zdekzNuFGSoxAZ8cURGsrhBObZs=",
|
||||
"path": "golang.org/x/image/riff",
|
||||
|
|
Loading…
Reference in a new issue