-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNG.py
executable file
·498 lines (411 loc) · 13.9 KB
/
DNG.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from struct import unpack
from os.path import splitext
class Logging:
CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
def __init__(self):
self.level = 0
def get_log_function(level):
def f(string):
if level >= self.level:
self.log(string)
return f
for levelname in ['critical', 'error', 'warning', 'info', 'debug']:
level = Logging.__dict__[levelname.upper()]
f = get_log_function(level)
setattr(self, levelname, f)
def basicConfig(self, level):
self.level = level
def log(self, string):
print(string)
def attributes(self, obj):
res = ""
for attr_name in dir(obj):
res += "%s: " % attr_name
try:
value = getattr(obj, attr_name)
if type(value) in (list, dict):
from pprint import pformat
res += pformat(value)
else:
res += str(value)
except Exception as e:
res += 'Error %s' % e
res += '\n'
return res
logging = Logging()
logging.basicConfig(level=logging.INFO)
class Tag:
BYTE = 1
ASCII = 2
SHORT = 3
LONG = 4
RATIONAL = 5
UNDEFINED = 7
PreviewImage = 46 # Specific tag for Panasonic RW2 files
SubFileType = 254
ImageWidth = 256
ImageLength = 257
BitsPerSample = 258
Compression = 259
Photometric = 262
Make = 271
Model = 272
StripOffsets = 273
Orientation = 274
SamplesPerPixel = 277
RowsPerStrip = 278
StripByteCounts = 279
XResolution = 282
YResolution = 283
ResolutionUnit = 296
DateTime = 306
TileWidth = 322
TileLength = 323
TileOffsets = 324
TileByteCounts = 325
SubIFD = 330
JPEGInterchangeFormat = 513
JPEGInterchangeFormatLength = 514
ExifTag = 34665
PixelXDimension = 40962
PixelYDimension = 40963
type_lengths = {BYTE: 1, ASCII: 1, SHORT: 2, LONG: 4,
RATIONAL: 8, UNDEFINED: 1}
def tag_name(self):
try:
return self.tag_dict[self.tag]
except:
return str(self.tag)
def __init__(self, tag, tag_type, count, value, file):
self.tag = tag
self.type = tag_type
self.count = count
self.value = value
self.file = file
self.read_function = {
self.BYTE: file.read_byte,
self.ASCII: file.read_ascii,
self.SHORT: file.read_short,
self.LONG: file.read_long,
self.RATIONAL: file.read_rational,
self.UNDEFINED: file.read_ascii
}
def unsupported(self, c=0):
logging.debug(
"Unsupported type %d for tag %s" % (
self.type, self.tag_name()))
raise NotImplementedError
def read_value(self):
if self.type_lengths[self.type]*self.count <= 4:
return
self.file.seek(self.value)
readf = self.read_function[self.type]
self.offset = self.value
try:
if self.count == 1 or self.type in (self.ASCII, self.UNDEFINED):
self.value = readf(self.count)
return
c = self.count
v = []
while c:
v.append(readf(self.count))
c -= 1
self.value = v
except NotImplementedError:
return
def __str__(self):
if self.type != self.UNDEFINED:
value = self.value
else:
value = ":".join("{:02x}".format(ord(c)) for c in self.value)
return "Tag %s: %s" % (self.tag_name(self.tag), value)
Tag.tag_dict = {number: tag_name for tag_name, number
in Tag.__dict__.iteritems()
if type(number) == int}
class IFD(object):
def __init__(self, dng, offset):
dng.seek(offset)
self.offset = offset
self.dng = dng
n_tags = dng.read_short()
self.entries = {}
self.entry_list = []
buf = dng.read(n_tags*12)
shortf = dng.shortf
longf = dng.longf
n = 0
SHORT = Tag.SHORT
BYTE = Tag.BYTE
ASCII = Tag.ASCII
while n < n_tags:
o = n*12
tag = unpack(shortf, buf[o:o+2])[0]
type = unpack(shortf, buf[o+2:o+4])[0]
count = unpack(longf, buf[o+4:o+8])[0]
if type == SHORT:
value = unpack(shortf, buf[o+8:o+10])[0]
elif type == BYTE:
value = buf[o+8]
elif type == ASCII and count <= 4:
value = buf[o+8:o+8+count]
else:
value = unpack(longf, buf[o+8:o+12])[0]
tag_obj = Tag(tag, type, count, value, dng)
tag_obj.value_is_checked = False
tag_name = tag_obj.tag_name()
self.entries[tag_name] = tag_obj
self.entry_list.append(tag_name)
n += 1
if self.entry_list[-1] == '0':
# At least the HTD diamond counts the next tag as one of the tags
self.next = 0
else:
self.next = dng.read_long()
def __getattr__(self, attr):
if attr == 'Width':
return self.ImageWidth if hasattr(self, 'ImageWidth') else -1
elif attr == 'Length':
return self.ImageLength if hasattr(self, 'ImageLength') else -1
elif attr == 'Size':
if hasattr(self, 'StripByteCounts'):
return self.StripByteCounts
elif hasattr(self, 'TileByteCounts'):
if type(self.TileByteCounts) == int:
s = self.TileByteCounts
else:
s = sum(self.TileByteCounts)
else:
s = -1
return s
try:
entry = self.entries[attr]
except:
raise AttributeError
if entry.value_is_checked is True:
return entry.value
else:
try:
entry.read_value()
entry.value_is_checked = True
except:
logging.debug("Unable to read value for tag %s" % attr)
raise NotImplementedError
return entry.value
def __str__(self):
w = self.Width
l = self.Length
t = self.SubFileType if hasattr(self, 'SubFileType') else -1
c = self.Compression if hasattr(self, 'Compression') else -1
s = self.Size
return "%dx%d, Type %d, compr: %d, size: %d" % (w, l, t, c, s)
def dump(self):
res = "Offset: %d -> Next: %d\n" % (self.offset, self.next)
for entry in self.entry_list:
tag = self.entries[entry]
try:
value = str(getattr(self, entry))
if tag.type == tag.UNDEFINED:
value = ":".join("{:02x}".format(ord(c)) for c in value)
if len(value) > 60:
value = value[:60]+" ..."
res += "%s: %s\n" % (entry, value)
except NotImplementedError:
pass
return res
class DNG:
def wrong_format(self):
logging.error("Invalid file format in file %s" % self.f.name)
raise IOError
def set_endian(self, endian):
if endian == 'II':
endianc = '<'
elif endian == 'MM':
endianc = '>'
else:
self.wrong_format()
self.longf = endianc + 'L'
self.shortf = endianc + 'H'
def fileno(self):
return self.f.fileno()
def seek(self, offset):
self.f.seek(self.offset + offset)
def read(self, count):
return self.f.read(count)
def read_byte(self, c=0):
return ord(self.f.read(1))
def read_ascii(self, count):
return self.f.read(count)[:-1]
def read_short(self, c=0):
return unpack(self.shortf, self.f.read(2))[0]
def read_long(self, c=0):
return unpack(self.longf, self.f.read(4))[0]
def read_rational(self, c=0):
return float(unpack(self.longf, self.f.read(4))[0]) \
/ unpack(self.longf, self.f.read(4))[0]
def __init__(self, path='', offset=0, exif=False):
self.exif = exif # If True we are opening the exif IFD from a JPEG
if path:
self.open(path, offset)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def open(self, source, offset=0):
try:
self.close()
except:
pass
self.f = open(source, "rb")
self.offset = offset
self.f.seek(offset)
endian = self.f.read(2)
self.set_endian(endian)
magic = self.read_short()
if magic not in (42, 85): # Tiff/DNG, RW2
logging.error("Unrecognized magic number %d" % magic)
self.wrong_format()
self.first_ifdo = self.read_long()
return self
def close(self):
self.f.close()
del self.f
def __del__(self):
try:
self.close()
except:
pass
def get_image(self, offset):
return IFD(self, offset)
def get_first_image(self):
return self.get_image(self.first_ifdo)
def get_images(self):
res = []
ifdo_list = [self.first_ifdo]
while len(ifdo_list):
ifdo = ifdo_list.pop(0)
if not ifdo:
break
try:
ifd = IFD(self, ifdo)
res.append(ifd)
def append_ifd(list, tag):
# It can either be a tag or a list of tags
try:
list = tag + list
except TypeError:
list = [tag] + list
return list
try:
ifdo_list = append_ifd(ifdo_list, ifd.SubIFD)
except:
pass
try:
ifdo_list = append_ifd(ifdo_list, ifd.ExifTag)
except:
pass
except:
pass
ifd.next and ifdo_list.append(ifd.next)
try:
res.sort(cmp=lambda x, y: cmp(x.ImageWidth*x.ImageLength,
y.ImageWidth*y.ImageLength))
except (KeyError, AttributeError):
pass # Exif images don't seem to have it
return res
def get_previews(self):
return [i for i in self.get_images()
if self.exif
or hasattr(i, 'SubFileType') and i.SubFileType == 1]
def get_jpeg_previews(self):
return [i for i in self.get_previews()
if hasattr(i, 'Compression') and i.Compression in (7, 6)]
def read_jpeg_preview(self, index=0):
try:
jpg = self.get_jpeg_previews()[index]
if hasattr(jpg, 'StripOffsets') and hasattr(jpg, 'StripByteCounts'):
self.seek(jpg.StripOffsets)
return self.read(jpg.StripByteCounts)
elif hasattr(jpg, 'JPEGInterchangeFormat') \
and hasattr(jpg, 'JPEGInterchangeFormatLength'):
self.seek(jpg.JPEGInterchangeFormat)
return self.read(jpg.JPEGInterchangeFormatLength)
except KeyError:
logging.error("No jpeg preview in %s" % self.f.name)
raise IOError
def dump(self):
for i in self.get_images():
print i.dump()
def __getattr__(self, attr):
if attr == 'Orientation':
try:
return self.get_first_image().Orientation
except:
return self.get_jpeg_previews()[-1].Orientation
else:
raise AttributeError
def JPG(path, offset=0):
# TODO would be a lot better to parse jpg applications
try:
dng = DNG(path, offset=offset+12, exif=True)
except:
dng = DNG(path, offset=offset+30, exif=True)
return dng
class Preview:
def __init__(self, path=''):
ext = splitext(path)[1].lower()
if ext == '.dng':
img = DNG(path)
elif ext in ['.jpg', '.jpeg']:
img = JPG(path)
elif ext == ".rw2":
with DNG(path) as img:
ifd = img.get_first_image()
# TODO This is not elegant. The caller should not need to know
# about the entries dictionary
try:
offset = ifd.entries['PreviewImage'].offset
except:
# read_value hasn't been called yet and so the offset
# property is not present
offset = ifd.entries['PreviewImage'].value
img = JPG(path, offset=offset)
else:
raise NotImplementedError('Unknown extension %s' % ext)
self.img = img
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.img.close()
def list(self):
for ifd in self.get_jpeg_previews():
print str(ifd)
def dump(self):
self.img.dump()
def get_jpeg_previews(self):
return self.img.get_jpeg_previews()
def read_jpeg_preview(self, index=0):
return self.img.read_jpeg_preview(index)
def __getattr__(self, attr):
if attr == 'Orientation':
return self.img.Orientation
else:
raise AttributeError
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description="Parse jpg, dng and rw2 files")
parser.add_argument("file", help="The image file to be parsed")
parser.add_argument("-d", "--dump", action='store_true',
help="Dump all the IFDs")
args = parser.parse_args()
preview = Preview(args.file)
if args.dump:
preview.dump()
preview.list()
print ("Orientation: %s" % preview.Orientation)