Skip to content

Commit

Permalink
add: refactor export one image
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Le Saux committed Apr 25, 2024
1 parent 2d3a070 commit b57f327
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 121 deletions.
2 changes: 1 addition & 1 deletion export/impdraw/overscan/overscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func OverscanPalette(filePath string) (color.Palette, uint8, error) {
// if err == nil {
c := constants.NewRawCpcPlusColor(pc)
log.GetLogger().Info("PEN(%d) R(%d) G(%d) B(%d)\n", i, c.R, c.G, c.B)
col := color.RGBA{A: 0xff, B: uint8(c.B) << 4, G: uint8(c.G) << 4, R: uint8(c.R) << 4}
col := color.RGBA{A: 0xff, B: c.B << 4, G: c.G << 4, R: c.R << 4}
palette = append(palette, col)
// } else {
// palette = append(palette, color.Black)
Expand Down
2 changes: 1 addition & 1 deletion gfx/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func revertColor(rawColor uint8, index int, isPlus bool) color.Color {
}
} else {
plusColor := constants.NewRawCpcPlusColor(uint16(rawColor))
c := color.RGBA{A: 0xFF, R: uint8(plusColor.R), G: uint8(plusColor.G), B: uint8(plusColor.B)}
c := color.RGBA{A: 0xFF, R: plusColor.R, G: plusColor.G, B: plusColor.B}
newColor = constants.CpcPlusPalette.Convert(c)
}
return newColor
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/goki/freetype v1.0.1 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jeromelesaux/fyne-io v0.0.0-20240425064507-610406c0b788 // indirect
github.com/jeromelesaux/fyne-io v0.0.0-20240425064507-610406c0b788
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/muesli/clusters v0.0.0-20200529215643-2700303c1762 // indirect
Expand Down
2 changes: 2 additions & 0 deletions ui/martine-ui/directory/saved_directory_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var savedDirectoryPath fyne.URI
var openDirectoryPath fyne.URI

// nolint: ireturn
func ExportDirectoryURI() (fyne.ListableURI, error) {
if savedDirectoryPath == nil {
return nil, fmt.Errorf("empty saved directory path")
Expand All @@ -21,6 +22,7 @@ func SetExportDirectoryURI(path fyne.URI) {
savedDirectoryPath = path
}

// nolint: ireturn
func ImportDirectoryURI() (fyne.ListableURI, error) {
if openDirectoryPath == nil {
return nil, fmt.Errorf("empty saved directory path")
Expand Down
4 changes: 2 additions & 2 deletions ui/martine-ui/export_dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (m *MartineUI) exportDialog(ie *menu.ImageExport, w fyne.Window) {
directory.SetExportDirectoryURI(lu)
ie.ExportFolderPath = lu.Path()
log.GetLogger().Infoln(ie.ExportFolderPath)
m.ExportOneImage(m.main)

// m.ExportOneImage(m.main)
m.main.ExportImage(m.imageExport, m.window, m.NewConfig)
// apply and export
}, m.window)
d, err := directory.ExportDirectoryURI()
Expand Down
116 changes: 0 additions & 116 deletions ui/martine-ui/image_tab.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package ui

import (
"fmt"
"image/color"
"os"
"path/filepath"
"strconv"

"fyne.io/fyne/v2"
Expand All @@ -18,15 +15,9 @@ import (
"github.com/jeromelesaux/fyne-io/widget/editor"
"github.com/jeromelesaux/martine/constants"
"github.com/jeromelesaux/martine/convert/image"
"github.com/jeromelesaux/martine/export/amsdos"
"github.com/jeromelesaux/martine/export/ascii"
"github.com/jeromelesaux/martine/export/diskimage"
impPalette "github.com/jeromelesaux/martine/export/impdraw/palette"
"github.com/jeromelesaux/martine/export/m4"
"github.com/jeromelesaux/martine/log"

"github.com/jeromelesaux/martine/export/png"
"github.com/jeromelesaux/martine/export/snapshot"
"github.com/jeromelesaux/martine/gfx"
"github.com/jeromelesaux/martine/ui/martine-ui/directory"
"github.com/jeromelesaux/martine/ui/martine-ui/menu"
Expand Down Expand Up @@ -55,113 +46,6 @@ func (m *MartineUI) CheckAmsdosHeaderExport(inDsk, addAmsdosHeader bool, d dialo
}
}

// nolint: funlen, gocognit
func (m *MartineUI) ExportOneImage(me *menu.ImageMenu) {
pi := wgt.NewProgressInfinite("Saving...., Please wait.", m.window)
pi.Show()
cfg := m.NewConfig(me, true)
if cfg == nil {
return
}
if m.imageExport.ExportText {
if cfg.Overscan {
cfg.ExportAsGoFile = true
}

out, _, palette, _, err := gfx.ApplyOneImage(me.OriginalImage().Image, cfg, me.Mode, me.Palette(), uint8(me.Mode))
if err != nil {
pi.Hide()
dialog.ShowError(err, m.window)
return
}
code := ascii.FormatAssemblyDatabyte(out, "\n")
palCode := ascii.FormatAssemblyCPCPalette(palette, "\n")
content := fmt.Sprintf("; Generated by martine\n; from file %s\nImage:\n%s\n\n; palette\npalette: \n%s\n ",
me.OriginalImagePath(),
code,
palCode)
filename := filepath.Base(me.OriginalImagePath())
fileExport := m.imageExport.ExportFolderPath + string(filepath.Separator) + filename + ".asm"
if err = amsdos.SaveStringOSFile(fileExport, content); err != nil {
pi.Hide()
dialog.ShowError(err, m.window)
return
}
} else {
// palette export
userDir, err := os.UserHomeDir()
if err != nil {
pi.Hide()
dialog.ShowError(err, m.window)
}
tmpPalette := userDir + string(filepath.Separator) + "temporary_palette.kit"
if err := impPalette.SaveKit(tmpPalette, me.Palette(), false); err != nil {
pi.Hide()
dialog.ShowError(err, m.window)
}
if me.UsePalette {
cfg.KitPath = tmpPalette
}

filename := filepath.Base(me.OriginalImagePath())
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 {
pi.Hide()
dialog.NewError(err, m.window).Show()
return
}
if cfg.Dsk {
if err := diskimage.ImportInDsk(me.OriginalImagePath(), cfg); err != nil {
dialog.NewError(err, m.window).Show()
return
}
}
if cfg.Sna {
if cfg.Overscan {
var gfxFile string
for _, v := range cfg.DskFiles {
if filepath.Ext(v) == ".SCR" {
gfxFile = v
break
}
}
cfg.SnaPath = filepath.Join(m.imageExport.ExportFolderPath, "test.sna")
if err := snapshot.ImportInSna(gfxFile, cfg.SnaPath, uint8(me.Mode)); err != nil {
dialog.NewError(err, m.window).Show()
return
}
}
}
}
if m.imageExport.ExportToM2 {
if err := m4.ImportInM4(cfg); err != nil {
dialog.NewError(err, m.window).Show()
log.GetLogger().Error("Cannot send to M4 error :%v\n", err)
}
}
pi.Hide()
dialog.ShowInformation("Save", "Your files are save in folder \n"+m.imageExport.ExportFolderPath, m.window)
}

func (m *MartineUI) monochromeColor(c color.Color) {
m.main.SetPalette(image.ColorMonochromePalette(c, m.main.Palette()))
m.main.SetPaletteImage(png.PalToImage(m.main.Palette()))
Expand Down
117 changes: 117 additions & 0 deletions ui/martine-ui/menu/image_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image"
"image/color"
"os"
"path/filepath"
"strconv"

"fyne.io/fyne/v2"
Expand All @@ -15,12 +16,21 @@ import (
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/disintegration/imaging"
wgt "github.com/jeromelesaux/fyne-io/widget"
"github.com/jeromelesaux/martine/config"
"github.com/jeromelesaux/martine/constants"
"github.com/jeromelesaux/martine/convert/screen"
ovs "github.com/jeromelesaux/martine/convert/screen/overscan"
"github.com/jeromelesaux/martine/convert/sprite"
"github.com/jeromelesaux/martine/export/amsdos"
"github.com/jeromelesaux/martine/export/ascii"
"github.com/jeromelesaux/martine/export/diskimage"
"github.com/jeromelesaux/martine/export/impdraw/overscan"
impPalette "github.com/jeromelesaux/martine/export/impdraw/palette"
"github.com/jeromelesaux/martine/export/m4"
"github.com/jeromelesaux/martine/export/png"
"github.com/jeromelesaux/martine/export/snapshot"
"github.com/jeromelesaux/martine/gfx"
"github.com/jeromelesaux/martine/log"
"github.com/jeromelesaux/martine/ui/martine-ui/directory"
)
Expand Down Expand Up @@ -288,3 +298,110 @@ func (me *ImageMenu) NewImportButton(dialogSize fyne.Size, modeSelection *widget
d.Show()
})
}

// nolint:funlen, gocognit
func (me *ImageMenu) ExportImage(e *ImageExport, w fyne.Window, getCfg func(me *ImageMenu, checkOriginalImage bool) *config.MartineConfig) {
pi := wgt.NewProgressInfinite("Saving...., Please wait.", w)
pi.Show()
cfg := getCfg(me, true)
if cfg == nil {
return
}
if e.ExportText {
if cfg.Overscan {
cfg.ExportAsGoFile = true
}

out, _, palette, _, err := gfx.ApplyOneImage(me.OriginalImage().Image, cfg, me.Mode, me.Palette(), uint8(me.Mode))
if err != nil {
pi.Hide()
dialog.ShowError(err, w)
return
}
code := ascii.FormatAssemblyDatabyte(out, "\n")
palCode := ascii.FormatAssemblyCPCPalette(palette, "\n")
content := fmt.Sprintf("; Generated by martine\n; from file %s\nImage:\n%s\n\n; palette\npalette: \n%s\n ",
me.OriginalImagePath(),
code,
palCode)
filename := filepath.Base(me.OriginalImagePath())
fileExport := e.ExportFolderPath + string(filepath.Separator) + filename + ".asm"
if err = amsdos.SaveStringOSFile(fileExport, content); err != nil {
pi.Hide()
dialog.ShowError(err, w)
return
}
} else {
// palette export
userDir, err := os.UserHomeDir()
if err != nil {
pi.Hide()
dialog.ShowError(err, w)
}
tmpPalette := userDir + string(filepath.Separator) + "temporary_palette.kit"
if err := impPalette.SaveKit(tmpPalette, me.Palette(), false); err != nil {
pi.Hide()
dialog.ShowError(err, w)
}
if me.UsePalette {
cfg.KitPath = tmpPalette
}

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

if err != nil {
pi.Hide()
dialog.NewError(err, w).Show()
return
}
if cfg.Dsk {
if err := diskimage.ImportInDsk(me.OriginalImagePath(), cfg); err != nil {
dialog.NewError(err, w).Show()
return
}
}
if cfg.Sna {
if cfg.Overscan {
var gfxFile string
for _, v := range cfg.DskFiles {
if filepath.Ext(v) == ".SCR" {
gfxFile = v
break
}
}
cfg.SnaPath = filepath.Join(e.ExportFolderPath, "test.sna")
if err := snapshot.ImportInSna(gfxFile, cfg.SnaPath, uint8(me.Mode)); err != nil {
dialog.NewError(err, w).Show()
return
}
}
}
}
if e.ExportToM2 {
if err := m4.ImportInM4(cfg); err != nil {
dialog.NewError(err, w).Show()
log.GetLogger().Error("Cannot send to M4 error :%v\n", err)
}
}
pi.Hide()
dialog.ShowInformation("Save", "Your files are save in folder \n"+e.ExportFolderPath, w)
}

0 comments on commit b57f327

Please sign in to comment.