-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
552 lines (479 loc) · 12.8 KB
/
main.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package main
import (
"bytes"
"context"
"fmt"
"image"
"image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/sync/errgroup"
"github.com/vitali-fedulov/images4"
)
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}
// errMsg is a type for error message
type (
errMsg error
)
// `resultMsg` is a struct that contains a `duration`, `emoji`, and an `err`.
// @property duration - The time it took to run the command.
// @property {string} emoji - The emoji that will be displayed in the message.
// @property {error} err - This is the error that occurred during the execution of the function.
type resultMsg struct {
duration time.Duration
emoji string
err error
}
// ImgWithDelay is a struct that contains an image.Image and an delay in numbers of frames.
// @property img - The image.Image object that represents the frame.
// @property {int} delay - The delay in numbers of frames before the next image is shown.
type imgWithDelay struct {
img image.Image
delay int
}
// PalettedWithDelay is a struct that contains an image.Paletted and an delay in numbers of frames.
// @property paletted - The image.Paletted object that represents the frame.
// @property {int} delay - The delay in numbers of frames before the next image is shown.
type palettedWithDelay struct {
paletted *image.Paletted
delay int
}
// input fields in the form
const (
path = iota
output
fps
)
// thy and thCbCr are the threshold for the YCbCr color model to check if images are equal.
const (
thy = float64(100)
thCbCr = float64(200)
)
// hotPink and darkGray are the colors used in the UI.
const (
hotPink = lipgloss.Color("#FF06B7")
darkGray = lipgloss.Color("#767676")
)
// inputStyle and continueStyle are the styles for inputs.
var (
inputStyle = lipgloss.NewStyle().Foreground(hotPink)
continueStyle = lipgloss.NewStyle().Foreground(darkGray)
)
// model is the main model for the tea app.
// @property {[]textinput.Model} inputs - the models for the text inputs.
// @property {int} focused - The index of the input that is currently focused.
// @property {spinner.Model} spinner - The spinner model.
// @property {bool} loading - Whether the app is currently processing images.
// @property {time.Duration} duration - The duration of the processing.
// @property {bool} finished - Whether the current processing pipe has finished.
// @property {error} err - This is the error that will be displayed if any errors happen.
type model struct {
inputs []textinput.Model
focused int
spinner spinner.Model
loading bool
duration time.Duration
finished bool
err error
}
// Validator functions to ensure valid input
func fpsValidator(s string) error {
// fps should be a number
c := strings.ReplaceAll(s, " ", "")
_, err := strconv.ParseInt(c, 10, 64)
return err
}
// initialize app model.
func initialModel() model {
var inputs []textinput.Model = make([]textinput.Model, 3)
inputs[path] = textinput.New()
inputs[path].Placeholder = "/path/to/folder/"
inputs[path].Focus()
inputs[path].Width = 30
inputs[path].Prompt = ""
inputs[output] = textinput.New()
inputs[output].Placeholder = "output.gif"
inputs[output].Width = 20
inputs[output].Prompt = ""
inputs[fps] = textinput.New()
inputs[fps].Placeholder = "30"
inputs[fps].CharLimit = 2
inputs[fps].Width = 5
inputs[fps].Prompt = ""
inputs[fps].Validate = fpsValidator
sp := spinner.New()
sp.Spinner = spinner.MiniDot
sp.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("206"))
return model{
inputs: inputs,
focused: 0,
spinner: sp,
err: nil,
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(textinput.Blink, m.spinner.Tick)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd = make([]tea.Cmd, len(m.inputs))
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
// if the app is currently processing images, then we don't want to do anything.
if m.loading {
return m, nil
}
// if the app has finished processing images, then we want to reset the model.
if m.finished || m.err != nil {
w := m.inputs[path].Width
sp := m.spinner
m = initialModel()
m.inputs[path].Width = w
m.inputs[output].Width = w / 2
m.inputs[fps].Width = w / 2
m.spinner = sp
return m, nil
}
// if the current input is the last input, then we want to start processing images.
if m.focused == len(m.inputs)-1 {
m.loading = true
for i := range m.inputs {
m.inputs[i].Blur()
}
return m, gen(m.inputs[path].Value(), m.inputs[output].Value(), m.inputs[fps].Value())
}
// otherwise, we want to move to the next input.
m.nextInput()
// quit app
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
// navigate between inputs
case tea.KeyShiftTab, tea.KeyCtrlP:
if m.loading || m.finished || m.err != nil {
return m, nil
}
m.prevInput()
case tea.KeyTab, tea.KeyCtrlN:
if m.loading || m.finished || m.err != nil {
return m, nil
}
m.nextInput()
}
for i := range m.inputs {
m.inputs[i].Blur()
}
m.inputs[m.focused].Focus()
// Check terminal size
case tea.WindowSizeMsg:
m.inputs[path].Width = msg.Width
m.inputs[output].Width = msg.Width / 2
m.inputs[fps].Width = msg.Width / 2
// Handle results
case resultMsg:
m.loading = false
m.inputs[path].Focus()
if msg.err != nil {
m.err = msg.err
return m, nil
}
m.finished = true
return m, nil
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}
// Update inputs
for i := range m.inputs {
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
}
var cmdSpin tea.Cmd
m.spinner, cmdSpin = m.spinner.Update(msg)
cmds = append(cmds, cmdSpin)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
pad := strings.Repeat(" ", 2)
// Render processing spinner
if m.loading {
return "\n\n" + pad + pad + m.spinner.View() + " processing...\n"
}
// Render error message
if m.err != nil {
return "" +
lipgloss.NewStyle().
Foreground(lipgloss.Color("#bf616a")).
Copy().
Width(m.inputs[path].Width).
PaddingLeft(4).
PaddingTop(2).
Render("error: "+m.err.Error()) +
continueStyle.
Copy().
PaddingTop(3).
PaddingLeft(2).
Render("Start again ->") +
"\n"
}
// Render success message
if m.finished {
filename := "./out.gif"
if m.inputs[output].Value() != "" {
filename = m.inputs[output].Value()
}
outPath, _ := filepath.Abs(filename)
return "" +
lipgloss.NewStyle().
Foreground(lipgloss.Color("#a3be8c")).
Copy().
Width(m.inputs[path].Width).
PaddingTop(1).
PaddingLeft(2).
Render("success, open your file: ") +
lipgloss.NewStyle().
Foreground(lipgloss.Color("#8fbcbb")).
Copy().
PaddingTop(2).
PaddingLeft(4).
Width(m.inputs[path].Width).
Render(outPath) +
continueStyle.
Copy().
PaddingTop(5).
PaddingLeft(2).
Render("Continue ->") +
"\n"
}
// Render input fields
return fmt.Sprintf(
`
Generate Gif from a bunch of png or jpg files:
%s
%s
%s
%s
%s
%s
%s
`,
inputStyle.Width(m.inputs[path].Width).Render("Path to folder with images:"),
m.inputs[path].View(),
inputStyle.Width(m.inputs[output].Width).Render("Output file:"),
m.inputs[output].View(),
inputStyle.Width(m.inputs[fps].Width).Render("Frame rate (👉25-50👈):"),
m.inputs[fps].View(),
continueStyle.Render("Continue ->"),
) + "\n"
}
// nextInput focuses the next input field
func (m *model) nextInput() {
m.focused = (m.focused + 1) % len(m.inputs)
}
// prevInput focuses the previous input field
func (m *model) prevInput() {
m.focused--
// Wrap around
if m.focused < 0 {
m.focused = len(m.inputs) - 1
}
}
// gen is the func that generates the gif
func gen(path, output, fps string) tea.Cmd {
if output == "" {
output = "out.gif"
}
return func() tea.Msg {
start := time.Now()
// list files in path
paths, err := listFiles(path)
if err != nil {
return resultMsg{err: err, emoji: "📂"}
}
// parse fps
c := strings.ReplaceAll(fps, " ", "")
if c == "" {
c = "30"
}
fpsVal, _ := strconv.ParseInt(c, 10, 64)
// build gif
err = BuildGif(
paths,
output,
int(fpsVal),
)
if err != nil {
return resultMsg{err: err, emoji: "🔨"}
}
duration := time.Since(start)
return resultMsg{err: nil, emoji: "🎉", duration: duration}
}
}
/* ------------------------------------------------------------ */
/* --------------------- WORK WITH IMAGES --------------------- */
/* ------------------------------------------------------------ */
// list files in path
func listFiles(path string) (*[]string, error) {
var files []string
dir, err := os.Open(path)
if err != nil {
return nil, err
}
defer dir.Close()
fileInfos, err := dir.Readdir(-1)
if err != nil {
return nil, err
}
for _, fi := range fileInfos {
if !fi.IsDir() {
// add file to list if it is not a .png or .jpg
if filepath.Ext(fi.Name()) == ".png" || filepath.Ext(fi.Name()) == ".jpg" {
files = append(files, filepath.Join(path, fi.Name()))
}
}
}
sort.Strings(files)
return &files, nil
}
func readImages(files *[]string) ([]imgWithDelay, error) {
// create slice of images
images := []imgWithDelay{}
// save previous image to compare with current and count delay (equal images in a row)
prevImg := image.Image(nil)
delay := 1
// read images from files
for _, s := range *files {
f, err := os.Open(s)
if err != nil {
return nil, fmt.Errorf("failed to open file (%s): %w", s, err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, fmt.Errorf("failed to decode image (%s): %w", s, err)
}
// if prevImg is not nil, compare it with current image, if they are equal, increase delay,
// else add previous image to slice of images, reset delay, and set current image as previous
if prevImg != nil {
if !imagesEqual(prevImg, img) {
images = append(images, imgWithDelay{prevImg, delay})
delay = 1
prevImg = img
} else {
delay++
}
} else {
prevImg = img
}
}
// add last image to slice of images
images = append(images, imgWithDelay{prevImg, delay})
return images, nil
}
func imagesEqual(a, b image.Image) bool {
// Icons are compact image representations (image "hashes").
// Name "hash" is not used intentionally.
iconA := images4.Icon(a)
iconB := images4.Icon(b)
// Compare icons by proportion similarity metric.
if images4.PropMetric(iconA, iconB) > 0.001 {
return false
}
// Compare icons by Euclidean distance in YCbCr color space.
m1, m2, m3 := images4.EucMetric(iconA, iconB)
if m1 > thy {
return false
}
if m2 > thCbCr || m3 > thCbCr {
return false
}
return true
}
// encode and decode is necessary to convert jpeg and png to gif.
func encodeImgPaletted(images *[]imgWithDelay) ([]*palettedWithDelay, error) {
// Gif options
opt := gif.Options{}
imgp := make([]*palettedWithDelay, len(*images))
// create a go routine for each image. and wait for all to finish.
errGroup, _ := errgroup.WithContext(context.Background())
lck := sync.Mutex{}
for ctr, im := range *images {
ctr := ctr
im := im
// create a go routine for each image. And wait for all to finish. Check if any errors.
errGroup.Go(func() error {
b := bytes.Buffer{}
// Write file to buffer.
err := gif.Encode(&b, im.img, &opt)
if err != nil {
return err
}
// Decode file from buffer to img.
img, err := gif.Decode(&b)
if err != nil {
return err
}
// Cast img.
i, ok := img.(*image.Paletted)
if ok {
lck.Lock()
defer lck.Unlock()
imgp[ctr] = &palettedWithDelay{i, im.delay}
}
return nil
})
}
if err := errGroup.Wait(); err != nil {
return nil, err
}
return imgp, nil
}
// write a file from a paletted image slice, delay in 100ths of a second per frame.
func writeGif(im *[]*palettedWithDelay, delay int, path string) error {
g := &gif.GIF{}
for _, i := range *im {
g.Image = append(g.Image, i.paletted)
// delay is in 100ths of a second per frame, i.delay represents image repetitions in the source.
g.Delay = append(g.Delay, delay*i.delay)
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return gif.EncodeAll(f, g)
}
// BuildGif takes an array of file paths pointing to images as input.
// out: path to the output file.
// fps: frames per second, default 30.
func BuildGif(files *[]string, out string, fps int) error {
if fps == 0 {
fps = 30
}
img, err := readImages(files)
if err != nil {
return err
}
im_p, err := encodeImgPaletted(&img)
if err != nil {
return err
}
return writeGif(&im_p, 100/fps, out)
}