-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnefparser.go
242 lines (207 loc) · 7.72 KB
/
nefparser.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
/*
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
import (
"fmt"
"log"
"math"
"os"
"time"
)
// NefParserKey is a unique identifier for the NEF raw file parser.
// This key may be used as a key the RawParsers map.
const NefParserKey = "NEF"
// nefHeader is a struct representing a NEF file header.
// Byte Order: offset 0, len 2
// TIFF Magic Value: offset 2, len 2
// TIFF Offset Value: offset 4, len 4
type nefHeader struct {
isBigEndian bool
tiffMagicValue uint16
tiffOffset int64 // offset from start of file
}
// NefParser is the struct defining the state of
// the RawFile concept. Implements the RawParser interface.
// This parser provides basic parsing functionaity for the Nikon Electronic Format
// (NEF). For a specified NEF, the EXIF create time and orientation are parsed and the
// embedded JPEG is extracted. The following are resources on NEF file details:
//
// NEF-specific information: http://lclevy.free.fr/nef/
// TIFF specification: http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
type NefParser struct {
*rawParser
}
// ProcessFile is the entry point into the NefParser. For a specified NEF,
// via RawFileInfo, the file shall be processed, JPEG extracted, and
// processed details returned to the caller.
// Returns a pointer the RawFile data structure or error.
func (n NefParser) ProcessFile(info *RawFileInfo) (nef *RawFile, err error) {
nef = new(RawFile)
// file is closed in subsequent method
f, err := os.Open(info.File)
if err != nil {
log.Printf("Error: Unable to open file: '%s'\n", info.File)
} else {
h, _ := n.processHeader(f)
jpegInfo, createDate, err := n.processIfds(f, h)
if err != nil {
return nef, err
} else if jpegInfo.length <= 0 {
return nef, fmt.Errorf("invalid jpeg length: %d", jpegInfo.length)
}
jpegPath, err := n.decodeAndWriteJpeg(f, jpegInfo, info.DestDir, info.Quality)
if err == nil {
nef.FileName = info.File
nef.CreateDate = createDate
nef.JpegPath = jpegPath
nef.JpegOrientation = jpegInfo.orientation
log.Printf("========= Processed file %s\n", info.File)
}
}
return nef, err
}
// processHeader reads NEF header that defines:
// byte order;
// TIFF magic value
// TIFF offset
// Returns a pointer to the header struct or error.
func (n NefParser) processHeader(f *os.File) (*nefHeader, error) {
var h nefHeader
// byte order
bytes, err := readField(0, 2, f)
if err != nil {
return &h, err
}
// byte order
byteOrder := bytesToUShort(n.IsHostLittleEndian(), false, bytes)
// set byte order from file read
h.isBigEndian = (byteOrder == 0x4D4D)
// DEBUG
//if !h.isBigEndian {
//log.Println("NEF is LITTLE ENDIAN!")
//}
// DEBUG
// TIFF magic value
bytes, err = readField(2, 2, f)
if err != nil {
return &h, err
}
h.tiffMagicValue = bytesToUShort(n.IsHostLittleEndian(), h.isBigEndian, bytes)
// TIFF offset
bytes, err = readField(4, 4, f)
if err != nil {
return &h, err
}
val := bytesToUInt(n.IsHostLittleEndian(), h.isBigEndian, bytes)
h.tiffOffset = int64(val)
return &h, err
}
// processIfds reads all currently-supported IFDs from the NEF. Currently, it parses:
// jpegInfo - the information pertaining to the embedded jpeg within the NEF;
// cDate - the EXIF specified NEF creation time;
// Note: more EXIF and NEF-specific tags could be parsed in a future release.
// Return jpegInfo, creation date/time or an error.
func (n NefParser) processIfds(f *os.File, h *nefHeader) (j *jpegInfo, cDate time.Time, err error) {
var jpeg jpegInfo
offset := h.tiffOffset
entries, err := processIfd(n.IsHostLittleEndian(), h.isBigEndian, offset, f)
if err == nil {
for e := entries.Front(); e != nil; e = e.Next() {
entry := e.Value.(ifdEntry)
if entry.tag == 0x014a { // SUBID
// JPEG offset (SUBID 0)
bytes, err := readField(int64(entry.valueOffset), 4, f)
if err == nil {
subID0Offset := int64(bytesToUInt(n.IsHostLittleEndian(), h.isBigEndian, bytes))
// Read SUBIFD 0 for JPEG
subIfd0Entries, err := processIfd(n.IsHostLittleEndian(), h.isBigEndian, subID0Offset, f)
if err == nil {
for se := subIfd0Entries.Front(); se != nil; se = se.Next() {
subID0Entry := se.Value.(ifdEntry)
if subID0Entry.tag == 0x011a {
jpeg.xRes, _, jpeg.xResFloat, _ = processRationalEntry(n.IsHostLittleEndian(), h.isBigEndian, subID0Entry.valueOffset, f)
}
if subID0Entry.tag == 0x011b {
jpeg.yRes, _, jpeg.yResFloat, _ = processRationalEntry(n.IsHostLittleEndian(), h.isBigEndian, subID0Entry.valueOffset, f)
}
if subID0Entry.tag == 0x0201 {
jpeg.offset = int64(subID0Entry.valueOffset)
}
if subID0Entry.tag == 0x0202 {
jpeg.length = int64(subID0Entry.valueOffset)
}
}
} else {
return &jpeg, cDate, err
}
}
} else if entry.tag == 0x0112 { // orientation tag
o := processShortValue(h.isBigEndian, entry.valueOffset)
if o == 8 {
// rotate 270 CW
rotationRads := 270 * math.Pi / 180
jpeg.orientation = rotationRads
} else {
jpeg.orientation = 0.0
}
} else if entry.tag == 0x8769 { // EXIF IFD pointer
// EXIF IFD pointer. Note: the pointer is the value represented
// in valueOffset.
// Read EXIF Entries
exifEntries, err := processIfd(n.IsHostLittleEndian(), h.isBigEndian, int64(entry.valueOffset), f)
if err == nil {
for exif := exifEntries.Front(); exif != nil; exif = exif.Next() {
exifEntry := exif.Value.(ifdEntry)
if exifEntry.tag == 0x9004 {
createDate, err := processASCIIEntry(&exifEntry, f)
if err == nil {
cDate, _ = parseDateTime(createDate)
}
}
}
} else {
return &jpeg, cDate, err
}
}
}
}
return &jpeg, cDate, err
}
// decodeAndWriteJpeg extracts the embedded jpeg bytes within a NEF,
// decodes the JPEG data, and then creates a new jpeg file.
// Returns the full path to the jpeg extracted or an error.
func (n NefParser) decodeAndWriteJpeg(f *os.File, j *jpegInfo, destDir string, quality int) (jpegFileName string, err error) {
// extract jpeg to new file
jpegFileName = genExtractedJpegName(f, destDir, "_extracted.jpg")
log.Printf("Creating JPEG file: %s\n", jpegFileName)
data := make([]byte, j.length)
_, err = f.ReadAt(data, j.offset)
if err != nil {
log.Printf("Error reading embedded jpeg file: %v\n", err)
return jpegFileName, err
}
err = decodeAndWriteJpeg(data, quality, jpegFileName)
return jpegFileName, err
}
// NewNefParser creates an instance of NEF-specific RawParser.
// Returns an instance of a NEF-specific RawParser.
func NewNefParser(hostIsLittleEndian bool) (RawParser, string) {
return &NefParser{&rawParser{hostIsLittleEndian}}, NefParserKey
}