Started working on proper parameters
This commit is contained in:
parent
2d42a0ef90
commit
488942db2e
3 changed files with 69 additions and 10 deletions
|
|
@ -14,6 +14,34 @@ import (
|
|||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
type imageProcessor struct {
|
||||
newParams func() any
|
||||
processImage func(ctx context.Context, srcImg image.Image, params any) (image.Image, error)
|
||||
}
|
||||
|
||||
type shadowProcessorArgs struct {
|
||||
Color string `json:"color"`
|
||||
OffsetY int `json:"offset_y"`
|
||||
}
|
||||
|
||||
var processors = map[string]imageProcessor{
|
||||
"shadow": {
|
||||
newParams: func() any {
|
||||
return &shadowProcessorArgs{
|
||||
Color: "#000000",
|
||||
OffsetY: 0,
|
||||
}
|
||||
},
|
||||
processImage: func(ctx context.Context, srcImg image.Image, params any) (image.Image, error) {
|
||||
p := params.(*shadowProcessorArgs)
|
||||
|
||||
shadow := makeBoxShadow(srcImg, color.Black, 4, 10, p.OffsetY)
|
||||
composit := imaging.OverlayCenter(shadow, srcImg, 1.0)
|
||||
return composit, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func (s *Service) reprocess(ctx context.Context, session *models.ImageEditSession) (imageSource, error) {
|
||||
var img imageSource
|
||||
|
||||
|
|
@ -72,12 +100,22 @@ func (s *Service) processImage(ctx context.Context, srcImg image.Image, processo
|
|||
defer f.Close()
|
||||
|
||||
return imaging.Decode(f)
|
||||
case "shadow":
|
||||
shadow := makeBoxShadow(srcImg, color.Black, 4, 10, 0)
|
||||
composit := imaging.OverlayCenter(shadow, srcImg, 1.0)
|
||||
return composit, nil
|
||||
//case "shadow":
|
||||
// shadow := makeBoxShadow(srcImg, color.Black, 4, 10, 0)
|
||||
// composit := imaging.OverlayCenter(shadow, srcImg, 1.0)
|
||||
// return composit, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown processor type: %v", processor.Type)
|
||||
|
||||
proc, ok := processors[processor.Type]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown processor type: %v", processor.Type)
|
||||
}
|
||||
|
||||
paramType := proc.newParams()
|
||||
if err := json.Unmarshal(processor.Props, paramType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return proc.processImage(ctx, srcImg, paramType)
|
||||
}
|
||||
|
||||
type imageSource interface {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package imgedit
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
|
|
@ -97,10 +98,21 @@ func (s *Service) AddProcessor(ctx context.Context, sessionID string, req AddPro
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: verify processor, etc.
|
||||
proc, ok := processors[req.Type]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown processor type: %v", req.Type)
|
||||
}
|
||||
|
||||
paramType := proc.newParams()
|
||||
paramBytes, err := json.Marshal(paramType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session.Processors = append(session.Processors, models.ImageEditProcessor{
|
||||
ID: models.NewNanoID(),
|
||||
Type: req.Type,
|
||||
ID: models.NewNanoID(),
|
||||
Type: req.Type,
|
||||
Props: paramBytes,
|
||||
})
|
||||
|
||||
session.RecalcVersionIDs()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue