forked from dsoprea/go-jpeg-image-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_parser.go
128 lines (94 loc) · 2.67 KB
/
media_parser.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
package jpegstructure
import (
"bufio"
"bytes"
"io"
"os"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-utility/image"
)
// JpegMediaParser is a `riimage.MediaParser` that knows how to parse JPEG
// images.
type JpegMediaParser struct {
}
// NewJpegMediaParser returns a new JpegMediaParser.
func NewJpegMediaParser() *JpegMediaParser {
// TODO(dustin): Add test
return new(JpegMediaParser)
}
// Parse parses a JPEG uses an `io.ReadSeeker`. Even if it fails, it will return
// the list of segments encountered prior to the failure.
func (jmp *JpegMediaParser) Parse(rs io.ReadSeeker, size int) (ec riimage.MediaContext, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s := bufio.NewScanner(rs)
// Since each segment can be any size, our buffer must allowed to grow as
// large as the file.
buffer := []byte{}
s.Buffer(buffer, size)
js := NewJpegSplitter(nil)
s.Split(js.Split)
for s.Scan() != false {
}
// Always return the segments that were parsed, at least until there was an
// error.
ec = js.Segments()
log.PanicIf(s.Err())
return ec, nil
}
// ParseFile parses a JPEG file. Even if it fails, it will return the list of
// segments encountered prior to the failure.
func (jmp *JpegMediaParser) ParseFile(filepath string) (ec riimage.MediaContext, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
f, err := os.Open(filepath)
log.PanicIf(err)
defer f.Close()
stat, err := f.Stat()
log.PanicIf(err)
size := stat.Size()
sl, err := jmp.Parse(f, int(size))
// Always return the segments that were parsed, at least until there was an
// error.
ec = sl
log.PanicIf(err)
return ec, nil
}
// ParseBytes parses a JPEG byte-slice. Even if it fails, it will return the
// list of segments encountered prior to the failure.
func (jmp *JpegMediaParser) ParseBytes(data []byte) (ec riimage.MediaContext, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
br := bytes.NewReader(data)
sl, err := jmp.Parse(br, len(data))
// Always return the segments that were parsed, at least until there was an
// error.
ec = sl
log.PanicIf(err)
return ec, nil
}
// LooksLikeFormat indicates whether the data looks like a JPEG image.
func (jmp *JpegMediaParser) LooksLikeFormat(data []byte) bool {
if len(data) < 4 {
return false
}
l := len(data)
if data[0] != 0xff || data[1] != MARKER_SOI || data[l-2] != 0xff || data[l-1] != MARKER_EOI {
return false
}
return true
}
var (
// Enforce interface conformance.
_ riimage.MediaParser = new(JpegMediaParser)
)