Skip to content

Commit

Permalink
add: export the edited image.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Le Saux committed Apr 24, 2024
1 parent ecc64e0 commit fab1057
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
46 changes: 46 additions & 0 deletions gfx/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,49 @@ func ApplyOneImage(in image.Image,
}
return data, out, newPalette, lineSize, err
}

func ExportRawImage(in image.Image,
palette color.Palette,
cfg *config.MartineConfig,
filename, picturePath string,
screenMode uint8) error {
var err error
//check the palette and sort it
palette = constants.FillColorPalette(palette)
palette = constants.SortColorsByDistance(palette)

// at least resize the image to be sure
out := ci.Resize(in, cfg.Size, cfg.ResizingAlgo)
if cfg.UseKmeans {
log.GetLogger().Info("kmeans with %f threshold", cfg.KmeansThreshold)
out, err = ci.Kmeans(cfg.Size.ColorsAvailable, cfg.KmeansThreshold, out)
if err != nil {
return err
}
}
if !cfg.CustomDimension && !cfg.SpriteHard {
err = Transform(out, palette, cfg.Size, picturePath, cfg)
if err != nil {
return err
}
} else {
if cfg.ZigZag {
// prepare zigzag transformation
out = transformation.Zigzag(out)
}
if !cfg.SpriteHard {
// log.GetLogger().Info( "Transform image in sprite.\n")
err = sprite.ToSpriteAndExport(out, palette, cfg.Size, screenMode, filename, false, cfg)
if err != nil {
return err
}
} else {
// log.GetLogger().Info( "Transform image in sprite hard.\n")
err = spritehard.ToSpriteHardAndExport(out, palette, cfg.Size, screenMode, filename, cfg)
if err != nil {
return err
}
}
}
return nil
}
36 changes: 20 additions & 16 deletions ui/martine-ui/image_tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"github.com/jeromelesaux/martine/export/m4"
"github.com/jeromelesaux/martine/log"

im "image"

"github.com/jeromelesaux/martine/export/png"
"github.com/jeromelesaux/martine/export/snapshot"
"github.com/jeromelesaux/martine/gfx"
Expand Down Expand Up @@ -106,14 +104,24 @@ func (m *MartineUI) ExportOneImage(me *menu.ImageMenu) {
}

filename := filepath.Base(me.OriginalImagePath())
err = gfx.ApplyOneImageAndExport(
me.OriginalImage().Image,
cfg,
filename,
m.imageExport.ExportFolderPath+string(filepath.Separator)+filename,
me.Mode,
uint8(me.Mode))

if me.Edited {
err = gfx.ExportRawImage(
me.CpcImage().Image,
me.Palette(),
cfg,
filename,
m.imageExport.ExportFolderPath+string(filepath.Separator)+filename,
uint8(me.Mode),
)
} else {
err = gfx.ApplyOneImageAndExport(
me.OriginalImage().Image,
cfg,
filename,
m.imageExport.ExportFolderPath+string(filepath.Separator)+filename,
me.Mode,
uint8(me.Mode))
}
os.Remove(tmpPalette)

if err != nil {
Expand Down Expand Up @@ -160,6 +168,7 @@ func (m *MartineUI) monochromeColor(c color.Color) {
}

func (m *MartineUI) ApplyOneImage(me *menu.ImageMenu) {
me.Edited = false
cfg := m.NewConfig(me, true)
if cfg == nil {
return
Expand Down Expand Up @@ -204,11 +213,6 @@ func (m *MartineUI) ApplyOneImage(me *menu.ImageMenu) {
me.SetPaletteImage(png.PalToImage(me.Palette()))
}

func (m *MartineUI) syncImagePalette(i im.Image, p color.Palette) {
m.main.SetCpcImage(i)
m.main.SetPalette(p)
}

// nolint: funlen
func (m *MartineUI) newImageTransfertTab(me *menu.ImageMenu) *fyne.Container {
importOpen := newImportButton(m, me)
Expand Down Expand Up @@ -361,7 +365,7 @@ func (m *MartineUI) newImageTransfertTab(me *menu.ImageMenu) *fyne.Container {
edit := editor.NewEditor(me.CpcImage().Image,
editor.MagnifyX2,
me.Palette(),
p, m.syncImagePalette)
p, me.SetImagePalette)

d := dialog.NewCustom("Editor", "Ok", edit.NewEditor(), m.window)
size := m.window.Content().Size()
Expand Down
7 changes: 7 additions & 0 deletions ui/martine-ui/menu/image_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ImageMenu struct {
CmdLineGenerate string
UseKmeans bool
KmeansThreshold float64
Edited bool
}

func NewImageMenu() *ImageMenu {
Expand Down Expand Up @@ -204,6 +205,12 @@ func (me *ImageMenu) OriginalImage() *canvas.Image {
return me.originalImage
}

func (me *ImageMenu) SetImagePalette(i image.Image, p color.Palette) {
me.SetCpcImage(i)
me.SetPalette(p)
me.Edited = true
}

// nolint: funlen, gocognit
func (me *ImageMenu) NewImportButton(dialogSize fyne.Size, modeSelection *widget.Select, refreshUI *widget.Button, win fyne.Window) *widget.Button {
return widget.NewButtonWithIcon("Import", theme.FileImageIcon(), func() {
Expand Down

0 comments on commit fab1057

Please sign in to comment.