-
Notifications
You must be signed in to change notification settings - Fork 6
/
animate_motif.go
131 lines (118 loc) · 3.65 KB
/
animate_motif.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package animate
import (
"fmt"
"image"
"image/color"
"image/gif"
"os"
"path/filepath"
"github.com/disintegration/imaging"
"github.com/jeromelesaux/martine/config"
"github.com/jeromelesaux/martine/constants"
ci "github.com/jeromelesaux/martine/convert/image"
"github.com/jeromelesaux/martine/convert/sprite"
"github.com/jeromelesaux/martine/export/amsdos"
"github.com/jeromelesaux/martine/export/ascii"
"github.com/jeromelesaux/martine/gfx/transformation"
zx0 "github.com/jeromelesaux/zx0/encode"
)
// nolint: funlen
func DeltaMotif(gitFilepath string, cfg *config.MartineConfig, threshold int, initialAddress uint16, mode uint8) error {
fr, err := os.Open(gitFilepath)
if err != nil {
return err
}
defer fr.Close()
gifImages, err := gif.DecodeAll(fr)
if err != nil {
return err
}
images := ConvertToImage(*gifImages)
// convertion de la palette en mode 1
size := constants.Mode1
screens := make([]*image.NRGBA, 0)
// resize all images
for _, v := range images {
out := ci.Resize(v, size, imaging.NearestNeighbor)
screens = append(screens, out)
}
// downgrading palette
customPalette, _, err := ci.DowngradingPalette(screens[0], size, cfg.CpcPlus)
if err != nil {
return err
}
// converting all screens
for index, v := range screens {
_, out := ci.DowngradingWithPalette(v, customPalette)
screens[index] = out
}
// recuperation des motifs
a := transformation.AnalyzeTilesBoard(screens[0], constants.Size{Width: 4, Height: 4}, nil)
refBoard := a.ReduceTilesNumber(float64(threshold))
btc := make([][]transformation.BoardTile, 0)
btc = append(btc, refBoard)
refTiles := transformation.GetUniqTiles(refBoard)
// application des motifs sur toutes les images
for i := 1; i < len(screens); i++ {
ab := transformation.AnalyzeTilesBoardWithTiles(screens[i], constants.Size{Width: 4, Height: 4}, refTiles)
board := ab.BoardTiles
btc = append(btc, board)
err = ab.Image(fmt.Sprintf("%.2d.png", i), board, a.ImageSize)
if err != nil {
continue
}
}
motifs := make([][]byte, 0)
/* conversion des sprites en mode cpc */
for i := 0; i < len(refTiles); i++ {
sprt := (&refTiles[i]).Image()
data, _, _, _ := sprite.ToSprite(sprt, customPalette, refTiles[i].Size, 1, cfg)
motifs = append(motifs, data)
}
/* calcul des coordonnées */
deltas := make([][]byte, 0)
for _, v := range btc {
// nbCoords := size.Width * size.Height / 2 / 4
delta := make([]byte, 0)
index := 0
pixel := 0
for j := 0; j < size.Height; j += 4 {
for i := 0; i < size.Width; i += 4 {
var pos int
if t := transformation.GetTile(v, i, j); t != nil {
pos = transformation.GetTilePostion(t, refTiles)
}
if index == 2 {
delta = append(delta, byte(pixel))
pixel = 0
index = 0
}
if index == 0 {
pixel += (pos << 4)
} else {
pixel += pos
}
index++
}
}
deltas = append(deltas, delta)
}
filename := cfg.OsFilename(".asm")
return exportDeltaMotif(deltas, motifs, customPalette, cfg, cfg.OutputPath+string(filepath.Separator)+filename)
}
func exportDeltaMotif(images [][]byte, motifs [][]byte, p color.Palette, cfg *config.MartineConfig, filename string) error {
var deltaCode string
for i := 0; i < len(images); i++ {
deltaCode += fmt.Sprintf("delta%.2d\n", i)
encoded := zx0.Encode(images[i])
deltaCode += ascii.FormatAssemblyDatabyte(encoded, "\n")
}
for i := 0; i < len(motifs); i++ {
deltaCode += fmt.Sprintf("motif%.2d\n", i)
deltaCode += ascii.FormatAssemblyDatabyte(motifs[i], "\n")
}
deltaCode += "palette:\n" + ascii.ByteToken + " "
deltaCode += ascii.FormatAssemblyBasicPalette(p, "\n")
// log.GetLogger().Info("%s", deltaCode)
return amsdos.SaveStringOSFile(filename, deltaCode)
}