Skip to content

Instantly share code, notes, and snippets.

@evilpie
Created May 29, 2011 15:32
Show Gist options
  • Save evilpie/997861 to your computer and use it in GitHub Desktop.
Save evilpie/997861 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Depth Experiment</title>
</head>
<body>
<script>
var data = {"depths": [0.0, 0.12958113849163055, 0.24288934469223022, 0.41884708404541016, 0.8312229514122009, 1.0], "width": 1200, "depthmap": [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.997050404548645, 0.912054181098938, 1.0, 1.0, 1.0, 1.0, 1.0, 0.996872067451477, 1.0, 1.0, 0.8829076290130615, 0.8656907677650452], [1.0, 1.0, 1.0, 0.9910506010055542, 1.0, 1.0, 1.0, 1.0, 0.3564097583293915, 0.7398068904876709, 1.0, 1.0, 0.9993961453437805, 1.0, 1.0, 0.9813909530639648, 0.9962142705917358, 0.9825999140739441, 0.8920065760612488, 0.9654608964920044], [1.0, 1.0, 1.0, 0.9991970658302307, 1.0, 1.0, 0.9978106617927551, 0.879845142364502, 0.1211555153131485, 0.8312267065048218, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.9779077768325806, 0.9561600685119629, 1.0], [1.0, 1.0, 1.0, 0.9976791143417358, 0.993761420249939, 1.0, 0.9646276235580444, 0.8590946197509766, 0.08840379863977432, 0.39600735902786255, 0.9880483150482178, 1.0, 0.9822200536727905, 0.9887795448303223, 1.0, 0.9877327084541321, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.9869758486747742, 1.0, 0.41884708404541016, 0.0014709210954606533, 0.08250416070222855, 0.7848181128501892, 0.9805268049240112, 0.9805268049240112, 0.9854349493980408, 0.9844462871551514, 0.9831809997558594, 0.9929143190383911, 0.99688720703125, 0.9964606761932373, 1.0], [1.0, 1.0, 1.0, 1.0, 0.9946131110191345, 0.9805268049240112, 0.9805268049240112, 0.41884708404541016, 0.012389078736305237, 0.05775389447808266, 0.37843045592308044, 0.947185754776001, 0.9805268049240112, 0.9971433877944946, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9808700680732727, 0.9810212850570679, 0.9256747364997864], [0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.41884708404541016, 0.0, 0.00925413053482771, 0.13248296082019806, 0.7725108861923218, 0.9805268049240112, 0.9805268049240112, 0.9832039475440979, 0.9912051558494568, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9245195984840393], [0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9791375994682312, 0.9687402844429016, 0.7594953179359436, 0.08158856630325317, 0.0, 0.0, 0.07829027622938156, 0.18991078436374664, 0.9783287644386292, 0.8604676723480225, 0.9146515130996704, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9191548824310303], [0.9800124168395996, 0.9805268049240112, 0.9805268049240112, 0.9014337658882141, 0.2794594466686249, 0.1589774787425995, 0.0, 0.0, 0.0, 0.0, 0.01438880991190672, 0.29408714175224304, 0.8843189477920532, 0.2597549855709076, 0.41884708404541016, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9323828816413879], [0.9795849919319153, 0.9805268049240112, 0.9805268049240112, 0.7398068904876709, 0.12978005409240723, 0.0, 0.0, 0.0, 0.0020346646197140217, 0.0, 0.0, 0.19289930164813995, 0.23758459091186523, 0.13446488976478577, 0.17598026990890503, 0.9154567122459412, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9411311745643616], [0.9681546688079834, 0.9805268049240112, 0.9805268049240112, 0.41884708404541016, 0.11991561204195023, 0.0, 0.0, 0.0, 0.014405070804059505, 0.0, 0.0, 0.04966122657060623, 0.06647779792547226, 0.09370265901088715, 0.14538660645484924, 0.7398068904876709, 0.9805268049240112, 0.9805268049240112, 0.9805268049240112, 0.9345185160636902], [0.967429518699646, 0.9768522381782532, 0.916610062122345, 0.8392140865325928, 0.41884708404541016, 0.0423114039003849, 0.0734100341796875, 0.013852576725184917, 0.017647264525294304, 0.0, 0.0, 0.017647264525294304, 0.017647264525294304, 0.038980670273303986, 0.11269232630729675, 0.41884708404541016, 0.920699417591095, 0.9292513132095337, 0.9324819445610046, 0.9285408854484558], [0.9222120642662048, 0.9227610230445862, 0.9002867937088013, 0.8997946381568909, 0.8250468373298645, 0.41884708404541016, 0.3250907063484192, 0.2041032761335373, 0.08393333852291107, 0.0, 0.0, 0.022835280746221542, 0.018580729141831398, 0.017647264525294304, 0.08643857389688492, 0.39792442321777344, 0.9004517793655396, 0.9020387530326843, 0.9157528281211853, 0.896843433380127], [0.8953638076782227, 0.8926278352737427, 0.8954646587371826, 0.9002867937088013, 0.8928878903388977, 0.8910471796989441, 0.8412646651268005, 0.809010922908783, 0.8372160196304321, 0.41884708404541016, 0.039061289280653, 0.03061079978942871, 0.018128976225852966, 0.017647264525294304, 0.08510582149028778, 0.25925055146217346, 0.8980698585510254, 0.9011489152908325, 0.9204343557357788, 0.9002867937088013], [0.8963058590888977, 0.8929592967033386, 0.8208149671554565, 0.8734646439552307, 0.8892267346382141, 0.8303439021110535, 0.884494423866272, 0.8200468420982361, 0.8478772640228271, 0.8333333730697632, 0.7947889566421509, 0.17884929478168488, 0.0006441305740736425, 0.017647264525294304, 0.07528197765350342, 0.1752147376537323, 0.8867035508155823, 0.8797844052314758, 0.8968508839607239, 0.8917601704597473], [0.854873776435852, 0.8407673835754395, 0.8012401461601257, 0.838948130607605, 0.8492759466171265, 0.8227787017822266, 0.8293670415878296, 0.820576548576355, 0.8077040910720825, 0.8200468420982361, 0.8200468420982361, 0.8177023530006409, 0.08130478113889694, 0.017647303640842438, 0.04605516418814659, 0.13723574578762054, 0.845832884311676, 0.8413910269737244, 0.827416718006134, 0.8359405398368835], [0.8196958303451538, 0.8174713850021362, 0.8330395221710205, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8217111825942993, 0.813824474811554, 0.8057525753974915, 0.820210874080658, 0.8200468420982361, 0.8200468420982361, 0.30516016483306885, 0.0, 0.026789098978042603, 0.11218960583209991, 0.8097101449966431, 0.8330249786376953, 0.850508987903595, 0.8200468420982361], [0.8200468420982361, 0.8157762885093689, 0.8197040557861328, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.7398068904876709, 0.03877004235982895, 0.020000150427222252, 0.14106254279613495, 0.8193905353546143, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361], [0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8175785541534424, 0.7398068904876709, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.7398068904876709, 0.12029226124286652, 0.41884708404541016, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361], [0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8146011829376221, 0.78768390417099, 0.7554335594177246, 0.8139940500259399, 0.8166132569313049, 0.819886326789856, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8185833096504211, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361, 0.8200468420982361]], "frames": 6, "height": 800};
var boxw = data.width / data.depthmap.length, boxh = data.height / data.depthmap[0].length;
var x = 0, y = 0, i, j, k;
var div, frame, image;
image = document.createElement('img');
image.src = 'frame_0.jpg';
document.body.appendChild(image);
for (i = 0; i < data.depthmap.length; i++) {
for (j = 0; j < data.depthmap[i].length; j++) {
div = document.createElement('div');
//console.log(data.depthmap[i][j]);
for (k = 1; k <= data.depths.length; k++) {
if (data.depths[k - 1] <= data.depthmap[i][j] && data.depthmap[i][j] <= data.depths[k])
frame = k;
}
div.style.width = boxw;
div.style.height = boxh;
div.style.position = 'absolute';
div.style.top = image.x + x;
div.style.left = image.y + y;
//div.style.backgroundColor = '#0000' + ((Math.random() * 255) >> 0).toString(16)
div.style.backgroundColor = 'transparent';
div.onmouseover = (function (frame) {
return function () {
console.log('change' + frame);
image.src = 'frame_' + frame + '.jpg';
}
})(frame)
//console.log(frame);
div.style.innerHTML = '<b>' + data.depthmap[i][j]; + '</b>'
document.body.appendChild(div)
x += boxw;
}
x = 0;
y += boxh;
}
</script>
</body>
</html>
import struct
import zlib
import json
f = open('lytro_50_00087.dat', 'rb')
class SimpleReader:
def __init__(self, data):
self.data = data
self.index = 0
def read(self, fmt, count = 1):
size = struct.calcsize(fmt) * count
data = struct.unpack(fmt * count, self.data[self.index:self.index + size])
self.index += size
return data
while True:
try:
section, length = struct.unpack('ii', f.read(8))
except Exception as e:
break
print 'Section', section
print 'Length', length
data = f.read(length)
if section == 0x1: # the depth information
data = zlib.decompress(data, 15)
reader = SimpleReader(data)
version, mode = reader.read('i', 2)
print 'Version', version, 'Mode', mode
if mode == 0x1:
count, width, height = reader.read('i', 3)
print 'Width', width, 'Height', height
depths = reader.read('f', count)
offsets = reader.read('f', count)
else:
raise NotImplementedError('mode 2')
depth, duration = reader.read('f', 2)
photographer, = reader.read('i')
assert photographer == 0
scripts, = reader.read('i')
reader.read('f', scripts * 2) # dummy
depthMapX, depthMapY = reader.read('i', 2)
depthMap = [[reader.read('f')[0] for i in range(0, depthMapY)] for j in range(0, depthMapX)]
elif section == 0x2: # the jpeg image
frame = struct.unpack('i', data[0:4])
open('frame_%d.jpg' % frame, 'wb').write(data[4:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment