mirror of
https://github.com/esimov/caire.git
synced 2025-10-06 00:57:16 +08:00
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package caire
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"os"
|
|
"io"
|
|
)
|
|
|
|
// Processor options
|
|
type Processor struct {
|
|
BlurRadius int
|
|
SobelThreshold int
|
|
}
|
|
|
|
// Process : Convert image
|
|
func (p *Processor) Process(file io.Reader, output string) (*os.File, error) {
|
|
src, _, err := image.Decode(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
img := imgToNRGBA(src)
|
|
sobel := SobelFilter(img, float64(p.SobelThreshold))
|
|
fq, err := Process(img, sobel, output, p.SobelThreshold, p.BlurRadius)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fq, nil
|
|
}
|
|
|
|
// Converts any image type to *image.NRGBA with min-point at (0, 0).
|
|
func imgToNRGBA(img image.Image) *image.NRGBA {
|
|
srcBounds := img.Bounds()
|
|
if srcBounds.Min.X == 0 && srcBounds.Min.Y == 0 {
|
|
if src0, ok := img.(*image.NRGBA); ok {
|
|
return src0
|
|
}
|
|
}
|
|
srcMinX := srcBounds.Min.X
|
|
srcMinY := srcBounds.Min.Y
|
|
|
|
dstBounds := srcBounds.Sub(srcBounds.Min)
|
|
dstW := dstBounds.Dx()
|
|
dstH := dstBounds.Dy()
|
|
dst := image.NewNRGBA(dstBounds)
|
|
|
|
switch src := img.(type) {
|
|
case *image.NRGBA:
|
|
rowSize := srcBounds.Dx() * 4
|
|
for dstY := 0; dstY < dstH; dstY++ {
|
|
di := dst.PixOffset(0, dstY)
|
|
si := src.PixOffset(srcMinX, srcMinY+dstY)
|
|
for dstX := 0; dstX < dstW; dstX++ {
|
|
copy(dst.Pix[di:di+rowSize], src.Pix[si:si+rowSize])
|
|
}
|
|
}
|
|
case *image.YCbCr:
|
|
for dstY := 0; dstY < dstH; dstY++ {
|
|
di := dst.PixOffset(0, dstY)
|
|
for dstX := 0; dstX < dstW; dstX++ {
|
|
srcX := srcMinX + dstX
|
|
srcY := srcMinY + dstY
|
|
siy := src.YOffset(srcX, srcY)
|
|
sic := src.COffset(srcX, srcY)
|
|
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
|
|
dst.Pix[di+0] = r
|
|
dst.Pix[di+1] = g
|
|
dst.Pix[di+2] = b
|
|
dst.Pix[di+3] = 0xff
|
|
di += 4
|
|
}
|
|
}
|
|
default:
|
|
for dstY := 0; dstY < dstH; dstY++ {
|
|
di := dst.PixOffset(0, dstY)
|
|
for dstX := 0; dstX < dstW; dstX++ {
|
|
c := color.NRGBAModel.Convert(img.At(srcMinX+dstX, srcMinY+dstY)).(color.NRGBA)
|
|
dst.Pix[di+0] = c.R
|
|
dst.Pix[di+1] = c.G
|
|
dst.Pix[di+2] = c.B
|
|
dst.Pix[di+3] = c.A
|
|
di += 4
|
|
}
|
|
}
|
|
}
|
|
return dst
|
|
}
|