Skip to content

Instantly share code, notes, and snippets.

@mutterer
Last active May 9, 2023 20:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutterer/5fbddc293d6c969a9d02778f1551b73f to your computer and use it in GitHub Desktop.
Save mutterer/5fbddc293d6c969a9d02778f1551b73f to your computer and use it in GitHub Desktop.
An ImageJ macro to read 'timestamps' and 'events' from CZI files. Request from Julien Cau and Emmanuel Perisse.
// ImageJ macro to read 'timestamps' and 'events' from CZI files.
path = File.openDialog("Select CZI File");
s = File.openAsRawString(path, File.length(path));
o = indexOf(s,"CZTIMS");
o=o+0xcc;
n = parseInt(read32bAt(o));
timestamps = newArray(n);
for (i=0;i<n;i++) {
offset = o+4+i*8;
timestamps[i] = parseFloat(readDoubleAt(offset));
if (i==0) timeOffset = timestamps[i];
timestamps[i] = timestamps[i] - timeOffset;
}
o = indexOf(s,"CZEVL");
o=o+0xcc;
n = parseInt(read32bAt(o));
events = newArray(n);
eventNames = newArray(n);
o=o+4;
for (i=0;i<n;i++) {
entryLength = parseInt(read32bAt(o));
o=o+4;
eventTime = parseFloat(readDoubleAt(o)) - timeOffset;
o=o+8;
eventType = parseInt(read32bAt(o));
o=o+4;
nameLength = parseInt(read32bAt(o));
o=o+4;
eventName = substring(s,o,o+nameLength);
o=o+nameLength;
events[i]=eventTime;
eventNames[i]=eventName;
}
index = Array.getSequence(timestamps.length);
Array.show("Timestamps and Events",index, timestamps, events,eventNames);
function readDoubleAt(offset) {
byte = substring(s, offset,offset+8);
b = newArray(8);
for (j=0;j<8;j++) b[j] = charCodeAt(byte,7-j);
e = (b[0]*16+b[1]>>4)-1023;
f1 = toHex(b[7]+b[6]<<8+b[5]<<16+b[4]<<24);
f0 = toHex(b[3]+b[2]<<8+((b[1]&0x0f)|0x10)<<16 );
while (lengthOf(f1)<8) { f1 = "0"+f1; }
f = f0+f1;
code = "e="+e+"; f=0x"+f+"; d=pow(2,e)*f*pow(2,-52); return ''+d;";
return eval (code);
}
function read32bAt(offset) {
byte = substring(s, offset,offset+4);
b = newArray(4);
for (j=0;j<4;j++) b[j] = charCodeAt(byte,3-j);
return ""+(b[0]<<24+b[1]<<16+b[2]<<8+b[3]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment