Skip to content

Commit

Permalink
Merge a44fc15 into acabd83
Browse files Browse the repository at this point in the history
  • Loading branch information
yef-ksh committed Jan 23, 2020
2 parents acabd83 + a44fc15 commit 9eb42b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
16 changes: 16 additions & 0 deletions adjust.go
Expand Up @@ -62,6 +62,10 @@ func Invert(img image.Image) *image.NRGBA {
// dstImage = imaging.AdjustSaturation(srcImage, -10) // Decrease image saturation by 10%.
//
func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
if percentage == 0 {
return Clone(img)
}

percentage = math.Min(math.Max(percentage, -100), 100)
multiplier := 1 + percentage/100

Expand All @@ -86,6 +90,10 @@ func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
// dstImage = imaging.AdjustContrast(srcImage, 20) // Increase image contrast by 20%.
//
func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
if percentage == 0 {
return Clone(img)
}

percentage = math.Min(math.Max(percentage, -100.0), 100.0)
lut := make([]uint8, 256)

Expand Down Expand Up @@ -114,6 +122,10 @@ func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
// dstImage = imaging.AdjustBrightness(srcImage, 10) // Increase image brightness by 10%.
//
func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
if percentage == 0 {
return Clone(img)
}

percentage = math.Min(math.Max(percentage, -100.0), 100.0)
lut := make([]uint8, 256)

Expand All @@ -134,6 +146,10 @@ func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
// dstImage = imaging.AdjustGamma(srcImage, 0.7)
//
func AdjustGamma(img image.Image, gamma float64) *image.NRGBA {
if gamma == 1 {
return Clone(img)
}

e := 1.0 / math.Max(gamma, 0.0001)
lut := make([]uint8, 256)

Expand Down
11 changes: 6 additions & 5 deletions resize.go
Expand Up @@ -87,21 +87,22 @@ func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NR
dstH = int(math.Max(1.0, math.Floor(tmpH+0.5)))
}

if srcW == dstW && srcH == dstH {
return Clone(img)
}

if filter.Support <= 0 {
// Nearest-neighbor special case.
return resizeNearest(img, dstW, dstH)
}

if srcW != dstW && srcH != dstH {
return resizeVertical(resizeHorizontal(img, dstW, filter), dstH, filter)
}
if srcW != dstW {
} else if srcW != dstW {
return resizeHorizontal(img, dstW, filter)
}
if srcH != dstH {
} else {
return resizeVertical(img, dstH, filter)
}
return Clone(img)
}

func resizeHorizontal(img image.Image, width int, filter ResampleFilter) *image.NRGBA {
Expand Down
6 changes: 6 additions & 0 deletions tools.go
Expand Up @@ -95,7 +95,10 @@ func Crop(img image.Image, rect image.Rectangle) *image.NRGBA {
r := rect.Intersect(img.Bounds()).Sub(img.Bounds().Min)
if r.Empty() {
return &image.NRGBA{}
} else if r.Eq(img.Bounds().Sub(img.Bounds().Min)) {
return Clone(img)
}

src := newScanner(img)
dst := image.NewNRGBA(image.Rect(0, 0, r.Dx(), r.Dy()))
rowSize := r.Dx() * 4
Expand Down Expand Up @@ -132,7 +135,10 @@ func Paste(background, img image.Image, pos image.Point) *image.NRGBA {
interRect := pasteRect.Intersect(dst.Bounds())
if interRect.Empty() {
return dst
} else if interRect.Eq(dst.Bounds()) {
return Clone(img)
}

src := newScanner(img)
parallel(interRect.Min.Y, interRect.Max.Y, func(ys <-chan int) {
for y := range ys {
Expand Down
53 changes: 53 additions & 0 deletions tools_test.go
Expand Up @@ -328,6 +328,28 @@ func TestCrop(t *testing.T) {
r image.Rectangle
want *image.NRGBA
}{
{
"Crop 2x3 2x3",
&image.NRGBA{
Rect: image.Rect(-1, -1, 1, 2),
Stride: 2 * 4,
Pix: []uint8{
0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
},
},
image.Rect(-1, -1, 1, 2),
&image.NRGBA{
Rect: image.Rect(0, 0, 2, 3),
Stride: 2 * 4,
Pix: []uint8{
0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
},
},
},
{
"Crop 2x3 2x1",
&image.NRGBA{
Expand Down Expand Up @@ -768,6 +790,37 @@ func TestPaste(t *testing.T) {
p image.Point
want *image.NRGBA
}{
{
"Paste 2x3 2x3",
&image.NRGBA{
Rect: image.Rect(0, 0, 2, 3),
Stride: 2 * 4,
Pix: []uint8{
0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
},
},
&image.NRGBA{
Rect: image.Rect(-1, -1, 1, 2),
Stride: 2 * 4,
Pix: []uint8{
0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
},
},
image.Pt(0, 0),
&image.NRGBA{
Rect: image.Rect(0, 0, 2, 3),
Stride: 2 * 4,
Pix: []uint8{
0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
},
},
},
{
"Paste 2x3 2x1",
&image.NRGBA{
Expand Down

0 comments on commit 9eb42b6

Please sign in to comment.