-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
132 lines (109 loc) · 2.44 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
package main
import (
"fmt"
"image/jpeg"
"io"
"log"
"os"
"github.com/adrium/goheif"
)
// Skip Writer for exif writing
type writerSkipper struct {
w io.Writer
bytesToSkip int
}
func main() {
if len(os.Args) > 2 {
// Try to convert the file
err := convertHeicToJpg(os.Args[1], os.Args[2])
if err != nil {
fmt.Printf("Error while converting %s: %v \n", os.Args[1], err)
Shutdown()
}
} else {
fmt.Println("Not enough arguments, needs: [executable] [file1.heic] [file2.jpg]")
Shutdown()
}
// Return the path of the converted image
path := "--" + os.Args[2] + "--"
fmt.Println("JPG:", path)
Shutdown()
}
// convertHeicToJpg takes in an input file (of heic format) and converts
// it to a jpeg format, named as the output parameters.
func convertHeicToJpg(input, output string) error {
fileInput, err := os.Open(input)
if err != nil {
return err
}
defer fileInput.Close()
// Extract exif data
exif, err := goheif.ExtractExif(fileInput)
if err != nil {
log.Println(err)
// Not use exif if not exists
exif = nil
}
img, err := goheif.Decode(fileInput)
if err != nil {
return err
}
fileOutput, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer fileOutput.Close()
// Write both the converted file and the exif data back
w, _ := newWriterExif(fileOutput, exif)
err = jpeg.Encode(w, img, nil)
if err != nil {
return err
}
return nil
}
func (w *writerSkipper) Write(data []byte) (int, error) {
if w.bytesToSkip <= 0 {
return w.w.Write(data)
}
if dataLen := len(data); dataLen < w.bytesToSkip {
w.bytesToSkip -= dataLen
return dataLen, nil
}
if n, err := w.w.Write(data[w.bytesToSkip:]); err == nil {
n += w.bytesToSkip
w.bytesToSkip = 0
return n, nil
} else {
return n, err
}
}
func newWriterExif(w io.Writer, exif []byte) (io.Writer, error) {
writer := &writerSkipper{w, 2}
soi := []byte{0xff, 0xd8}
if _, err := w.Write(soi); err != nil {
return nil, err
}
if exif != nil {
app1Marker := 0xe1
markerlen := 2 + len(exif)
marker := []byte{0xff, uint8(app1Marker), uint8(markerlen >> 8), uint8(markerlen & 0xff)}
if _, err := w.Write(marker); err != nil {
return nil, err
}
if _, err := w.Write(exif); err != nil {
return nil, err
}
}
return writer, nil
}
func Shutdown() {
fmt.Println("Created by MaestroError")
os.Exit(1)
}
func pwd() string {
path, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
return path
}