/
transform.go
210 lines (201 loc) · 4.74 KB
/
transform.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package gfx
import (
"image"
"image/color"
"github.com/jeromelesaux/martine/config"
"github.com/jeromelesaux/martine/constants"
"github.com/jeromelesaux/martine/convert/export"
"github.com/jeromelesaux/martine/convert/pixel"
"github.com/jeromelesaux/martine/convert/screen"
"github.com/jeromelesaux/martine/gfx/errors"
"github.com/jeromelesaux/martine/log"
)
func Transform(in *image.NRGBA,
p color.Palette,
size constants.Size,
filepath string,
cfg *config.MartineConfig) error {
switch size {
case constants.Mode0:
return export.ToMode0AndExport(in, p, size, filepath, cfg)
case constants.Mode1:
return export.ToMode1AndExport(in, p, size, filepath, cfg)
case constants.Mode2:
return export.ToMode2AndExport(in, p, size, filepath, cfg)
case constants.OverscanMode0:
return export.ToMode0AndExport(in, p, size, filepath, cfg)
case constants.OverscanMode1:
return export.ToMode1AndExport(in, p, size, filepath, cfg)
case constants.OverscanMode2:
return export.ToMode2AndExport(in, p, size, filepath, cfg)
default:
return errors.ErrorNotYetImplemented
}
}
func InternalTransform(
in *image.NRGBA,
p color.Palette,
size constants.Size,
cfg *config.MartineConfig) []byte {
switch size {
case constants.Mode0:
return screen.ToMode0(in, p, cfg)
case constants.Mode1:
return screen.ToMode1(in, p, cfg)
case constants.Mode2:
return screen.ToMode2(in, p, cfg)
case constants.OverscanMode0:
return screen.ToMode0(in, p, cfg)
case constants.OverscanMode1:
return screen.ToMode1(in, p, cfg)
case constants.OverscanMode2:
return screen.ToMode2(in, p, cfg)
default:
return []byte{}
}
}
func revertColor(rawColor uint8, index int, isPlus bool) color.Color {
var newColor color.Color
var err error
if !isPlus {
newColor, err = constants.ColorFromHardware(rawColor)
if err != nil {
log.GetLogger().Error("No color found in data at index %d\n", index)
return constants.White.Color
}
} else {
plusColor := constants.NewRawCpcPlusColor(uint16(rawColor))
c := color.RGBA{A: 0xFF, R: plusColor.R, G: plusColor.G, B: plusColor.B}
newColor = constants.CpcPlusPalette.Convert(c)
}
return newColor
}
// nolint: funlen, gocognit
func TransformRawCpcData(data, palette []int, width, height int, mode int, isPlus bool) (*image.NRGBA, error) {
in := image.NewNRGBA(image.Rectangle{image.Point{X: 0, Y: 0}, image.Point{X: width, Y: height}})
x := 0
y := 0
for index, val := range data {
switch mode {
case 0:
p1, p2 := pixel.RawPixelMode0(byte(val))
c1 := palette[p1]
newColor := revertColor(uint8(c1), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c2 := palette[p2]
newColor = revertColor(uint8(c2), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
case 1:
p1, p2, p3, p4 := pixel.RawPixelMode1(byte(val))
c1 := palette[p1]
newColor := revertColor(uint8(c1), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c2 := palette[p2]
newColor = revertColor(uint8(c2), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c3 := palette[p3]
newColor = revertColor(uint8(c3), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c4 := palette[p4]
newColor = revertColor(uint8(c4), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
case 2:
p1, p2, p3, p4, p5, p6, p7, p8 := pixel.RawPixelMode2(byte(val))
c1 := palette[p1]
newColor := revertColor(uint8(c1), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c2 := palette[p2]
newColor = revertColor(uint8(c2), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c3 := palette[p3]
newColor = revertColor(uint8(c3), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c4 := palette[p4]
newColor = revertColor(uint8(c4), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c5 := palette[p5]
newColor = revertColor(uint8(c5), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c6 := palette[p6]
newColor = revertColor(uint8(c6), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c7 := palette[p7]
newColor = revertColor(uint8(c7), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
c8 := palette[p8]
newColor = revertColor(uint8(c8), index, isPlus)
in.Set(x, y, newColor)
x++
if (x % width) == 0 {
x = 0
y++
}
}
}
return in, nil
}