Skip to content

Commit

Permalink
Added methods and script to show TIFF memory map
Browse files Browse the repository at this point in the history
  • Loading branch information
jabriffa committed Jun 14, 2018
1 parent 80b7385 commit e112d52
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cr2_memorymap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2015-2018 Johann A. Briffa
#
# This file is part of CR2_Scripts.
#
# CR2_Scripts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CR2_Scripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CR2_Scripts. If not, see <https://www.gnu.org/licenses/>.

import sys
import os
import argparse

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pyshared'))
import jbtiff

## main program

def main():
# interpret user options
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", required=True,
help="input raw file to analyse")
args = parser.parse_args()

# read input raw file
tiff = jbtiff.tiff_file(open(args.input, 'rb'))
# display memory map
mmap = tiff.get_memorymap()
print "*** Memory Map ***"
for offset, length, description in sorted(mmap):
print "%8d - %8d\t%8d\t%s" % (offset, offset+length-1, length, description)
print "*** END ***"

return

# main entry point
if __name__ == '__main__':
main()
47 changes: 47 additions & 0 deletions pyshared/jbtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,53 @@ def display(self, fid):
tiff_file.display_directory(fid, IFD)
return

# return memory map of objects in directory
@staticmethod
def get_memorymap_directory(IFD, prefix, parent=0):
mmap = []
for tag, (field_type, value_count, values, value_offset) in IFD.iteritems():
# we only need to add cases with external data
if not value_offset:
continue
# IFD entry tag
this_item = prefix + " Tag %d" % tag
if (parent,tag) in tiff_file.tag_name:
this_item += " [%s]" % tiff_file.tag_name[(parent,tag)]
# if this was a subdirectory, recurse
if isinstance(values, dict):
# add directory to memory map
length = 2+len(values)*12
mmap += tiff_file.get_memorymap_directory(values, this_item, tag)
else:
# data segment length
length = tiff_file.field_size[field_type] * value_count
# add to memory map
mmap.append((value_offset, length, this_item))
return mmap

# return memory map of all objects
def get_memorymap(self):
# start with an empty memory map
mmap = []
# work through all IFDs
for k, (IFD, ifd_offset, strips) in enumerate(self.data):
this_ifd = "IFD#%d" % k
# add entry for the directory itself
mmap.append((ifd_offset, 2+len(IFD)*12, this_ifd))
# add content of IFD
mmap += tiff_file.get_memorymap_directory(IFD, this_ifd)
# add data strips if present
if strips:
tag_offset, tag_length = tiff_file.get_strip_parameters(IFD)
assert len(IFD[tag_offset][2]) == len(strips)
assert len(IFD[tag_length][2]) == len(strips)
for i, strip in enumerate(strips):
# check IFD data
assert IFD[tag_length][2][i] == len(strip)
# add to memory map
mmap.append((IFD[tag_offset][2][i], IFD[tag_length][2][i], this_ifd + " strip %d" % i))
return mmap

# print formatted data to stream
def save_data(self, basename):
# go through all IFDs in file
Expand Down

0 comments on commit e112d52

Please sign in to comment.