forked from jeremytorres/rawparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawparser.go
229 lines (199 loc) · 7.04 KB
/
rawparser.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
/*
Copyright (c) 2013 Jeremy Torres, https://github.com/jeremytorres/rawparser
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Package rawparser provides a basic parsing interface for camera raw files. The current
// incarnation supports TIFF-based RAW files (e.g., Canon CR2, Nikon NEF...).
//
// TIFF specification: http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
package rawparser
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// ifdEntry is a struct representing a TIFF Image File Directory (IFD).
// Each 12-byte IFD entry has the following format:
// Bytes 0-1 The Tag that identifies the field.
// Bytes 2-3 The field Type.
// Bytes 4-7 The number of values, Count of the indicated Type.
// Bytes 8-11 The Value Offset, the file offset (in bytes) of the Value for the field.
type ifdEntry struct {
tag, fieldType uint16
count, valueOffset uint32 // offset from start of file
}
// jpegInfo is a struct representing a RawFile'sembedded jpeg information.
type jpegInfo struct {
orientation float64
offset, length int64
xRes, yRes uint32
xResFloat, yResFloat float64
}
// RawFileInfo is a struct defining key information for parsing a RawFile.
type RawFileInfo struct {
File string
DestDir string
Quality int
// NumOfChannels int
}
// RawFile is a struct representing parsed results for a specific raw file.
type RawFile struct {
// Note: additional EXIF metadata may be added in future release.
CreateDate time.Time
FileName, JpegPath string
JpegOrientation float64
}
// RawParser is the defining interface of a raw file parser. Camera-specific parsers
// shall implement this interface.
type RawParser interface {
// ProcessFile processes a raw file per the implementation of this parser.
// Return a pointer to a RawFile struct or error.
ProcessFile(i *RawFileInfo) (r *RawFile, e error)
// SetHostIsLittleEndian is a function to set the RawParser host's
// endianness.
// Set to true if host is a little endian machine; false otherwise.
SetHostIsLittleEndian(b bool)
// IsLittleEndian is a function to get the value of the specified host
// endianness.
// Returns true if the host is a little endian machine.
IsHostLittleEndian() bool
}
// rawParser is a base implementation of the RawParser interface.
// It's purpose is to provide common functionality to implentations
// of the interface.
type rawParser struct {
HostIsLittleEndian bool
}
// SetHostIsLittleEndian is a function to set the host's
// endianness for the given instance of the RawParser.
// Set to true if host is a little endian machine; false otherwise.
func (r *rawParser) SetHostIsLittleEndian(hostIsLe bool) {
r.HostIsLittleEndian = hostIsLe
}
// IsHostLittleEndian is a function to get the host's
// endianness specified for the given instance of the RawParser.
// Returns true if the host is a little endian machine.
func (r rawParser) IsHostLittleEndian() bool {
return r.HostIsLittleEndian
}
// RawParsers is a structure containing a mapping
// of registered raw file parsers. The key is the
// lower-case file extension of the raw file type;
// the value is the pointer to the RawParser implementation.
type RawParsers struct {
parserMap map[string]*RawParser
}
// NewRawParsers creates an instance of RawParsers.
func NewRawParsers() *RawParsers {
p := new(RawParsers)
p.parserMap = make(map[string]*RawParser)
return p
}
// Register maps the implementation of the RawParser
// interface to the key.
func (p *RawParsers) Register(key string, parser *RawParser) {
p.parserMap[key] = parser
}
// GetParser returns a RawParser for a given raw file type or nil if not found.
func (p RawParsers) GetParser(key string) *RawParser {
return p.parserMap[key]
}
// DeleteParser removes the specified RawParser.
func (p *RawParsers) DeleteParser(key string) {
delete(p.parserMap, key)
}
// parseDateTime converts a TIFF-based date/time string into a time.Time.
// Returns a time.Time or error.
func parseDateTime(s string) (t time.Time, err error) {
const format = "02 Jan 06 15:04"
split := strings.Split(s, " ")
if len(split) != 2 {
return t, fmt.Errorf("dateTime string invalid: '%s'", s)
}
dateToken := split[0]
timeToken := split[1]
dateTokens := strings.Split(dateToken, ":")
timeTokens := strings.Split(timeToken, ":")
if len(dateTokens) == 3 && len(timeTokens) == 3 {
montStr, err := toRfc822Date(dateTokens)
if err != nil {
return t, err
}
dateStr := dateTokens[2] + " " + montStr + " " + string(dateTokens[0][2]) + string(dateTokens[0][3])
t, err = time.Parse(format, dateStr+" "+timeTokens[0]+":"+timeTokens[1])
if err != nil {
return t, err
}
} else {
err = fmt.Errorf("invalid date and/or time string format: %s %s", dateToken, timeToken)
}
return t, err
}
// toRfc822Date converts a TIFF-based numerical month to an RFC822, 3-digit
// alpha date.
// Returns 3-digit date string or error.
func toRfc822Date(dateTokens []string) (string, error) {
var monthStr string
var e error
token := dateTokens[1]
// numerical date to 3-digit alpha
switch token {
case "01":
monthStr = "Jan"
case "02":
monthStr = "Feb"
case "03":
monthStr = "Mar"
case "04":
monthStr = "Apr"
case "05":
monthStr = "May"
case "06":
monthStr = "Jun"
case "07":
monthStr = "Jul"
case "08":
monthStr = "Aug"
case "09":
monthStr = "Sep"
case "10":
monthStr = "Oct"
case "11":
monthStr = "Nov"
case "12":
monthStr = "Dec"
default:
e = fmt.Errorf("invalid month: '%s'", token)
}
return monthStr, e
}
// genExtractedJpegName creates a full path name for an extracted JPEG
// from a raw file.
// The input file is the pointer to the raw file and its base name is used
// as the base of the JPEG files; destDir is the full path
// to the destination directory containing the JPEG file; and suffix is
// the remainder of the file name including file extension.
// Example:
// destDir="/path_to/outputDir"
// suffix="_extracted.jpg"
// Returns fully-qualified path to the JPEG extraced from the raw file.
func genExtractedJpegName(f *os.File, destDir, suffix string) string {
return destDir + filepath.Base(f.Name()) + suffix
}