-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPAD-correct_IO.cpp
324 lines (272 loc) · 8.53 KB
/
SPAD-correct_IO.cpp
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
#include <windows.h>
#include <iostream>
#include "SPAD-correct_internal.h"
#include "SPAD-correct.h"
/* Load a sim file */
int SPAD_loadfile(char filepath[], BYTE** data, unsigned long long* nBytes)
{
FILE* fp = NULL;
long long filelen;
if (fopen_s(&fp, filepath, "rb") != 0) {
printf("Could not open file\n");
return -1;
}
_fseeki64(fp, 0, SEEK_END);
filelen = _ftelli64(fp);
if (filelen < 0) {
const int errmsglen = 256;
char errmsg[errmsglen];
strerror_s(errmsg, errmsglen, errno);
printf("ERROR: %s\n", errmsg);
}
else {
printf("File is %lld bytes.\n", filelen);
}
rewind(fp);
BYTE* file_bytes = (BYTE*)malloc(filelen);
if (!file_bytes) {
printf("Malloc error!\n");
return(-2);
}
fread_s(file_bytes, filelen, 1, filelen, fp);
fclose(fp);
*data = file_bytes;
*nBytes = filelen;
return 0;
}
/* load an ics image file */
int SPAD_load3DICSfile(char filepath[], USHORT** image, int* width, int* height, int* timebins)
{
ICS* ip;
Ics_DataType dt;
int ndims;
size_t dims[ICS_MAXDIM];
size_t bufsize;
void* buf;
Ics_Error retval;
retval = IcsOpen(&ip, filepath, "r");
if (retval != IcsErr_Ok) {
printf("SPAD_load3DICSfile ERROR: Cannot open ics file for reading.\n");
return(-1);
}
IcsGetLayout(ip, &dt, &ndims, dims);
if (dt != Ics_uint16) {
printf("SPAD_load3DICSfile ERROR: File not UINT16 data type.\n");
return(-2);
}
bufsize = IcsGetDataSize(ip);
buf = malloc(bufsize);
if (buf == NULL) {
printf("SPAD_load3DICSfile ERROR: Cannot malloc buffer.\n");
return(-3);
}
retval = IcsGetData(ip, buf, bufsize);
if (retval != IcsErr_Ok) {
printf("SPAD_load3DICSfile ERROR: IcsGetData failed.\n");
return(-4);
}
retval = IcsClose(ip);
if (retval != IcsErr_Ok) {
printf("SPAD_load3DICSfile ERROR: Cannot close ics file.\n");
return(-5);
}
if (ndims != 3) {
printf("SPAD_load3DICSfile ERROR: Not a 3D image file, %d dims detected\n", ndims);
return(-6);
}
*height = (int)dims[2];
*width = (int)dims[1];
*timebins = (int)dims[0];
*image = (USHORT*)buf;
return(0);
}
/* load an ics image file into existing buffer - for Labview use*/
int SPAD_load3DICSfile_LV(char filepath[], USHORT* image, int width, int height, int timebins)
{
ICS* ip;
Ics_DataType dt;
int ndims;
size_t dims[ICS_MAXDIM];
size_t bufsize;
Ics_Error retval;
retval = IcsOpen(&ip, filepath, "r");
if (retval != IcsErr_Ok) {
printf("SPAD_load3DICSfile ERROR: Cannot open ics file for reading.\n");
return(-1);
}
IcsGetLayout(ip, &dt, &ndims, dims);
if (dt != Ics_uint16) {
printf("SPAD_load3DICSfile ERROR: File not UINT16 data type.\n");
return(-2);
}
// Check the image is of the expected size
if (dims[0] != timebins) {
printf("SPAD_load3DICSfile ERROR: Expected %d timebins, found %d.\n", timebins, dims[0]);
return(-11);
}
if (dims[1] != width) {
printf("SPAD_load3DICSfile ERROR: Expected %d width, found %d.\n", width, dims[1]);
return(-11);
}
if (dims[2] != height) {
printf("SPAD_load3DICSfile ERROR: Expected %d height, found %d.\n", height, dims[2]);
return(-11);
}
bufsize = IcsGetDataSize(ip);
if (image == NULL) {
printf("SPAD_load3DICSfile ERROR: Invalid image array supplied.\n");
return(-3);
}
retval = IcsGetData(ip, (void*)image, bufsize);
if (retval != IcsErr_Ok) {
printf("SPAD_load3DICSfile ERROR: IcsGetData failed.\n");
return(-4);
}
retval = IcsClose(ip);
if (retval != IcsErr_Ok) {
printf("SPAD_load3DICSfile ERROR: Cannot close ics file.\n");
return(-5);
}
if (ndims != 3) {
printf("SPAD_load3DICSfile ERROR: Not a 3D image file, %d dims detected\n", ndims);
return(-6);
}
return(0);
}
int SPAD_save3DICSfile(char filepath[], USHORT* histogram, int width, int height, int timebins, int compression_level,
BYTE* header, unsigned long long max_header_bytes, double xy_microns_per_pixel, double ns_per_bin)
{
ICS* imagefile;
Ics_Error retval;
size_t dims[3] = { (size_t)timebins, (size_t)width, (size_t)height };
size_t histsize = width * height * timebins * sizeof(USHORT);
retval = IcsOpen(&imagefile, filepath, "w2");
if (retval != IcsErr_Ok) {
printf("SPAD_save3DICSfile ERROR: Cannot open ics file for writing.\n");
return -1;
}
IcsSetLayout(imagefile, Ics_uint16, 3, dims);
IcsSetData(imagefile, histogram, histsize);
if (compression_level == 0) {
IcsSetCompression(imagefile, IcsCompr_uncompressed, 0);
}
else {
IcsSetCompression(imagefile, IcsCompr_gzip, compression_level);
}
// Dimensions
char buffer1[ICS_LINE_LENGTH];
sprintf_s(buffer1, "%d %d %d", timebins, width, height);
if (header != NULL) { // add minimal header info
SPAD_readHeader(header, max_header_bytes, imagefile);
// Add correct values after sorting and transform
// Extents
/* Read from the header and scale values according to crop?
float x = 1.0f, y = 1.0f;
Ics_HistoryIterator it;
Ics_Error retval = IcsNewHistoryIterator(imagefile, &it, "extents");
if (retval == IcsErr_Ok) { // we found the extents line
char value[ICS_LINE_LENGTH];
retval = IcsGetHistoryKeyValueI(imagefile, &it, NULL, value);
if (retval == IcsErr_Ok) {
sscanf_s(value, "%f\t%f", &x, &y);
}
}
sprintf_s(buffer2, "%e %e %e", ns_per_bin * timebins * 1E-9, xy_microns_per_pixel * width * 1E-6, xy_microns_per_pixel * height * 1E-6);
*/
IcsDeleteHistory(imagefile, "type");
IcsDeleteHistory(imagefile, "labels");
IcsDeleteHistory(imagefile, "dimensions");
IcsDeleteHistory(imagefile, "extents");
IcsDeleteHistory(imagefile, "units");
}
// Extents
char buffer2[ICS_LINE_LENGTH];
sprintf_s(buffer2, "%e %e %e", ns_per_bin * timebins * 1E-9, xy_microns_per_pixel * width * 1E-6, xy_microns_per_pixel * height * 1E-6);
// Add essential metadata
IcsSetPosition(imagefile, 0, 0.0, ns_per_bin, "ns");
IcsSetPosition(imagefile, 1, 0.0, xy_microns_per_pixel, "microns");
IcsSetPosition(imagefile, 2, 0.0, xy_microns_per_pixel, "microns");
IcsAddHistory(imagefile, "author", "SPAD-sorter");
IcsAddHistory(imagefile, "type", "Time Resolved");
IcsAddHistory(imagefile, "labels", "t x y");
IcsAddHistory(imagefile, "dimensions", buffer1);
IcsAddHistory(imagefile, "extents", buffer2);
IcsAddHistory(imagefile, "units", "s m m");
retval = IcsClose(imagefile);
if (retval != IcsErr_Ok) {
printf("SPAD_save3DICSfile ERROR: Cannot close ics file.\n");
return -2;
}
return 0;
}
int SPAD_save2DICSfile(char filepath[], void* buffer, int width, int height, int bit_depth, int compression_level,
BYTE* header, unsigned long long max_header_bytes, double xy_microns_per_pixel)
{
ICS* imagefile;
Ics_Error retval;
size_t dims[2] = { (size_t)width, (size_t)height };
size_t buffsize;
Ics_DataType data_type;
retval = IcsOpen(&imagefile, filepath, "w2");
if (retval != IcsErr_Ok) {
printf("SPAD_save2DICSfile ERROR: Cannot open ics file for writing.\n");
return -1;
}
switch (bit_depth)
{
case 8:
data_type = Ics_uint8;
buffsize = width * height * sizeof(UCHAR);
break;
case 16:
data_type = Ics_uint16;
buffsize = width * height * sizeof(USHORT);
break;
case 32:
data_type = Ics_uint32;
buffsize = width * height * sizeof(UINT);
break;
default:
break;
}
IcsSetLayout(imagefile, data_type, 2, dims);
IcsSetData(imagefile, buffer, buffsize);
if (compression_level == 0) {
IcsSetCompression(imagefile, IcsCompr_uncompressed, 0);
}
else {
IcsSetCompression(imagefile, IcsCompr_gzip, compression_level);
}
if (header == NULL) { // add minimal header info
IcsAddHistory(imagefile, "author", "SPAD-sorter");
IcsAddHistory(imagefile, "type", "Intensity");
IcsAddHistory(imagefile, "labels", "x y");
}
else {
SPAD_readHeader(header, max_header_bytes, imagefile);
// Dimensions
char buffer1[ICS_LINE_LENGTH];
sprintf_s(buffer1, "%d %d", width, height);
// Extents
char buffer2[ICS_LINE_LENGTH];
sprintf_s(buffer2, "%e %e", xy_microns_per_pixel * width * 1E-6, xy_microns_per_pixel * height * 1E-6);
IcsSetPosition(imagefile, 0, 0.0, xy_microns_per_pixel, "microns");
IcsSetPosition(imagefile, 1, 0.0, xy_microns_per_pixel, "microns");
IcsDeleteHistory(imagefile, "type");
IcsDeleteHistory(imagefile, "labels");
IcsDeleteHistory(imagefile, "dimensions");
IcsDeleteHistory(imagefile, "extents");
IcsDeleteHistory(imagefile, "units");
IcsAddHistory(imagefile, "type", "Intensity");
IcsAddHistory(imagefile, "labels", "x y");
IcsAddHistory(imagefile, "dimensions", buffer1);
IcsAddHistory(imagefile, "extents", buffer2);
IcsAddHistory(imagefile, "units", "m m");
}
retval = IcsClose(imagefile);
if (retval != IcsErr_Ok) {
printf("SPAD_save2DICSfile ERROR: Cannot close ics file.\n");
return -2;
}
return 0;
}