-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreader.go
139 lines (114 loc) · 2.73 KB
/
reader.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
// Copyright 2015 Jesse G Donat.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
// Package mpo implements an MPO image decoder.
//
// MPO is defined in CIPA DC-007: http://www.cipa.jp/std/documents/e/DC-007_E.pdf.
package mpo
import (
"bytes"
"errors"
"image"
"image/jpeg"
"io"
"io/ioutil"
)
// ErrNoImages indicates that no images were found in the specified file.
var ErrNoImages = errors.New("no images found in mpo image")
// MPO represents the likely multiple images stored in a MPO file.
type MPO struct {
Image []image.Image
}
const (
mpojpgMKR = 0xFF
mpojpgSOI = 0xD8 // Start of Image
mpojpgEOI = 0xD9 // End of Image
)
// DecodeAll reads an MPO image from r and returns the sequential frames
func DecodeAll(rr io.Reader) (*MPO, error) {
data, err := ioutil.ReadAll(rr)
if err != nil {
return nil, err
}
r := bytes.NewReader(data)
sectReaders := make([]*io.SectionReader, 0)
readData := make([]byte, 1)
var (
depth uint8
imgStart int64
loc int64
)
for {
i1, err := r.Read(readData)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
loc += int64(i1)
if readData[0] == mpojpgMKR {
i2, err := r.Read(readData)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
loc += int64(i2)
if readData[0] == mpojpgSOI {
if depth == 0 {
imgStart = loc - 2
}
depth++
} else if readData[0] == mpojpgEOI {
depth--
if depth == 0 {
sectReaders = append(sectReaders, io.NewSectionReader(r, imgStart, loc))
}
}
}
}
m := &MPO{
Image: make([]image.Image, 0),
}
for _, s := range sectReaders {
img, err := jpeg.Decode(s)
if err != nil {
return nil, err
}
m.Image = append(m.Image, img)
}
return m, nil
}
// Decode reads a MPO image from r and returns it as an image.Image.
func Decode(r io.Reader) (image.Image, error) {
all, err := DecodeAll(r)
if err != nil {
return nil, err
}
if len(all.Image) < 1 {
return nil, ErrNoImages
}
return all.Image[0], nil
}
// DecodeConfig returns the color model and dimensions of an MPO image without
// decoding the entire image.
//
// TODO Optimize this - possibly just faling back to jpeg.DecodeConfig
func DecodeConfig(r io.Reader) (image.Config, error) {
all, err := DecodeAll(r)
if err != nil {
return image.Config{}, err
}
if len(all.Image) < 1 {
return image.Config{}, ErrNoImages
}
return image.Config{
ColorModel: all.Image[0].ColorModel(),
Width: all.Image[0].Bounds().Max.X,
Height: all.Image[0].Bounds().Max.Y,
}, nil
}
func init() {
// \xff\xd8\xff clashes with jpeg but hopefully shouldn't cause issues
image.RegisterFormat("mpo", "\xff\xd8\xff", Decode, DecodeConfig)
}