-
Notifications
You must be signed in to change notification settings - Fork 1
/
DVreader.ijm
292 lines (255 loc) · 7.38 KB
/
DVreader.ijm
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
// Importing Deltavision .dv file (DVreader.txt)
// Kota Miura (miura@embl.de)
//20050513
// Header size of .dv file is calculated from the width and height of the frame, frame number.
// General rule is: static header length 528pixels (1056bytes) + dynamic headers (80 pixels/frame, 160 bytes/frame)
// information for each frame is contained in the first 20 pixels (40bytes) and header is terminated at
// multiples of 512 pizels (1028bytes).
// * header length is somehow related to the number contained in [46,0] but there are exceptions
// (header length=((lengthPrimer/512/2+1)*512*2))
// [0,0] width
// [2,0] height
// [4,0] frames
// [6,0] depth (z)
// [90,0] time-points
// [98,0] channels
//UNSOLVED PROBLEM: if the sequence is the one that was force-terminated during the acquisition, header length is shorter than
// the value calculated by the above method. The sequence will not be imported properly.
// 050526 added second method (estimate from primer).
// 050712 depends only on headerlength primer; 4bytes (32bit signed integer);
//050713 .r3d loader (Big endian)
//070612 "Read .dv header" added. Reads out only the header info, so that the accidentally terminated file can be loaded from [import-> Raw]
var headerlength=512;
var impH=100;
var impW=100;
var framenumber=1;
var pathname="";
var filename="";
var timepoints=10;
var channels=1;
var zslices=1;
var hexstr0="";
var hexstr1="";
var hexstr2="";
var hexstr3="";
var hexstrtop="";
var hexstrbot="";
function DvImport(HeaderMethod,byteOrder) {
requires("1.34j");
DVHeaderReader(HeaderMethod,byteOrder);
if (byteOrder==0){
imp_para=" image=[16-bit Unsigned] width="+impW+" height="+impH+" offset="+headerlength+" number="+framenumber+" gap=0 little-endian ";
}
else {
imp_para=" image=[16-bit Unsigned] width="+impW+" height="+impH+" offset="+headerlength+" number="+framenumber+" gap=0 ";
}
open_para="open="+pathname+filename+imp_para;
run("Raw...", open_para);
}
function DVHeaderReader(HeaderMethod,byteOrder) {
setBatchMode(true);
if (byteOrder==0) {
imp_para=" image=[16-bit Unsigned] width=110 height=1 offset=0 number=1 gap=0 little-endian ";
}
else {
imp_para=" image=[16-bit Unsigned] width=110 height=1 offset=0 number=1 gap=0 ";
}
open_para="open="+imp_para;
run("Raw...", open_para);
if (byteOrder==0) {
impW=getPixel(0,0);
impH=getPixel(2,0);
framenumber=getPixel(4,0);
lengthPrimer=getPixel(46,0);
lengthPrimer2=getPixel(47,0);
timepoints=getPixel(90,0);
channels=getPixel(98,0);
} else {
impW=getPixel(1,0);
impH=getPixel(3,0);
framenumber=getPixel(5,0);
lengthPrimer=getPixel(46,0);
lengthPrimer2=getPixel(47,0);
timepoints=getPixel(90,0);
channels=getPixel(98,0);
}
if (HeaderMethod==3) headerlength=Header3(framenumber,impW);
if (HeaderMethod==2) headerlength=Header2(lengthPrimer);
if (HeaderMethod==1) {
if (byteOrder==0) {
headerlength=Header1(lengthPrimer,lengthPrimer2);
} else {
headerlength=Header1BigEnd(lengthPrimer,lengthPrimer2);
}
}
zslices=framenumber/timepoints/channels;
pathname=getDirectory("image");
filename=getTitle();
close();
setBatchMode(false);
print("");
print(".dv importer "+pathname+filename);
print("Header Length:"+headerlength);
print("Channels:"+channels+" Time Points:"+timepoints+" Z slices:"+zslices);
print("width:"+impW+" height:"+impH+" Frames: "+framenumber);
}
function Header1(lengthPrimer,lengthPrimer2){
extendedHeader=TwoPixelDec2Hex(lengthPrimer,lengthPrimer2);
totalHeader=1024+extendedHeader;
totalHeaderPix=totalHeader/2;
return (totalHeader);
}
function Header1BigEnd(lengthPrimer,lengthPrimer2) {
extendedHeader=TwoPixelDec2HexBigEndian(lengthPrimer,lengthPrimer2);
totalHeader=1024+extendedHeader;
totalHeaderPix=totalHeader/2;
return (totalHeader);
}
//not working sometimes
function Header2(lengthPrimer){
return ((lengthPrimer/512/2+1)*512*2);
}
function Header3(framenumber,impW){
temp1=528+80*framenumber;
bit16=floor(temp1/512);
restHL=temp1-bit16*512;
//bit16=floor(temp1/512);
if (restHL>50) {
headerlen=512*(bit16+1)*2;
} else {
headerlen=512*bit16*2;
}
return (headerlen);
}
//050712
macro "Import .dv method1 [f1]" {
//DVHeaderReader();
DvImport(1,0);
}
macro "Import .r3d method1 [f2]" {
//DVHeaderReader();
DvImport(1,1);
}
macro "-" {}
//070612
macro "Read .dv Header" {
DVHeaderReader(1,0);
}
//macro "Import .dv method2 old" {
//// DVHeaderReader();
// DvImport(2,0);
//}
//macro "Import .dv method3 old" {
// DVHeaderReader();
// DvImport(3,0);
//}
//macro "Print Header" {
// printheaderCore();
//}
function printheaderCore() {
pixelnum=getNumber("How Many Pixels?",4096);
ww=getWidth();
wh=getHeight();
rownumber=floor(pixelnum/ww)+1;
if (ww<pixelnum) {
for (j=0;j<=rownumber;j++) {
if (j==rownumber) {
for (i=0;i<(pixelnum-ww*(rownumber-1));i++) {
currentpix=getPixel(i,j);
print(currentpix);
}
} else {
for (i=0;i<ww;i++) {
currentpix=getPixel(i,j);
print(currentpix);
}
}
}
} else {
for (i=0;i<pixelnum;i++) {
currentpix=getPixel(i,0);
print(currentpix);
}
}
}
//050712 conversion of two pixel values to a 4byte hex number.
//macro "print hex test" {
// //Dec2Hex(200);
// resultdec=TwoPixelDec2Hex(20480,18);
// print(resultdec);
//}
//050712 Reinterprets two values in 2 pixels positions to a 4byte integer.
function TwoPixelDec2Hex(firstpix,secondpix) {
Dec2Hex1(firstpix);
Dec2Hex2(secondpix);
totalHex=""+hexstr0+hexstr1+hexstr2+hexstr3;
return(hexs2dec(totalHex));
}
//050713 Reinterprets two values in 2 pixels positions to a 4byte integer.
// big-endian byte order
function TwoPixelDec2HexBigEndian(firstpix,secondpix) {
Dec2HexBE1(firstpix);
Dec2HexBE2(secondpix);
totalHex=""+hexstr0+hexstr1+hexstr2+hexstr3;
return(hexs2dec(totalHex));
}
//050712
function Dec2Hex1(dec) {
hexedstr=leftPad(toHex(dec), 4);
print(hexedstr);
hexstr2=""+substring(hexedstr,0,2);
hexstr3=""+substring(hexedstr,2,4);
// hexed=0+hexedstr;
// hextop=floor(hexed/100);
// hexbot=hexed-100*hextop;
// print(hexstr2);
// print(hexstr3);
}
//050712
function Dec2Hex2(dec) {
hexedstr=leftPad(toHex(dec), 4);
print(hexedstr);
hexstr0=""+substring(hexedstr,0,2);
hexstr1=""+substring(hexedstr,2,4);
// hexed=0+hexedstr;
// hextop=floor(hexed/100);
// hexbot=hexed-100*hextop;
// print(hexstr0);
// print(hexstr1);
}
//050713 big-endian
function Dec2HexBE1(dec) {
hexedstr=leftPad(toHex(dec), 4);
print(hexedstr);
hexstr0=""+substring(hexedstr,0,2);
hexstr1=""+substring(hexedstr,2,4);
}
//050712
function Dec2HexBE2(dec) {
hexedstr=leftPad(toHex(dec), 4);
print(hexedstr);
hexstr2=""+substring(hexedstr,0,2);
hexstr3=""+substring(hexedstr,2,4);
}
//050712 copied from PerkElnmer Opener
function leftPad(n, width) {
s =""+n;
while (lengthOf(s)<width)
s = "0"+s;
return s;
}
//050712 copied from PerkElnmer Opener
//041025 Kota hexadecimal string to decimal number
function hexs2dec(hexs) {
dec=0;
for(i=0;i<lengthOf(hexs);i++) {
s_unic=charCodeAt(hexs, i);
if (s_unic<58) {
digit=s_unic-48;//0=unicode 48
} else {
digit=s_unic-96+9;//a=unicode 97
}
dec+=pow(2,4*(lengthOf(hexs)-i-1))*digit;
}
return dec;
}