Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions carver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image/draw"
"math"

cimage "github.com/esimov/caire/image"
"github.com/esimov/caire/utils"
pigo "github.com/esimov/pigo/core"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func (c *Carver) set(x, y int, px float64) {
// with the minimum pixel value of the neighboring pixels from the previous row.
func (c *Carver) ComputeSeams(p *Processor, img *image.NRGBA) (*image.NRGBA, error) {
width, height := img.Bounds().Dx(), img.Bounds().Dy()
sobel := c.SobelDetector(img, float64(p.SobelThreshold))
sobel := cimage.SobelDetector(img, float64(p.SobelThreshold))

dets := []pigo.Detection{}

Expand All @@ -81,7 +82,7 @@ func (c *Carver) ComputeSeams(p *Processor, img *image.NRGBA) (*image.NRGBA, err
minSize := float64(utils.Min(width, height)) * ratio / 3

// Transform the image to pixel array.
pixels := rgbToGrayscale(img)
pixels := cimage.RgbToGrayscale(img)

cParams := pigo.CascadeParams{
MinSize: int(minSize),
Expand Down Expand Up @@ -206,7 +207,7 @@ func (c *Carver) ComputeSeams(p *Processor, img *image.NRGBA) (*image.NRGBA, err
var srcImg *image.NRGBA
if p.BlurRadius > 0 {
srcImg = image.NewNRGBA(img.Bounds())
err := Stackblur(srcImg, sobel, uint32(p.BlurRadius))
err := cimage.Stackblur(srcImg, sobel, uint32(p.BlurRadius))
if err != nil {
return nil, fmt.Errorf("error bluring the image: %w", err)
}
Expand Down
79 changes: 40 additions & 39 deletions carver_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package caire

import (
"image"
"os"
"path/filepath"
"testing"
)

func Benchmark_Carver(b *testing.B) {
sampleImg := filepath.Join("./testdata", "sample.jpg")
f, err := os.Open(sampleImg)
if err != nil {
b.Fatalf("could not load sample image: %v", err)
}
defer f.Close()

src, _, err := image.Decode(f)
if err != nil {
b.Fatalf("error decoding image: %v", err)
}
b.ResetTimer()

img := imgToNRGBA(src)

width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c := NewCarver(width, height)

for i := 0; i < b.N; i++ {
_, err := c.ComputeSeams(p, img)
if err != nil {
b.FailNow()
}

seams := c.FindLowestEnergySeams(p)
img = c.RemoveSeam(img, seams, p.Debug)
}

}
package caire

import (
"image"
"os"
"path/filepath"
"testing"

cimage "github.com/esimov/caire/image"
)

func Benchmark_Carver(b *testing.B) {
sampleImg := filepath.Join("./testdata", "sample.jpg")
f, err := os.Open(sampleImg)
if err != nil {
b.Fatalf("could not load sample image: %v", err)
}
defer f.Close()

src, _, err := image.Decode(f)
if err != nil {
b.Fatalf("error decoding image: %v", err)
}

img := cimage.ImgToNRGBA(src)

width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c := NewCarver(width, height)

for b.Loop() {
_, err := c.ComputeSeams(p, img)
if err != nil {
b.FailNow()
}

seams := c.FindLowestEnergySeams(p)
img = c.RemoveSeam(img, seams, p.Debug)
}

}
22 changes: 11 additions & 11 deletions carver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"testing"

cimage "github.com/esimov/caire/image"
"github.com/esimov/caire/utils"
pigo "github.com/esimov/pigo/core"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestCarver_DetectVerticalEnergySeam(t *testing.T) {
}

var c = NewCarver(dx, dy)
img = rotateImage90(img)
img = cimage.RotateImage90(img)
for x := 0; x < imgHeight; x++ {
width, height := img.Bounds().Max.X, img.Bounds().Max.Y

Expand Down Expand Up @@ -279,11 +280,11 @@ func TestCarver_ShouldDetectFace(t *testing.T) {
if err != nil {
t.Fatalf("error decoding image: %v", err)
}
img := imgToNRGBA(src)
img := cimage.ImgToNRGBA(src)
dx, dy := img.Bounds().Max.X, img.Bounds().Max.Y

// Transform the image to a pixel array.
pixels := rgbToGrayscale(img)
pixels := cimage.RgbToGrayscale(img)

cParams := pigo.CascadeParams{
MinSize: 100,
Expand Down Expand Up @@ -329,16 +330,16 @@ func TestCarver_ShouldNotRemoveFaceZone(t *testing.T) {
if err != nil {
t.Fatalf("error decoding image: %v", err)
}
img := imgToNRGBA(src)
img := cimage.ImgToNRGBA(src)
dx, dy := img.Bounds().Max.X, img.Bounds().Max.Y

c := NewCarver(dx, dy)
// Transform the image to a pixel array.
pixels := rgbToGrayscale(img)
pixels := cimage.RgbToGrayscale(img)

sobel := c.SobelDetector(img, float64(p.SobelThreshold))
sobel := cimage.SobelDetector(img, float64(p.SobelThreshold))

err = Stackblur(img, sobel, uint32(p.BlurRadius))
err = cimage.Stackblur(img, sobel, uint32(p.BlurRadius))
assert.NoError(t, err)

cParams := pigo.CascadeParams{
Expand Down Expand Up @@ -410,11 +411,11 @@ func TestCarver_ShouldNotResizeWithFaceDistorsion(t *testing.T) {
if err != nil {
t.Fatalf("error decoding image: %v", err)
}
img := imgToNRGBA(src)
img := cimage.ImgToNRGBA(src)
dx, dy := img.Bounds().Max.X, img.Bounds().Max.Y

// Transform the image to a pixel array.
pixels := rgbToGrayscale(img)
pixels := cimage.RgbToGrayscale(img)
cParams := pigo.CascadeParams{
MinSize: 100,
MaxSize: utils.Max(dx, dy),
Expand Down Expand Up @@ -600,8 +601,7 @@ func TestCarver_SobelShouldDetectEdgesInEveryColorChannel(t *testing.T) {
draw.Draw(img, image.Rect(0, 0, imgWidth/2, imgHeight), green, image.Point{}, draw.Src)
draw.Draw(img, image.Rect(imgWidth/2, 0, imgWidth, imgHeight), blue, image.Point{}, draw.Src)

c := NewCarver(imgWidth, imgHeight)
sobel := c.SobelDetector(img, 0)
sobel := cimage.SobelDetector(img, 0)

var energy int
for i := 0; i < len(sobel.Pix); i += 4 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/caire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
log.SetFlags(0)

flag.Usage = func() {
fmt.Fprintf(os.Stderr, fmt.Sprintf(HelpBanner, Version))
fmt.Fprintf(os.Stderr, "%s", fmt.Sprintf(HelpBanner, Version))
flag.PrintDefaults()
}
flag.Parse()
Expand Down
14 changes: 8 additions & 6 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"slices"

cimage "github.com/esimov/caire/image"
"github.com/esimov/caire/utils"
"golang.org/x/term"
)
Expand All @@ -41,6 +42,7 @@ type result struct {
err error
}

// Resize implements the interface method with the same name in the Processor struct.
func Resize(s SeamCarver, img *image.NRGBA) (image.Image, error) {
return s.Resize(img)
}
Expand Down Expand Up @@ -156,13 +158,13 @@ func (p *Processor) Execute(img *Image) {
}

if err = <-errc; err != nil {
fmt.Fprintf(os.Stderr, utils.DecorateText(err.Error(), utils.ErrorMessage))
fmt.Fprintf(os.Stderr, "%s", utils.DecorateText(err.Error(), utils.ErrorMessage))
}

case mode.IsRegular() || mode&os.ModeNamedPipe != 0: // check for regular files or pipe names
ext := filepath.Ext(img.Dst)
if !slices.Contains(validExtensions, ext) && img.Dst != img.PipeName {
log.Fatalf(utils.DecorateText(fmt.Sprintf("%v file type not supported", ext), utils.ErrorMessage))
log.Fatalf("%s", utils.DecorateText(fmt.Sprintf("%v file type not supported", ext), utils.ErrorMessage))
}

err = img.process(p, img.Src, img.Dst)
Expand Down Expand Up @@ -258,20 +260,20 @@ func (img *Image) process(p *Processor, in, out string) error {
}()

if len(img.MaskPath) > 0 {
mask, err := decodeImg(img.MaskPath)
mask, err := cimage.DecodeContent(img.MaskPath)
if err != nil {
return fmt.Errorf("cannot decode image: %w", err)
}
p.Mask = dither(imgToNRGBA(mask))
p.Mask = cimage.Dither(mask)
p.DebugMask = p.Mask
}

if len(img.RMaskPath) > 0 {
rmask, err := decodeImg(img.RMaskPath)
rmask, err := cimage.DecodeContent(img.RMaskPath)
if err != nil {
return fmt.Errorf("cannot decode image: %w", err)
}
p.RMask = dither(imgToNRGBA(rmask))
p.RMask = cimage.Dither(rmask)
p.DebugMask = p.RMask
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/esimov/caire

go 1.22
go 1.27

require (
gioui.org v0.8.0
Expand Down
3 changes: 2 additions & 1 deletion gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
cimage "github.com/esimov/caire/image"
"github.com/esimov/caire/imop"
"github.com/esimov/caire/utils"
)
Expand Down Expand Up @@ -273,7 +274,7 @@ func (g *Gui) Run() error {
}

if g.proc.vRes {
g.process.img = rotateImage270(g.process.img.(*image.NRGBA))
g.process.img = cimage.RotateImage270(g.process.img.(*image.NRGBA))
}
w.Invalidate()
default:
Expand Down
Loading
Loading