private Dictionary<string, object> extractTags(long IFD_offset, Dictionary<int, string> tags2extract)
{
long length = data.SHORT(IFD_offset), i, ii;
long type, count, tagOffset, offset;
string tag;
string[] values;
Dictionary<string, object> hash = new Dictionary<string, object>();
for (i = 0; i < length; i++) {
// Set binary reader pointer to beginning of the next tag
offset = tagOffset = IFD_offset + 12 * i + 2;
if (!tags2extract.TryGetValue((int)data.SHORT(offset), out tag)) {
continue; // Not the tag we requested
}
type = data.SHORT(offset+=2);
count = data.LONG(offset+=2);
values = new string[count];
offset += 4;
switch (type) {
case 1: // BYTE
case 7: // UNDEFINED
if (count > 4) {
offset = data.LONG(offset) + offsets["tiffHeader"];
}
for (ii = 0; ii < count; ii++) {
values[ii] = data.BYTE(offset + ii).ToString();
}
break;
case 2: // STRING
if (count > 4) {
offset = data.LONG(offset) + offsets["tiffHeader"];
}
hash.Add(tag, data.STRING(offset, (int)(count - 1)));
continue;
case 3: // SHORT
if (count > 2) {
offset = data.LONG(offset) + offsets["tiffHeader"];
}
for (ii = 0; ii < count; ii++) {
values[ii] = data.SHORT(offset + ii*2).ToString();
}
break;
case 4: // LONG
if (count > 1) {
offset = data.LONG(offset) + offsets["tiffHeader"];
}
for (ii = 0; ii < count; ii++) {
values[ii] = data.LONG(offset + ii*4).ToString();
}
break;
case 5: // RATIONAL
offset = data.LONG(offset) + offsets["tiffHeader"];
for (ii = 0; ii < count; ii++) {
values[ii] = ((double)data.LONG(offset + ii*8)/(double)data.LONG(offset + ii*8 + 4)).ToString();
}
break;
case 9: // SLONG
offset = data.LONG(offset) + offsets["tiffHeader"];
for (ii = 0; ii < count; ii++) {
values[ii] = data.SLONG(offset + ii*4).ToString();
}
break;
case 10: // SRATIONAL
offset = data.LONG(offset) + offsets["tiffHeader"];
for (ii = 0; ii < count; ii++) {
values[ii] = ((double)data.SLONG(offset + ii*8)/(double)data.SLONG(offset + ii*8 + 4)).ToString();
}
break;
default:
continue;
}
object details;
string description;
if (values.Length == 1)
{
if (tagDescs.TryGetValue(tag, out details))
{
if (details is Dictionary<int, string> &&
((Dictionary<int, string>)details).TryGetValue(Convert.ToInt32(values[0]), out description) ||
details is Dictionary<string, string> &&
((Dictionary<string, string>)details).TryGetValue(values[0], out description))
{
hash[tag] = description;
}
}
else
{
hash[tag] = values[0];
}
}
else
{
hash.Add(tag, values);
}
}
return hash;
}