Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jabriffa committed Jun 13, 2018
1 parent ef55e22 commit 86b9fca
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions cr2_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pyshared'))
import jbtiff

## component functions

def replace_ifd(tiff, k, data):
print "IFD#%d: Replacing data strip with length %d" % (k, len(data))
# get references to required IFD
IFD, ifd_offset, strips = tiff.data[k]
# replace data strips with new data
assert strips
del strips[:]
strips.append(data)
# update IFD data
if 273 in IFD:
assert 279 in IFD
assert 513 not in IFD and 514 not in IFD
IFD[279] = (4, 1, [len(data)], 0)
elif 513 in IFD:
assert 514 in IFD
assert 273 not in IFD and 279 not in IFD
IFD[514] = (4, 1, [len(data)], 0)
else:
raise AssertionError("Reference to data strip not found in IFD#%d" % k)
return

## main program

def main():
Expand All @@ -40,29 +63,12 @@ def main():

# read input raw file
tiff = jbtiff.tiff_file(open(args.input, 'rb'))
# replace data strips for main sensor image (IFD#3)
k = 3
IFD, ifd_offset, strips = tiff.data[k]

# read input data file
with open(args.sensor, 'rb') as fid:
data = fid.read()
print "IFD#%d: Replacing data strip with length %d" % (k, len(data))
# replace data strips with new data
assert strips
del strips[:]
strips.append(data)
# update IFD data
if 273 in IFD:
assert 279 in IFD
assert 513 not in IFD and 514 not in IFD
IFD[279] = (4, 1, [len(data)], 0)
elif 513 in IFD:
assert 514 in IFD
assert 273 not in IFD and 279 not in IFD
IFD[514] = (4, 1, [len(data)], 0)
else:
raise AssertionError("Reference to data strip not found in IFD#%d" % k)

# replace data strips for main sensor image (IFD#3)
replace_ifd(tiff, 3, data)

# save updated CR2 file
tiff.write(open(args.output,'wb'))
Expand Down

0 comments on commit 86b9fca

Please sign in to comment.