Skip to content

Commit

Permalink
minor changes to code style and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
disintegration committed Oct 5, 2018
1 parent 95ce21c commit e7b6d79
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 71 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Grigory Dryapak
Copyright (c) 2014-2018 Grigory Dryapak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[![GoDoc](https://godoc.org/github.com/disintegration/gift?status.svg)](https://godoc.org/github.com/disintegration/gift)
[![Build Status](https://travis-ci.org/disintegration/gift.svg?branch=master)](https://travis-ci.org/disintegration/gift)
[![Coverage Status](https://coveralls.io/repos/github/disintegration/gift/badge.svg?branch=master)](https://coveralls.io/github/disintegration/gift?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/disintegration/gift)](https://goreportcard.com/report/github.com/disintegration/gift)


*Package gift provides a set of useful image processing filters.*

Expand All @@ -22,19 +24,19 @@ http://godoc.org/github.com/disintegration/gift
### QUICK START

```go
// 1. Create a new GIFT filter list and add some filters:
// 1. Create a new filter list and add some filters.
g := gift.New(
gift.Resize(800, 0, gift.LanczosResampling),
gift.UnsharpMask(1, 1, 0),
)

// 2. Create a new image of the corresponding size.
// dst is a new target image, src is the original image
// dst is a new target image, src is the original image.
dst := image.NewRGBA(g.Bounds(src.Bounds()))

// 3. Use Draw func to apply the filters to src and store the result in dst:
// 3. Use the Draw func to apply the filters to src and store the result in dst.
g.Draw(dst, src)
```
```

### USAGE

Expand All @@ -47,10 +49,10 @@ g := gift.New(
```
Filters also can be added using the `Add` method:
```go
g.Add(GaussianBlur(2))
g.Add(GaussianBlur(2))
```

The `Bounds` method takes the bounds of the source image and returns appropriate bounds for the destination image to fit the result (for example, after using `Resize` or `Rotate` filters).
The `Bounds` method takes the bounds of the source image and returns appropriate bounds for the destination image to fit the result (for example, after using `Resize` or `Rotate` filters).

```go
dst := image.NewRGBA(g.Bounds(src.Bounds()))
Expand Down Expand Up @@ -100,7 +102,7 @@ gift.New().DrawAt(dstImage, fgImage, image.Pt(100, 100), gift.OverOperator)
- Rotate90()
- Transpose()
- Transverse()

+ Adjustments & effects

- Brightness(percentage float32)
Expand Down Expand Up @@ -133,7 +135,7 @@ gift.New().DrawAt(dstImage, fgImage, image.Pt(100, 100), gift.OverOperator)

The original image:

![](testdata/src.png)
![](testdata/src.png)

Resulting images after applying some of the filters:

Expand Down Expand Up @@ -205,7 +207,7 @@ func main() {
g = g0 + 0.1 // shift the green channel by 0.1
b = 0 // set the blue channel to 0
a = a0 // preserve the alpha channel
return
return r, g, b, a
},
),
"convolution_emboss": gift.Convolution(
Expand Down
10 changes: 5 additions & 5 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func convertHSLToRGB(h, s, l float32) (float32, float32, float32) {
return l, l, l
}

_v := func(p, q, t float32) float32 {
hueToRGB := func(p, q, t float32) float32 {
if t < 0 {
t++
}
Expand All @@ -319,9 +319,9 @@ func convertHSLToRGB(h, s, l float32) (float32, float32, float32) {
}
p = 2*l - q

r := _v(p, q, h+1/3.0)
g := _v(p, q, h)
b := _v(p, q, h-1/3.0)
r := hueToRGB(p, q, h+1/3.0)
g := hueToRGB(p, q, h)
b := hueToRGB(p, q, h-1/3.0)

return r, g, b
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func Threshold(percentage float32) Filter {
// g = g0 + 0.1 // shift the green channel by 0.1
// b = 0 // set the blue channel to 0
// a = a0 // preserve the alpha channel
// return
// return r, g, b, a
// },
// ),
// )
Expand Down
15 changes: 8 additions & 7 deletions convolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (p *convolutionFilter) Draw(dst draw.Image, src image.Image, options *Optio
pixSetter := newPixelSetter(dst)

parallelize(options.Parallelization, srcb.Min.Y, srcb.Max.Y, func(start, stop int) {
// init temp rows
// Init temporary rows.
starty := start
rows := make([][]pixel, ksize)
for i := 0; i < ksize; i++ {
Expand All @@ -122,7 +122,7 @@ func (p *convolutionFilter) Draw(dst draw.Image, src image.Image, options *Optio
}

for y := start; y < stop; y++ {
// calculate dst row
// Calculate dst row.
for x := srcb.Min.X; x < srcb.Max.X; x++ {
var r, g, b, a float32
for _, w := range weights {
Expand Down Expand Up @@ -165,7 +165,7 @@ func (p *convolutionFilter) Draw(dst draw.Image, src image.Image, options *Optio
pixSetter.setPixel(dstb.Min.X+x-srcb.Min.X, dstb.Min.Y+y-srcb.Min.Y, pixel{r, g, b, a})
}

// rotate temp rows
// Rotate temporary rows.
if y < stop-1 {
tmprow := rows[0]
for i := 0; i < ksize-1; i++ {
Expand Down Expand Up @@ -216,7 +216,8 @@ func Convolution(kernel []float32, normalize, alpha, abs bool, delta float32) Fi
}
}

// prepare pixel weights using convolution kernel. weights equal to 0 are excluded
// prepareConvolutionWeights1d prepares pixel weights using a convolution kernel.
// Weights equal to 0 are excluded.
func prepareConvolutionWeights1d(kernel []float32) (int, []uweight) {
size := len(kernel)
if size%2 == 0 {
Expand All @@ -239,7 +240,7 @@ func prepareConvolutionWeights1d(kernel []float32) (int, []uweight) {
return size, weights
}

// calculate pixels for one line according to weights
// convolveLine convolves a single line of pixels according to the given weights.
func convolveLine(dstBuf []pixel, srcBuf []pixel, weights []uweight) {
max := len(srcBuf) - 1
if max < 0 {
Expand Down Expand Up @@ -270,7 +271,7 @@ func convolveLine(dstBuf []pixel, srcBuf []pixel, weights []uweight) {
}
}

// fast vertical 1d convolution
// convolve1dv performs a fast vertical 1d convolution.
func convolve1dv(dst draw.Image, src image.Image, kernel []float32, options *Options) {
srcb := src.Bounds()
dstb := dst.Bounds()
Expand All @@ -295,7 +296,7 @@ func convolve1dv(dst draw.Image, src image.Image, kernel []float32, options *Opt
})
}

// fast horizontal 1d convolution
// convolve1dh performs afast horizontal 1d convolution.
func convolve1dh(dst draw.Image, src image.Image, kernel []float32, options *Options) {
srcb := src.Bounds()
dstb := dst.Bounds()
Expand Down
4 changes: 0 additions & 4 deletions convolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

func TestConvolution(t *testing.T) {

testData := []struct {
desc string
kernel []float32
Expand Down Expand Up @@ -229,7 +228,6 @@ func TestConvolution(t *testing.T) {
}

func TestGaussianBlur(t *testing.T) {

testData := []struct {
desc string
sigma float32
Expand Down Expand Up @@ -320,7 +318,6 @@ func TestGaussianBlur(t *testing.T) {
}

func TestUnsharpMask(t *testing.T) {

testData := []struct {
desc string
sigma, amount, threshold float32
Expand Down Expand Up @@ -411,7 +408,6 @@ func TestUnsharpMask(t *testing.T) {
}

func TestMean(t *testing.T) {

testData := []struct {
desc string
ksize int
Expand Down
6 changes: 3 additions & 3 deletions effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (p *pixelateFilter) Draw(dst draw.Image, src image.Image, options *Options)
parallelize(options.Parallelization, 0, numBlocksY, func(start, stop int) {
for by := start; by < stop; by++ {
for bx := 0; bx < numBlocksX; bx++ {
// calculate the block bounds
// Calculate the block bounds.
bb := image.Rect(bx*blockSize, by*blockSize, (bx+1)*blockSize, (by+1)*blockSize)
bbSrc := bb.Add(srcb.Min).Intersect(srcb)
bbDst := bbSrc.Sub(srcb.Min).Add(dstb.Min).Intersect(dstb)

// calculate average color of the block
// Calculate the average color of the block.
var r, g, b, a float32
var cnt float32
for y := bbSrc.Min.Y; y < bbSrc.Max.Y; y++ {
Expand All @@ -68,7 +68,7 @@ func (p *pixelateFilter) Draw(dst draw.Image, src image.Image, options *Options)
a /= cnt
}

// set the calculated color for all pixels in the block
// Set the calculated color for all pixels in the block.
for y := bbDst.Min.Y; y < bbDst.Max.Y; y++ {
for x := bbDst.Min.X; x < bbDst.Max.X; x++ {
pixSetter.setPixel(x, y, pixel{r, g, b, a})
Expand Down
16 changes: 6 additions & 10 deletions gift.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
/*
Package gift provides a set of useful image processing filters.
Basic usage:
// 1. Create a new GIFT and add some filters:
// 1. Create a new filter list and add some filters.
g := gift.New(
gift.Resize(800, 0, gift.LanczosResampling),
gift.UnsharpMask(1, 1, 0),
)
// 2. Create a new image of the corresponding size.
// dst is a new target image, src is the original image
// dst is a new target image, src is the original image.
dst := image.NewRGBA(g.Bounds(src.Bounds()))
// 3. Use Draw func to apply the filters to src and store the result in dst:
// 3. Use the Draw func to apply the filters to src and store the result in dst.
g.Draw(dst, src)
*/
Expand Down Expand Up @@ -45,21 +41,21 @@ var defaultOptions = Options{
Parallelization: true,
}

// GIFT implements a list of filters that can be applied to an image at once.
// GIFT is a list of image processing filters.
type GIFT struct {
Filters []Filter
Options Options
}

// New creates a new instance of the filter toolkit and initializes it with the given list of filters.
// New creates a new filter list and initializes it with the given slice of filters.
func New(filters ...Filter) *GIFT {
return &GIFT{
Filters: filters,
Options: defaultOptions,
}
}

// SetParallelization enables or disables faster image processing using parallel goroutines.
// SetParallelization enables or disables the image processing parallelization.
// Parallelization is enabled by default.
func (g *GIFT) SetParallelization(isEnabled bool) {
g.Options.Parallelization = isEnabled
Expand Down
4 changes: 2 additions & 2 deletions gift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func TestGolden(t *testing.T) {
g = g0 + 0.1
b = 0
a = a0
return
return r, g, b, a
},
),
"convolution_emboss": Convolution(
Expand Down Expand Up @@ -644,7 +644,7 @@ func BenchmarkFilter(b *testing.B) {
g = g0 + 0.1
b = 0
a = a0
return
return r, g, b, a
},
)},
{"ColorspaceSRGBToLinear", ColorspaceSRGBToLinear()},
Expand Down
4 changes: 2 additions & 2 deletions rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *rankFilter) Draw(dst draw.Image, src image.Image, options *Options) {
}

for y := start; y < stop; y++ {
// init buffer
// Init buffer.
pxbuf = pxbuf[:0]
for i := srcb.Min.X - kradius; i <= srcb.Min.X+kradius; i++ {
for j := y - kradius; j <= y+kradius; j++ {
Expand Down Expand Up @@ -163,7 +163,7 @@ func (p *rankFilter) Draw(dst draw.Image, src image.Image, options *Options) {

pixSetter.setPixel(dstb.Min.X+x-srcb.Min.X, dstb.Min.Y+y-srcb.Min.Y, pixel{r, g, b, a})

// rotate buffer columns
// Rotate buffer columns.
if x < srcb.Max.X-1 {
copy(pxbuf[0:], pxbuf[ksize:])
pxbuf = pxbuf[0 : ksize*(ksize-1)]
Expand Down
5 changes: 3 additions & 2 deletions resize_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gift

import (
"bytes"
"image"
"testing"
)
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestResize(t *testing.T) {
if img1.Bounds().Size() != img1Exp.Bounds().Size() {
t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size())
}
if !comparePix(img1Exp.Pix, img1.Pix) {
if !bytes.Equal(img1Exp.Pix, img1.Pix) {
t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix)
}

Expand All @@ -84,7 +85,7 @@ func TestResize(t *testing.T) {
if img1.Bounds().Size() != img1Exp.Bounds().Size() {
t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size())
}
if !comparePix(img1Exp.Pix, img1.Pix) {
if !bytes.Equal(img1Exp.Pix, img1.Pix) {
t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix)
}

Expand Down
Loading

0 comments on commit e7b6d79

Please sign in to comment.