Skip to content

Commit eef367b

Browse files
committed
Revise the 8BIM detection method to correctly parse the three std IPTC test images at https://iptc.org/standards/photo-metadata/iptc-standard/
1 parent 17bf138 commit eef367b

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

js/load-image-iptc.js

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,43 +96,54 @@
9696
return
9797
}
9898

99-
// skip over leading (variable) chars, to the always-fixed "8BIM" sequence
100-
offset+=18
99+
// Found "8BIM<EOT><EOT>" ?
100+
var isFieldSegmentStart = function(dataView, offset){
101+
return (
102+
dataView.getUint32(offset) === 0x3842494d &&
103+
dataView.getUint16(offset+4) === 0x0404
104+
)
105+
}
101106

102-
var littleEndian
103-
var dirOffset
107+
// Hunt forward, looking for the correct Iptc block signature:
108+
// Reference: https://metacpan.org/pod/distribution/Image-MetaData-JPEG/lib/Image/MetaData/JPEG/Structures.pod#Structure-of-a-Photoshop-style-APP13-segment
104109

105110
// From https://github.com/exif-js/exif-js/blob/master/exif.js ~ line 474 on
111+
while (offset < offset+length) {
106112

107-
// Check for the "Iptc" "8BIM<EOT><EOT>" ASCII sequence (0x3842494d 0x0404):
108-
if (dataView.getUint32(offset) !== 0x3842494d && dataView.getUint16(offset + 4) !== 0x0404) {
109-
console.log('no Iptc data at this offset')
110-
// No Iptc data, might be XMP data instead
111-
return
112-
}
113+
if (isFieldSegmentStart(dataView, offset)) {
113114

114-
var nameHeaderLength = dataView.getUint8(offset + 7)
115-
if (nameHeaderLength % 2 !== 0) nameHeaderLength += 1
116-
// Check for pre photoshop 6 format
117-
if (nameHeaderLength === 0) {
118-
// Always 4
119-
nameHeaderLength = 4
120-
}
115+
var nameHeaderLength = dataView.getUint8(offset + 7)
116+
if (nameHeaderLength % 2 !== 0) nameHeaderLength += 1
117+
// Check for pre photoshop 6 format
118+
if (nameHeaderLength === 0) {
119+
// Always 4
120+
nameHeaderLength = 4
121+
}
122+
123+
var startOffset = offset + 8 + nameHeaderLength
124+
var sectionLength = dataView.getUint16(offset + 6 + nameHeaderLength)
125+
126+
// Create the iptc object to store the tags:
127+
data.iptc = new loadImage.IptcMap()
121128

122-
var startOffset = offset + 8 + nameHeaderLength
123-
var sectionLength = dataView.getUint16(offset + 6 + nameHeaderLength)
129+
// Parse the tags
130+
return loadImage.parseIptcTags(
131+
dataView,
132+
startOffset,
133+
sectionLength,
134+
data
135+
)
136+
137+
break;
138+
139+
}
140+
141+
offset++
142+
143+
}
124144

125-
// Create the iptc object to store the tags:
126-
data.iptc = new loadImage.IptcMap()
127-
window._iptc=data
145+
console.log('No Iptc data at this offset - could be XMP')
128146

129-
// Parse the tags
130-
loadImage.parseIptcTags(
131-
dataView,
132-
startOffset,
133-
sectionLength,
134-
data
135-
)
136147
}
137148

138149
// Registers this Iptc parser for the APP13 JPEG meta data segment:

0 commit comments

Comments
 (0)