-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed from draw to native function
- Loading branch information
Showing
2 changed files
with
54 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,43 @@ | ||
package canvas | ||
|
||
import ( | ||
"golang.org/x/image/draw" | ||
"image" | ||
"math" | ||
) | ||
|
||
type InterpolationFunction int | ||
func Resize(img image.Image, newWidth int, newHeight int, intFunc string) image.Image { | ||
|
||
// InterpolationFunction constants | ||
const ( | ||
Bilinear InterpolationFunction = iota | ||
CatmullRom | ||
) | ||
|
||
// kernal, returns an InterpolationFunctions taps and kernel. | ||
func (i InterpolationFunction) kernel() *draw.Kernel { | ||
switch i { | ||
case Bilinear: | ||
return draw.BiLinear | ||
case CatmullRom: | ||
return draw.CatmullRom | ||
if newWidth == 0 && newHeight == 0 { | ||
newWidth = img.Bounds().Dx() | ||
newHeight = img.Bounds().Dy() | ||
} else if newWidth == 0 && newHeight != 0 { | ||
newWidth = img.Bounds().Dx() / img.Bounds().Dy() * newHeight | ||
} else if newWidth != 0 && newHeight == 0 { | ||
newHeight = img.Bounds().Dy() / img.Bounds().Dx() * newWidth | ||
} | ||
// perform resizing | ||
switch intFunc { | ||
case "NearestNeighbor": | ||
return ScaleNearestNeighbor(img, newWidth, newHeight) | ||
default: | ||
return draw.CatmullRom | ||
return ScaleNearestNeighbor(img, newWidth, newHeight) | ||
} | ||
} | ||
|
||
func Resize(img image.Image, width int, height int, intFunc InterpolationFunction) (image.Image, error) { | ||
|
||
if width == 0 && height == 0 { | ||
width = img.Bounds().Dx() | ||
height = img.Bounds().Dy() | ||
} else if width == 0 && height != 0 { | ||
width = img.Bounds().Dx() / img.Bounds().Dy() * height | ||
} else if width != 0 && height == 0 { | ||
height = img.Bounds().Dy() / img.Bounds().Dx() * width | ||
func ScaleNearestNeighbor(img image.Image, newWidth int, newHeight int) image.Image { | ||
targetRect := image.Rect(0, 0, newWidth, newHeight) | ||
targetImg := image.NewNRGBA(targetRect) | ||
width := img.Bounds().Dx() | ||
height := img.Bounds().Dy() | ||
for x := 0; x <= width; x++ { | ||
for y := 0; y <= height; y++ { | ||
srcX := int(math.Round(float64(x) / float64(newWidth) * float64(width))) | ||
srcY := int(math.Round(float64(y) / float64(newHeight) * float64(height))) | ||
srcX = int(math.Min(float64(srcX), float64(width-1))) | ||
srcX = int(math.Min(float64(srcY), float64(height-1))) | ||
tarPixel := img.At(srcX, srcY) | ||
targetImg.Set(x, y, tarPixel) | ||
} | ||
} | ||
// new size of image | ||
dstRect := image.Rect(0, 0, width, height) | ||
// perform resizing | ||
dstImg := image.NewRGBA(dstRect) | ||
intFunc.kernel().Scale(dstImg, dstRect, img, img.Bounds(), draw.Over, nil) | ||
|
||
return dstImg, nil | ||
return targetImg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters