Debian Codegraph @

ptufile/_ptufile.pyx

raw
1 # _ptufile.pyx
2 # distutils: language = c
3 # cython: boundscheck = False
4 # cython: wraparound = False
5 # cython: cdivision = True
6 # cython: nonecheck = False
7 # cython: freethreading_compatible = True
8
9 # Copyright (c) 2023-2026, Christoph Gohlke
10 # All rights reserved.
11 #
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions are met:
14 #
15 # 1. Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the following disclaimer.
17 #
18 # 2. Redistributions in binary form must reproduce the above copyright notice,
19 # this list of conditions and the following disclaimer in the documentation
20 # and/or other materials provided with the distribution.
21 #
22 # 3. Neither the name of the copyright holder nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
30 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 # POSSIBILITY OF SUCH DAMAGE.
37
38 """Decode PicoQuant Time-Tagged Time-Resolved (TTTR) records."""
39
40 from libc.math cimport round
41 from libc.stdint cimport (
42 UINT64_MAX,
43 int8_t,
44 int16_t,
45 uint8_t,
46 uint16_t,
47 uint32_t,
48 uint64_t,
49 )
50
51
52 cdef packed struct t2_t:
53 uint64_t time
54 int8_t channel
55 uint8_t marker
56 # uint8_t[6] _align
57
58 cdef packed struct t3_t:
59 uint64_t time
60 int16_t dtime
61 int8_t channel
62 uint8_t marker
63 # uint8_t[4] _align
64
65 ctypedef uint32_t (*encode_func_t)(
66 const uint32_t time,
67 const uint32_t dtime,
68 const uint32_t channel,
69 const uint32_t overflow,
70 const uint32_t marker
71 ) noexcept nogil
72
73 ctypedef void (*decode_func_t)(
74 const uint32_t record,
75 uint32_t* time,
76 uint32_t* dtime,
77 uint32_t* channel,
78 uint64_t* overflow,
79 uint8_t* marker,
80 uint8_t* special
81 ) noexcept nogil
82
83 ctypedef fused uint_t:
84 uint8_t
85 uint16_t
86 uint32_t
87 uint64_t
88
89
90 cdef int init_format(
91 const uint32_t format,
92 decode_func_t* decode,
93 ssize_t* bins, # number_bins_max
94 ssize_t* channels, # number_channels_max
95 ) noexcept nogil:
96 if format == 0x00010303:
97 # PicoHarpT3/PicoHarp300
98 decode[0] = decode_pt3
99 bins[0] = 4096
100 channels[0] = 4
101 elif format == 0x00010203:
102 # PicoHarpT2/PicoHarp300
103 decode[0] = decode_pt2
104 bins[0] = 0
105 channels[0] = 5 # ?
106 elif format in {
107 0x01010204, # HydraHarp2T2
108 0x00010205, # TimeHarp260NT2
109 0x00010206, # TimeHarp260PT2
110 0x00010207, # GenericT2 (MultiHarpT2 and Picoharp330T2)
111 }:
112 decode[0] = decode_ht2
113 bins[0] = 0
114 channels[0] = 64
115 elif format == 0x00010204:
116 # HydraHarpT2
117 decode[0] = decode_ht2v1
118 bins[0] = 0
119 channels[0] = 64
120 elif format in {
121 0x01010304, # HydraHarp2T3
122 0x00010305, # TimeHarp260NT3
123 0x00010306, # TimeHarp260PT3
124 0x00010307, # GenericT3 (MultiHarpT3 and Picoharp330T3)
125 }:
126 decode[0] = decode_ht3
127 bins[0] = 32768
128 channels[0] = 64
129 elif format == 0x00010304: # HydraHarpT3
130 decode[0] = decode_ht3v1
131 bins[0] = 32768
132 channels[0] = 64
133 else:
134 return 1
135 return 0
136
137
138 def decode_info(
139 const uint32_t[::1] records,
140 const uint32_t format,
141 const uint32_t line_start,
142 const uint32_t line_stop,
143 const uint32_t frame_change,
144 const ssize_t lines_in_frame, # may be zero to disable frame trimming
145 ):
146 """Return information about PicoQuant TTTR records."""
147 cdef:
148 ssize_t nrecords = records.size
149 ssize_t i, maxchannels, maxbins
150 ssize_t channels_active_first, channels_active_last
151 ssize_t y = 0
152 ssize_t skip_first_frame = 0
153 ssize_t skip_last_frame = 0
154 uint64_t nbins = 0
155 uint64_t nframes = 0
156 uint64_t nphotons = 0
157 uint64_t nmarkers = 0
158 uint64_t nlines = 0
159 uint64_t overflow = 0
160 uint64_t time_line_start = UINT64_MAX
161 uint64_t time_in_lines = 0
162 uint64_t channels_active = 0
163 uint32_t itime = 0
164 uint32_t idtime, ichannel
165 uint8_t ispecial, imarker
166
167 with nogil:
168 # dispatch on format so compiler can inline decoder and optimize loop
169 if format == 0x00010303:
170 # PicoHarpT3/PicoHarp300
171 maxbins = 4096
172 maxchannels = 4
173 for i in range(nrecords):
174 decode_pt3(
175 records[i],
176 &itime,
177 &idtime,
178 &ichannel,
179 &overflow,
180 &imarker,
181 &ispecial,
182 )
183 decode_info_record(
184 itime,
185 idtime,
186 ichannel,
187 overflow,
188 imarker,
189 ispecial,
190 line_start,
191 line_stop,
192 frame_change,
193 lines_in_frame,
194 maxchannels,
195 maxbins,
196 &nphotons,
197 &nbins,
198 &nmarkers,
199 &nlines,
200 &nframes,
201 &channels_active,
202 &time_in_lines,
203 &time_line_start,
204 &y,
205 &skip_first_frame,
206 &skip_last_frame,
207 )
208 elif format == 0x00010203:
209 # PicoHarpT2/PicoHarp300
210 maxbins = 0
211 maxchannels = 5 # ?
212 for i in range(nrecords):
213 decode_pt2(
214 records[i],
215 &itime,
216 &idtime,
217 &ichannel,
218 &overflow,
219 &imarker,
220 &ispecial,
221 )
222 decode_info_record(
223 itime,
224 idtime,
225 ichannel,
226 overflow,
227 imarker,
228 ispecial,
229 line_start,
230 line_stop,
231 frame_change,
232 lines_in_frame,
233 maxchannels,
234 maxbins,
235 &nphotons,
236 &nbins,
237 &nmarkers,
238 &nlines,
239 &nframes,
240 &channels_active,
241 &time_in_lines,
242 &time_line_start,
243 &y,
244 &skip_first_frame,
245 &skip_last_frame,
246 )
247 elif format in {
248 0x01010204, # HydraHarp2T2
249 0x00010205, # TimeHarp260NT2
250 0x00010206, # TimeHarp260PT2
251 0x00010207, # GenericT2 (MultiHarpT2 and Picoharp330T2)
252 }:
253 maxbins = 0
254 maxchannels = 64
255 for i in range(nrecords):
256 decode_ht2(
257 records[i],
258 &itime,
259 &idtime,
260 &ichannel,
261 &overflow,
262 &imarker,
263 &ispecial
264 )
265 decode_info_record(
266 itime,
267 idtime,
268 ichannel,
269 overflow,
270 imarker,
271 ispecial,
272 line_start,
273 line_stop,
274 frame_change,
275 lines_in_frame,
276 maxchannels,
277 maxbins,
278 &nphotons,
279 &nbins,
280 &nmarkers,
281 &nlines,
282 &nframes,
283 &channels_active,
284 &time_in_lines,
285 &time_line_start,
286 &y,
287 &skip_first_frame,
288 &skip_last_frame,
289 )
290 elif format == 0x00010204:
291 # HydraHarpT2
292 maxbins = 0
293 maxchannels = 64
294 for i in range(nrecords):
295 decode_ht2v1(
296 records[i],
297 &itime,
298 &idtime,
299 &ichannel,
300 &overflow,
301 &imarker,
302 &ispecial,
303 )
304 decode_info_record(
305 itime,
306 idtime,
307 ichannel,
308 overflow,
309 imarker,
310 ispecial,
311 line_start,
312 line_stop,
313 frame_change,
314 lines_in_frame,
315 maxchannels,
316 maxbins,
317 &nphotons,
318 &nbins,
319 &nmarkers,
320 &nlines,
321 &nframes,
322 &channels_active,
323 &time_in_lines,
324 &time_line_start,
325 &y,
326 &skip_first_frame,
327 &skip_last_frame,
328 )
329 elif format in {
330 0x01010304, # HydraHarp2T3
331 0x00010305, # TimeHarp260NT3
332 0x00010306, # TimeHarp260PT3
333 0x00010307, # GenericT3 (MultiHarpT3 and Picoharp330T3)
334 }:
335 maxbins = 32768
336 maxchannels = 64
337 for i in range(nrecords):
338 decode_ht3(
339 records[i],
340 &itime,
341 &idtime,
342 &ichannel,
343 &overflow,
344 &imarker,
345 &ispecial,
346 )
347 decode_info_record(
348 itime,
349 idtime,
350 ichannel,
351 overflow,
352 imarker,
353 ispecial,
354 line_start,
355 line_stop,
356 frame_change,
357 lines_in_frame,
358 maxchannels,
359 maxbins,
360 &nphotons,
361 &nbins,
362 &nmarkers,
363 &nlines,
364 &nframes,
365 &channels_active,
366 &time_in_lines,
367 &time_line_start,
368 &y,
369 &skip_first_frame,
370 &skip_last_frame,
371 )
372 elif format == 0x00010304:
373 # HydraHarpT3
374 maxbins = 32768
375 maxchannels = 64
376 for i in range(nrecords):
377 decode_ht3v1(
378 records[i],
379 &itime,
380 &idtime,
381 &ichannel,
382 &overflow,
383 &imarker,
384 &ispecial,
385 )
386 decode_info_record(
387 itime,
388 idtime,
389 ichannel,
390 overflow,
391 imarker,
392 ispecial,
393 line_start,
394 line_stop,
395 frame_change,
396 lines_in_frame,
397 maxchannels,
398 maxbins,
399 &nphotons,
400 &nbins,
401 &nmarkers,
402 &nlines,
403 &nframes,
404 &channels_active,
405 &time_in_lines,
406 &time_line_start,
407 &y,
408 &skip_first_frame,
409 &skip_last_frame,
410 )
411 else:
412 with gil:
413 msg = f'no decoder available for {format=:02x}'
414 raise ValueError(msg)
415
416 channels_active_first = 64
417 channels_active_last = 0
418 for i in range(64):
419 if channels_active & ((<uint64_t> 1) << i):
420 if i < channels_active_first:
421 channels_active_first = i
422 channels_active_last = i
423 if channels_active_first == 64:
424 channels_active_first = 0
425 channels_active_last = 0
426
427 nbins = 0 if maxbins == 0 else nbins + 1
428
429 if nframes > 1 and y > 0 and lines_in_frame > y + 1:
430 skip_last_frame = 1
431
432 if nframes == 0:
433 skip_first_frame = 0
434 skip_last_frame = 0
435 # elif nframes == 1:
436 # # leave incomplete single frame
437 # skip_first_frame = 0
438 # skip_last_frame = 0
439 # elif nframes == 2:
440 # if skip_first_frame and skip_last_frame:
441 # # leave both incomplete frames
442 # skip_first_frame = 0
443 # skip_last_frame = 0
444 # elif skip_first_frame or skip_last_frame:
445 # # remove incomplete first xor last frame
446 # nframes -= 1
447 else:
448 # remove incomplete first and/or last frames
449 if skip_first_frame:
450 nframes -= 1
451 if skip_last_frame and nframes > 0:
452 nframes -= 1
453
454 if nlines > 0:
455 time_in_lines = <ssize_t> (
456 round(<double> time_in_lines / <double> nlines)
457 )
458 else:
459 time_in_lines = 0
460
461 return (
462 format,
463 nrecords,
464 nphotons,
465 nmarkers,
466 nframes,
467 nlines,
468 maxchannels,
469 channels_active,
470 channels_active_first,
471 channels_active_last,
472 maxbins,
473 nbins,
474 skip_first_frame,
475 skip_last_frame,
476 time_in_lines,
477 overflow + itime
478 )
479
480
481 cdef inline void decode_info_record(
482 const uint32_t itime,
483 const uint32_t idtime,
484 const uint32_t ichannel,
485 const uint64_t overflow,
486 const uint8_t imarker,
487 const uint8_t ispecial,
488 const uint32_t line_start,
489 const uint32_t line_stop,
490 const uint32_t frame_change,
491 const ssize_t lines_in_frame,
492 const ssize_t maxchannels,
493 const ssize_t maxbins,
494 uint64_t* nphotons,
495 uint64_t* nbins,
496 uint64_t* nmarkers,
497 uint64_t* nlines,
498 uint64_t* nframes,
499 uint64_t* channels_active,
500 uint64_t* time_in_lines,
501 uint64_t* time_line_start,
502 ssize_t* y,
503 ssize_t* skip_first_frame,
504 ssize_t* skip_last_frame,
505 ) noexcept nogil:
506 """Accumulate one decoded TTTR record into decode_info statistics."""
507 if ispecial == 0:
508 # photon record
509 nphotons[0] += 1
510 if ichannel < <uint32_t> maxchannels:
511 channels_active[0] |= (<uint64_t> 1) << ichannel
512 if idtime > nbins[0] and idtime < <uint32_t> maxbins:
513 nbins[0] = idtime
514 elif ispecial == 2:
515 # marker record
516 nmarkers[0] += 1
517 if imarker & frame_change:
518 # frame marker
519 if lines_in_frame > y[0] + 1:
520 # not enough lines in frame
521 if nframes[0] == 1:
522 skip_first_frame[0] = 1
523 else:
524 skip_last_frame[0] = 1
525 else:
526 skip_last_frame[0] = 0
527 if not imarker & line_stop:
528 time_line_start[0] = UINT64_MAX
529 y[0] = 0
530 if imarker & line_stop:
531 # line stop marker
532 if (
533 time_line_start[0] != UINT64_MAX
534 and (lines_in_frame == 0 or lines_in_frame >= y[0])
535 ):
536 time_in_lines[0] += (overflow + itime) - time_line_start[0]
537 time_line_start[0] = UINT64_MAX
538 if imarker & line_start:
539 # line start marker
540 # TODO: add to time_in_lines if previous line did not stop?
541 time_line_start[0] = overflow + itime
542 if y[0] == 0:
543 # new frame starts at first line
544 # after start or frame change marker
545 nframes[0] += 1
546 y[0] += 1
547 if lines_in_frame == 0 or lines_in_frame >= y[0]:
548 nlines[0] += 1
549
550
551 def decode_t3_point(
552 uint_t[:, :, ::1] histogram,
553 uint64_t[::1] times,
554 const uint32_t[::1] records,
555 const uint32_t format,
556 const uint64_t pixel_time,
557 const ssize_t startt = 0,
558 const ssize_t startc = 0,
559 const ssize_t starth = 0,
560 const ssize_t bint = 1,
561 const ssize_t binc = 1,
562 const ssize_t binh = 1,
563 ):
564 """Return TCSPC histogram from TTTR T3 records of point measurement."""
565 cdef:
566 ssize_t sizec, sizet, sizeh, stopc, stopt, stoph
567 ssize_t nrecords = records.size
568 uint64_t overflow, time_global
569 uint32_t itime, idtime, ichannel
570 uint8_t ispecial, imarker
571 ssize_t i, iframe, iframe_binned, maxbins_
572 decode_func_t decode_func
573
574 if pixel_time == 0:
575 raise ValueError(f'invalid {pixel_time=}')
576
577 if init_format(format, &decode_func, &maxbins_, &i) != 0:
578 raise ValueError(f'no decoder available for {format=:02x}')
579 if maxbins_ == 0:
580 raise NotImplementedError(f'not a T3 {format=:02x}')
581
582 if startc < 0 or startt < 0 or starth < 0:
583 raise ValueError(f'invalid {startt=}, {startc=}, or {starth=}')
584 if binc < 1 or bint < 1 or binh < 1:
585 raise ValueError(f'invalid {bint=}, {binc=}, or {binh=}')
586 if times.size != histogram.shape[0]:
587 raise ValueError(f'{times.size=} does not match {histogram.shape=}')
588
589 sizet, sizec, sizeh = histogram.shape[:3]
590
591 with nogil:
592 stopt = startt + sizet * bint
593 stopc = startc + sizec * binc
594 stoph = starth + sizeh * binh
595
596 overflow = 0
597 iframe = 0
598 iframe_binned = -1
599
600 for i in range(nrecords):
601 decode_func(
602 records[i],
603 &itime,
604 &idtime,
605 &ichannel,
606 &overflow,
607 &imarker,
608 &ispecial
609 )
610 if ispecial == 0:
611 # regular record
612 if (
613 ichannel < startc
614 or ichannel >= stopc
615 or idtime < starth
616 or idtime >= stoph
617 ):
618 continue
619 time_global = overflow + itime
620 iframe = time_global // pixel_time
621 if iframe < startt:
622 continue
623 if iframe >= stopt:
624 break
625 if iframe_binned != (iframe - startt) // bint:
626 iframe_binned = (iframe - startt) // bint
627 times[iframe_binned] = time_global
628
629 histogram[
630 iframe_binned,
631 (ichannel - startc) // binc,
632 (idtime - starth) // binh,
633 ] += 1
634 # elif ispecial == 1:
635 # # overflow
636 # elif ispecial == 2:
637 # # no markers
638
639
640 def decode_t3_line(
641 uint_t[:, :, :, ::1] histogram,
642 uint64_t[::1] times,
643 const uint32_t[::1] records,
644 const uint32_t format,
645 const uint64_t pixel_time,
646 const uint32_t line_start,
647 const uint32_t line_stop,
648 const ssize_t startt = 0,
649 const ssize_t startx = 0,
650 const ssize_t startc = 0,
651 const ssize_t starth = 0,
652 const ssize_t bint = 1,
653 const ssize_t binx = 1,
654 const ssize_t binc = 1,
655 const ssize_t binh = 1,
656 ):
657 """Return TCSPC histogram from TTTR T3 records of line scan measurement."""
658 cdef:
659 ssize_t sizec, sizet, sizex, sizeh
660 ssize_t stopc, stopt, stopx, stoph
661 ssize_t nrecords = records.size
662 uint64_t overflow, time_global, time_line_start
663 uint32_t itime, idtime, ichannel
664 uint8_t ispecial, imarker
665 ssize_t i, ix, iframe, iframe_binned, maxbins_
666 decode_func_t decode_func
667
668 if pixel_time == 0:
669 raise ValueError(f'invalid {pixel_time=}')
670
671 if init_format(format, &decode_func, &maxbins_, &i) != 0:
672 raise ValueError(f'no decoder available for {format=:02x}')
673 if maxbins_ == 0:
674 raise NotImplementedError(f'not a T3 {format=:02x}')
675
676 if startc < 0 or startt < 0 or startx < 0 or starth < 0:
677 raise ValueError(
678 f'invalid {startt=}, {startx=}, {startc=}, or {starth=}'
679 )
680 if binc < 1 or bint < 1 or binx < 1 or binh < 1:
681 raise ValueError(f'invalid {bint=}, {binx=}, {binc=}, or {binh=}')
682 if times.size != histogram.shape[0]:
683 raise ValueError(f'{times.size=} does not match {histogram.shape=}')
684
685 sizet, sizex, sizec, sizeh = histogram.shape[:4]
686
687 with nogil:
688 stopt = startt + sizet * bint
689 stopx = startx + sizex * binx
690 stopc = startc + sizec * binc
691 stoph = starth + sizeh * binh
692
693 time_line_start = UINT64_MAX
694 overflow = 0
695 iframe = 0
696 iframe_binned = -1
697 ix = 0
698
699 for i in range(nrecords):
700 decode_func(
701 records[i],
702 &itime,
703 &idtime,
704 &ichannel,
705 &overflow,
706 &imarker,
707 &ispecial
708 )
709 time_global = overflow + itime
710 if ispecial == 0:
711 # regular record
712 if (
713 time_line_start == UINT64_MAX # no line start marker yet
714 or ichannel < startc
715 or ichannel >= stopc
716 or idtime < starth
717 or idtime >= stoph
718 ):
719 continue
720
721 ix = (time_global - time_line_start) // pixel_time
722 if ix < startx or ix >= stopx:
723 continue
724 histogram[
725 iframe_binned,
726 (ix - startx) // binx,
727 (ichannel - startc) // binc,
728 (idtime - starth) // binh,
729 ] += 1
730
731 # elif ispecial == 1:
732 # # overflow
733 elif ispecial == 2:
734 # marker
735 if imarker & line_stop:
736 time_line_start = UINT64_MAX
737 iframe += 1
738 if iframe == stopt:
739 break
740 if imarker & line_start and iframe >= startt:
741 time_line_start = time_global
742 if iframe_binned != (iframe - startt) // bint:
743 iframe_binned = (iframe - startt) // bint
744 times[iframe_binned] = time_line_start
745
746
747 def decode_t3_image(
748 uint_t[:, :, :, :, ::1] histogram,
749 uint64_t[::1] times,
750 const uint32_t[::1] records,
751 const uint32_t format,
752 const ssize_t pixels_in_line,
753 ssize_t pixel_time, # average global time spent in pixel
754 ssize_t line_time, # global time spent in one line
755 const uint16_t[::1] pixel_at_time, # global time in line to pixel index
756 const uint32_t line_start, # mask
757 const uint32_t line_stop, # mask
758 const uint32_t frame_change, # mask
759 const ssize_t startt = 0,
760 const ssize_t starty = 0,
761 const ssize_t startx = 0,
762 const ssize_t startc = 0,
763 const ssize_t starth = 0,
764 const ssize_t bint = 1,
765 const ssize_t biny = 1,
766 const ssize_t binx = 1,
767 const ssize_t binc = 1,
768 const ssize_t binh = 1,
769 const ssize_t bishift = 0,
770 const bint bidirectional = False,
771 const bint sinusoidal = False,
772 const bint skip_first_frame = 0,
773 ):
774 """Return TCSPC histogram from TTTR T3 records of image measurement."""
775 cdef:
776 ssize_t sizec, sizet, sizey, sizex, sizeh
777 ssize_t stopc, stopt, stopy, stopx, stoph
778 ssize_t nrecords = records.size
779 ssize_t i, j, ix, iy, iy_binned, iframe, iframe_binned, maxbins_, bidiv
780 uint64_t overflow, overflowj, time_global, time_line_start
781 uint32_t itime, idtime, ichannel
782 uint8_t ispecial, imarker
783 decode_func_t decode_func
784 bint scanline = pixel_time <= 0 or line_time <= 0
785
786 if scanline and pixels_in_line <= 0:
787 raise ValueError(f'invalid {pixels_in_line=}')
788
789 if (
790 sinusoidal
791 and (pixel_at_time is None or pixel_at_time.size != line_time)
792 ):
793 sizey = 0 if pixel_at_time is None else pixel_at_time.size
794 raise ValueError(
795 f'invalid pixel_at_time.size={sizey} != {line_time=}'
796 )
797
798 if init_format(format, &decode_func, &maxbins_, &i) != 0:
799 raise ValueError(f'no decoder available for {format=:02x}')
800 if maxbins_ == 0:
801 raise NotImplementedError(f'not a T3 {format=:02x}')
802
803 if startc < 0 or startt < 0 or starty < 0 or startx < 0 or starth < 0:
804 raise ValueError(
805 f'invalid {startt=}, {starty=}, {startx=}, {startc=}, or {starth=}'
806 )
807 if binc < 1 or bint < 1 or biny < 1 or binx < 1 or binh < 1:
808 raise ValueError(
809 f'invalid {bint=}, {biny=}, {binx=}, {binc=}, or {binh=}'
810 )
811 if times.size != histogram.shape[0]:
812 raise ValueError(f'{times.size=} does not match {histogram.shape=}')
813 # if wraparound and starth > 0:
814 # raise ValueError(f'can not wrap dtime with {starth=}')
815
816 sizet, sizey, sizex, sizec, sizeh = histogram.shape[:5]
817
818 with nogil:
819 stopt = startt + sizet * bint
820 stopy = starty + sizey * biny
821 stopx = startx + sizex * binx
822 stopc = startc + sizec * binc
823 stoph = starth + sizeh * binh
824 bidiv = starty % 2
825
826 time_line_start = UINT64_MAX
827 overflow = 0
828 iframe = -2 if skip_first_frame else -1
829 iframe_binned = -1
830 iy_binned = -1
831 iy = -1
832 ix = 0
833
834 # TODO: process channels/frames in parallel?
835 for i in range(nrecords):
836 ispecial = 3 # invalid, should always be overridden
837
838 decode_func(
839 records[i],
840 &itime,
841 &idtime,
842 &ichannel,
843 &overflow,
844 &imarker,
845 &ispecial
846 )
847 time_global = overflow + itime
848
849 if ispecial == 0:
850 # regular record
851 if (
852 time_line_start == UINT64_MAX # no line start marker yet
853 or ichannel < startc
854 or ichannel >= stopc
855 or idtime < starth
856 or idtime >= stoph
857 # or (not wraparound and idtime >= stoph)
858 ):
859 continue
860
861 ix = <ssize_t> (time_global - time_line_start)
862
863 if bidirectional and iy_binned % 2 != bidiv:
864 # line backward scan
865 # TODO: support bidirectional per frame
866 # TODO: is bishift correct for sinusoidal scanning?
867 ix = line_time - 1 - ix + bishift
868
869 if ix < 0 or ix >= line_time:
870 ix = stopx
871 elif sinusoidal:
872 ix = <ssize_t> pixel_at_time[ix]
873 else:
874 ix = ix // pixel_time
875
876 if ix >= startx and ix < stopx:
877 # idtime_binned = (idtime - starth) // binh
878 # if wraparound and idtime_binned >= sizeh:
879 # idtime_binned %= sizeh
880 histogram[
881 iframe_binned,
882 iy_binned,
883 (ix - startx) // binx,
884 (ichannel - startc) // binc,
885 (idtime - starth) // binh,
886 ] += 1
887
888 # elif ispecial == 1:
889 # # overflow
890 elif ispecial == 2:
891 # marker
892 if imarker & frame_change:
893 time_line_start = UINT64_MAX
894 iy = -1
895 if imarker & line_stop:
896 time_line_start = UINT64_MAX
897 if imarker & line_start:
898 iy += 1
899 if iy == 0:
900 # new frame starts at first line
901 # after start or frame change marker
902 iframe += 1
903 if iframe == stopt:
904 break
905 if iframe >= startt and iy >= starty and iy < stopy:
906 iy_binned = (iy - starty) // biny
907 time_line_start = time_global
908 if iframe_binned != (iframe - startt) // bint:
909 iframe_binned = (iframe - startt) // bint
910 times[iframe_binned] = time_line_start
911 if scanline:
912 # scan line to next marker to determine
913 # line_time and pixel_time
914 overflowj = overflow
915 j = i + 1
916 while True:
917 if j >= nrecords:
918 break
919 decode_func(
920 records[j],
921 &itime,
922 &idtime,
923 &ichannel,
924 &overflowj,
925 &imarker,
926 &ispecial
927 )
928 time_global = overflowj + itime
929 if ispecial == 2:
930 # any marker
931 break
932 j += 1
933 line_time = <ssize_t> (
934 time_global - time_line_start
935 )
936 # pixel_time = <ssize_t> round(
937 # <double> line_time / <double> pixels_in_line
938 # )
939 pixel_time = line_time // pixels_in_line
940 pixel_time = pixel_time if pixel_time > 0 else 1
941 else:
942 # line is not part of selection
943 time_line_start = UINT64_MAX
944
945 elif ispecial != 1:
946 with gil:
947 raise ValueError(f'invalid records[{i}]={records[i]}')
948
949
950 def decode_t3_histogram(
951 uint_t[:, ::1] histogram,
952 const uint32_t[::1] records,
953 const uint32_t format,
954 const ssize_t startc,
955 ):
956 """Decode PicoQuant T3 TTTR records to histogram per channel."""
957 cdef:
958 ssize_t nrecords = records.size
959 ssize_t i, nbins, nchannels
960 uint64_t overflow
961 uint32_t itime, idtime, ichannel
962 uint8_t ispecial, imarker
963
964 if startc < 0:
965 raise ValueError(f'{startc=} < 0')
966
967 nchannels, nbins = histogram.shape[:2]
968
969 with nogil:
970 overflow = 0
971 # dispatch on format so compiler can inline decoder and optimize loop
972 if format == 0x00010303:
973 # PicoHarpT3
974 for i in range(nrecords):
975 decode_pt3(
976 records[i],
977 &itime,
978 &idtime,
979 &ichannel,
980 &overflow,
981 &imarker,
982 &ispecial
983 )
984 ichannel -= <uint32_t> startc # may underflow
985 if ispecial == 0 and ichannel < nchannels and idtime < nbins:
986 histogram[ichannel, idtime] += 1
987 elif format in {
988 0x01010304, # HydraHarp2T3
989 0x00010305, # TimeHarp260NT3
990 0x00010306, # TimeHarp260PT3
991 0x00010307, # GenericT3 (MultiHarpT3 and Picoharp330T3)
992 }:
993 for i in range(nrecords):
994 decode_ht3(
995 records[i],
996 &itime,
997 &idtime,
998 &ichannel,
999 &overflow,
1000 &imarker,
1001 &ispecial
1002 )
1003 ichannel -= <uint32_t> startc # may underflow
1004 if ispecial == 0 and ichannel < nchannels and idtime < nbins:
1005 histogram[ichannel, idtime] += 1
1006 elif format == 0x00010304:
1007 # HydraHarpT3
1008 for i in range(nrecords):
1009 decode_ht3v1(
1010 records[i],
1011 &itime,
1012 &idtime,
1013 &ichannel,
1014 &overflow,
1015 &imarker,
1016 &ispecial
1017 )
1018 ichannel -= <uint32_t> startc # may underflow
1019 if ispecial == 0 and ichannel < nchannels and idtime < nbins:
1020 histogram[ichannel, idtime] += 1
1021 # if wraparound:
1022 # histogram[ichannel, idtime % nbins] += 1
1023 else:
1024 with gil:
1025 raise ValueError(f'no decoder available for {format=:02x}')
1026
1027
1028 def decode_t2_histogram(
1029 uint_t[:, ::1] histogram,
1030 const uint32_t[::1] records,
1031 const uint32_t format,
1032 const uint64_t bin_time,
1033 const ssize_t startc,
1034 ):
1035 """Decode PicoQuant T2 TTTR records to histogram per channel."""
1036 cdef:
1037 ssize_t nrecords = records.size
1038 ssize_t i, ibin, nbins, nchannels
1039 uint64_t overflow
1040 uint32_t itime, idtime, ichannel
1041 uint8_t ispecial, imarker
1042
1043 if bin_time == 0:
1044 raise ValueError(f'invalid {bin_time=}')
1045
1046 if startc < 0:
1047 raise ValueError(f'{startc=} < 0')
1048
1049 nchannels, nbins = histogram.shape[:2]
1050
1051 with nogil:
1052 overflow = 0
1053 # dispatch on format so compiler can inline decoder and optimize loop
1054 if format == 0x00010203:
1055 # PicoHarpT2
1056 for i in range(nrecords):
1057 decode_pt2(
1058 records[i],
1059 &itime,
1060 &idtime,
1061 &ichannel,
1062 &overflow,
1063 &imarker,
1064 &ispecial
1065 )
1066 ibin = <ssize_t> ((overflow + itime) // bin_time)
1067 if ibin >= nbins:
1068 break
1069 ichannel -= <uint32_t> startc # may underflow
1070 if ispecial == 0 and ichannel < nchannels:
1071 histogram[ichannel, ibin] += 1
1072 elif format in {
1073 0x01010204, # HydraHarp2T2
1074 0x00010205, # TimeHarp260NT2
1075 0x00010206, # TimeHarp260PT2
1076 0x00010207, # GenericT2 (MultiHarpT2 and Picoharp330T2)
1077 }:
1078 for i in range(nrecords):
1079 decode_ht2(
1080 records[i],
1081 &itime,
1082 &idtime,
1083 &ichannel,
1084 &overflow,
1085 &imarker,
1086 &ispecial
1087 )
1088 ibin = <ssize_t> ((overflow + itime) // bin_time)
1089 if ibin >= nbins:
1090 break
1091 ichannel -= <uint32_t> startc # may underflow
1092 if ispecial == 0 and ichannel < nchannels:
1093 histogram[ichannel, ibin] += 1
1094 elif format == 0x00010204:
1095 # HydraHarpT2
1096 for i in range(nrecords):
1097 decode_ht2v1(
1098 records[i],
1099 &itime,
1100 &idtime,
1101 &ichannel,
1102 &overflow,
1103 &imarker,
1104 &ispecial
1105 )
1106 ibin = <ssize_t> ((overflow + itime) // bin_time)
1107 if ibin >= nbins:
1108 break
1109 ichannel -= <uint32_t> startc # may underflow
1110 if ispecial == 0 and ichannel < nchannels:
1111 histogram[ichannel, ibin] += 1
1112 else:
1113 with gil:
1114 raise ValueError(f'no decoder available for {format=:02x}')
1115
1116
1117 def decode_t3_records(
1118 t3_t[::1] decoded,
1119 const uint32_t[::1] records,
1120 const uint32_t format
1121 ):
1122 """Decode PicoQuant T3 TTTR records."""
1123 cdef:
1124 ssize_t nrecords = min(records.size, decoded.size)
1125 ssize_t i
1126 uint64_t overflow
1127 uint32_t itime, idtime, ichannel
1128 uint8_t ispecial, imarker
1129
1130 with nogil:
1131 overflow = 0
1132 # dispatch on format so compiler can inline decoder and optimize loop
1133 if format == 0x00010303:
1134 # PicoHarpT3
1135 for i in range(nrecords):
1136 decode_pt3(
1137 records[i],
1138 &itime,
1139 &idtime,
1140 &ichannel,
1141 &overflow,
1142 &imarker,
1143 &ispecial
1144 )
1145 if ispecial == 0:
1146 decoded[i].time = overflow + itime
1147 decoded[i].dtime = idtime
1148 decoded[i].channel = ichannel
1149 decoded[i].marker = 0
1150 elif ispecial == 1:
1151 decoded[i].time = overflow + itime
1152 decoded[i].dtime = -1
1153 decoded[i].channel = -1
1154 decoded[i].marker = 0
1155 elif ispecial == 2:
1156 decoded[i].time = overflow + itime
1157 decoded[i].dtime = -1
1158 decoded[i].channel = -1
1159 decoded[i].marker = imarker
1160 elif format in {
1161 0x01010304, # HydraHarp2T3
1162 0x00010305, # TimeHarp260NT3
1163 0x00010306, # TimeHarp260PT3
1164 0x00010307, # GenericT3 (MultiHarpT3 and Picoharp330T3)
1165 }:
1166 for i in range(nrecords):
1167 decode_ht3(
1168 records[i],
1169 &itime,
1170 &idtime,
1171 &ichannel,
1172 &overflow,
1173 &imarker,
1174 &ispecial
1175 )
1176 if ispecial == 0:
1177 decoded[i].time = overflow + itime
1178 decoded[i].dtime = idtime
1179 decoded[i].channel = ichannel
1180 decoded[i].marker = 0
1181 elif ispecial == 1:
1182 decoded[i].time = overflow + itime
1183 decoded[i].dtime = -1
1184 decoded[i].channel = -1
1185 decoded[i].marker = 0
1186 elif ispecial == 2:
1187 decoded[i].time = overflow + itime
1188 decoded[i].dtime = -1
1189 decoded[i].channel = -1
1190 decoded[i].marker = imarker
1191 elif format == 0x00010304:
1192 # HydraHarpT3
1193 for i in range(nrecords):
1194 decode_ht3v1(
1195 records[i],
1196 &itime,
1197 &idtime,
1198 &ichannel,
1199 &overflow,
1200 &imarker,
1201 &ispecial
1202 )
1203 if ispecial == 0:
1204 decoded[i].time = overflow + itime
1205 decoded[i].dtime = idtime
1206 decoded[i].channel = ichannel
1207 decoded[i].marker = 0
1208 elif ispecial == 1:
1209 decoded[i].time = overflow + itime
1210 decoded[i].dtime = -1
1211 decoded[i].channel = -1
1212 decoded[i].marker = 0
1213 elif ispecial == 2:
1214 decoded[i].time = overflow + itime
1215 decoded[i].dtime = -1
1216 decoded[i].channel = -1
1217 decoded[i].marker = imarker
1218 else:
1219 with gil:
1220 raise ValueError(f'no decoder available for {format=:02x}')
1221
1222
1223 def decode_t2_records(
1224 t2_t[::1] decoded,
1225 const uint32_t[::1] records,
1226 const uint32_t format
1227 ):
1228 """Decode PicoQuant T2 TTTR records."""
1229 cdef:
1230 ssize_t nrecords = min(records.size, decoded.size)
1231 ssize_t i
1232 uint64_t overflow
1233 uint32_t itime, idtime, ichannel
1234 uint8_t ispecial, imarker
1235
1236 with nogil:
1237 overflow = 0
1238 # dispatch on format so compiler can inline decoder and optimize loop
1239 if format == 0x00010203:
1240 # PicoHarpT2
1241 for i in range(nrecords):
1242 decode_pt2(
1243 records[i],
1244 &itime,
1245 &idtime,
1246 &ichannel,
1247 &overflow,
1248 &imarker,
1249 &ispecial
1250 )
1251 if ispecial == 0:
1252 decoded[i].time = overflow + itime
1253 decoded[i].channel = ichannel
1254 decoded[i].marker = 0
1255 elif ispecial == 1:
1256 decoded[i].time = overflow + itime
1257 decoded[i].channel = -1
1258 decoded[i].marker = 0
1259 elif ispecial == 2:
1260 decoded[i].time = overflow + itime
1261 decoded[i].channel = -1
1262 decoded[i].marker = imarker
1263 elif format in {
1264 0x01010204, # HydraHarp2T2
1265 0x00010205, # TimeHarp260NT2
1266 0x00010206, # TimeHarp260PT2
1267 0x00010207, # GenericT2 (MultiHarpT2 and Picoharp330T2)
1268 }:
1269 for i in range(nrecords):
1270 decode_ht2(
1271 records[i],
1272 &itime,
1273 &idtime,
1274 &ichannel,
1275 &overflow,
1276 &imarker,
1277 &ispecial
1278 )
1279 if ispecial == 0:
1280 decoded[i].time = overflow + itime
1281 decoded[i].channel = ichannel
1282 decoded[i].marker = 0
1283 elif ispecial == 1:
1284 decoded[i].time = overflow + itime
1285 decoded[i].channel = -1
1286 decoded[i].marker = 0
1287 elif ispecial == 2:
1288 decoded[i].time = overflow + itime
1289 decoded[i].channel = -1
1290 decoded[i].marker = imarker
1291 elif format == 0x00010204:
1292 # HydraHarpT2
1293 for i in range(nrecords):
1294 decode_ht2v1(
1295 records[i],
1296 &itime,
1297 &idtime,
1298 &ichannel,
1299 &overflow,
1300 &imarker,
1301 &ispecial
1302 )
1303 if ispecial == 0:
1304 decoded[i].time = overflow + itime
1305 decoded[i].channel = ichannel
1306 decoded[i].marker = 0
1307 elif ispecial == 1:
1308 decoded[i].time = overflow + itime
1309 decoded[i].channel = -1
1310 decoded[i].marker = 0
1311 elif ispecial == 2:
1312 decoded[i].time = overflow + itime
1313 decoded[i].channel = -1
1314 decoded[i].marker = imarker
1315 else:
1316 with gil:
1317 raise ValueError(f'no decoder available for {format=:02x}')
1318
1319
1320 # Decode functions only set outputs relevant to each record type.
1321 # Callers must check special before using channel, dtime, or marker,
1322 # which may be stale from the previous call.
1323
1324 cdef inline void decode_pt3(
1325 const uint32_t record,
1326 uint32_t* time, # nsync
1327 uint32_t* dtime,
1328 uint32_t* channel,
1329 uint64_t* overflow,
1330 uint8_t* marker,
1331 uint8_t* special
1332 ) noexcept nogil:
1333 cdef uint32_t tmp
1334
1335 time[0] = record & 0xffff # 16 bit nsync
1336 dtime[0] = (record >> 16) & 0xfff # 12 bit dtime
1337 tmp = (record >> 28) & 0xf # 4 bit channel
1338 if tmp != 0xf:
1339 # regular record
1340 # TODO: understand this undocumented channel decoding
1341 if tmp > 0 and tmp < 5:
1342 channel[0] = tmp - 1 # one to zero-based
1343 else:
1344 # should not happen
1345 channel[0] = 4
1346 special[0] = 0
1347 elif dtime[0] == 0:
1348 # overflow
1349 special[0] = 1
1350 overflow[0] += 65536
1351 else:
1352 # marker
1353 special[0] = 2
1354 marker[0] = dtime[0]
1355 dtime[0] = 0
1356
1357
1358 cdef inline void decode_pt2(
1359 const uint32_t record,
1360 uint32_t* time, # timetag
1361 uint32_t* dtime, # not used
1362 uint32_t* channel,
1363 uint64_t* overflow,
1364 uint8_t* marker,
1365 uint8_t* special
1366 ) noexcept nogil:
1367 cdef uint32_t tmp
1368
1369 dtime[0] = 0 # not present in T2 record
1370 time[0] = record & 0xfffffff # 28 bit timetag
1371 tmp = ((record >> 28) & 0xf) # 4 bit channel
1372 if tmp != 0xf:
1373 # regular record
1374 if tmp < 5:
1375 channel[0] = tmp
1376 else:
1377 # should not happen
1378 channel[0] = 5
1379 special[0] = 0
1380 else:
1381 tmp = record & 0xf # lower 4 bits
1382 if tmp == 0:
1383 # overflow
1384 special[0] = 1
1385 overflow[0] += 210698240
1386 else:
1387 # marker
1388 special[0] = 2
1389 marker[0] = tmp
1390
1391
1392 cdef inline void decode_ht3(
1393 const uint32_t record,
1394 uint32_t* time, # nsync
1395 uint32_t* dtime,
1396 uint32_t* channel,
1397 uint64_t* overflow,
1398 uint8_t* marker,
1399 uint8_t* special
1400 ) noexcept nogil:
1401 cdef uint32_t tmp
1402
1403 time[0] = record & 0x3ff # 10 bit nsync
1404 dtime[0] = (record >> 10) & 0x7fff # 15 bit dtime
1405 tmp = (record >> 31) & 0x1 # 1 bit special
1406 if tmp == 0:
1407 # regular record
1408 special[0] = 0
1409 channel[0] = (record >> 25) & 0x3f # 6 bit channel
1410 else:
1411 tmp = (record >> 25) & 0x3f # 6 bit
1412 if tmp == 0x3f:
1413 # overflow
1414 special[0] = 1
1415 if time[0] <= 1:
1416 overflow[0] += 1024
1417 else:
1418 overflow[0] += time[0] * 1024
1419 elif 0 < tmp < 16:
1420 # marker
1421 special[0] = 2
1422 marker[0] = tmp
1423 else:
1424 # reserved; treat as overflow/skip
1425 special[0] = 1
1426
1427
1428 cdef inline void decode_ht3v1(
1429 const uint32_t record,
1430 uint32_t* time, # nsync
1431 uint32_t* dtime,
1432 uint32_t* channel,
1433 uint64_t* overflow,
1434 uint8_t* marker,
1435 uint8_t* special
1436 ) noexcept nogil:
1437 cdef uint32_t tmp
1438
1439 time[0] = record & 0x3ff # 10 bit nsync
1440 dtime[0] = (record >> 10) & 0x7fff # 15 bit dtime
1441 tmp = (record >> 31) & 0x1 # 1 bit special
1442 if tmp == 0:
1443 # regular record
1444 special[0] = 0
1445 channel[0] = (record >> 25) & 0x3f # 6 bit channel
1446 else:
1447 tmp = (record >> 25) & 0x3f # 6 bit
1448 if tmp == 0x3f:
1449 # overflow
1450 special[0] = 1
1451 overflow[0] += 1024
1452 elif 0 < tmp < 16:
1453 # marker
1454 special[0] = 2
1455 marker[0] = tmp
1456 else:
1457 # reserved; treat as overflow/skip
1458 special[0] = 1
1459
1460
1461 cdef inline void decode_ht2(
1462 const uint32_t record,
1463 uint32_t* time, # timetag
1464 uint32_t* dtime, # not used
1465 uint32_t* channel,
1466 uint64_t* overflow,
1467 uint8_t* marker,
1468 uint8_t* special
1469 ) noexcept nogil:
1470 cdef uint32_t tmp
1471
1472 dtime[0] = 0 # not present in T2 record
1473 time[0] = record & 0x1ffffff # 25 bit timetag
1474 tmp = (record >> 31) & 0x1 # 1 bit special
1475 if tmp == 0:
1476 # regular record
1477 special[0] = 0
1478 channel[0] = (record >> 25) & 0x3f # 6 bit channel
1479 else:
1480 tmp = (record >> 25) & 0x3f # 6 bit
1481 if tmp == 0x3f:
1482 # overflow
1483 special[0] = 1
1484 if time[0] <= 1:
1485 overflow[0] += 33554432
1486 else:
1487 overflow[0] += time[0] * 33554432
1488 elif tmp == 0:
1489 # regular record
1490 special[0] = 0
1491 channel[0] = 0
1492 elif tmp < 16:
1493 # marker
1494 special[0] = 2
1495 marker[0] = tmp
1496 else:
1497 # reserved; treat as overflow/skip
1498 special[0] = 1
1499
1500
1501 cdef inline void decode_ht2v1(
1502 const uint32_t record,
1503 uint32_t* time, # timetag
1504 uint32_t* dtime, # not used
1505 uint32_t* channel,
1506 uint64_t* overflow,
1507 uint8_t* marker,
1508 uint8_t* special
1509 ) noexcept nogil:
1510 cdef uint32_t tmp
1511
1512 dtime[0] = 0 # not present in T2 record
1513 time[0] = record & 0x1ffffff # 25 bit timetag
1514 tmp = (record >> 31) & 0x1 # 1 bit special
1515 if tmp == 0:
1516 # regular record
1517 special[0] = 0
1518 channel[0] = (record >> 25) & 0x3f # 6 bit channel
1519 else:
1520 tmp = (record >> 25) & 0x3f # 6 bit
1521 if tmp == 0x3f:
1522 # overflow
1523 special[0] = 1
1524 overflow[0] += 33552000
1525 elif tmp == 0:
1526 # regular record
1527 special[0] = 0
1528 channel[0] = 0
1529 elif tmp < 16:
1530 # marker
1531 special[0] = 2
1532 marker[0] = tmp
1533 else:
1534 # reserved; treat as overflow/skip
1535 special[0] = 1
1536
1537
1538 def encode_t3_image(
1539 uint32_t[::1] records,
1540 const uint_t[:, :, :, :, ::1] histogram,
1541 const uint32_t format,
1542 const uint32_t pixel_time, # average global time spent in pixel
1543 const uint32_t line_start, # mask
1544 const uint32_t line_stop, # mask
1545 const uint32_t frame_change, # mask
1546 ):
1547 """Return GenericT3 records from TCSPC image histogram."""
1548 # TODO: frame and line markers may be combined in one record
1549 # TODO: record photons across channels at same time (slower)
1550 # TODO: randomize photon arrival times at fixed count rate
1551
1552 cdef:
1553 ssize_t sizet, sizey, sizex, sizec, sizeh
1554 ssize_t t, y, x, c, h, nrecords, maxrecords
1555 uint32_t time, time_in_pixel, overflow, maxtime, maxoverflow, _i
1556 uint_t _count
1557 encode_func_t encode
1558
1559 sizet, sizey, sizex, sizec, sizeh = histogram.shape[:5]
1560 maxrecords = records.size
1561
1562 if format == 0x00010303:
1563 # PicoHarpT3/PicoHarp300
1564 encode = encode_pt3
1565 maxtime = 65536
1566 maxoverflow = 1
1567 elif format == 0x00010307:
1568 # GenericT3 (MultiHarpT3 and Picoharp330T3)
1569 encode = encode_ht3
1570 maxtime = 1024
1571 maxoverflow = 1023
1572 else:
1573 raise ValueError(f'{format=} not supported')
1574
1575 with nogil:
1576 time = 0
1577 nrecords = 0
1578
1579 for t in range(sizet):
1580 # frame
1581 for y in range(sizey):
1582 # line
1583
1584 if nrecords >= maxrecords:
1585 nrecords = -1
1586 break
1587 # line start marker
1588 records[nrecords] = encode(time, 0, 0, 0, line_start)
1589 nrecords += 1
1590
1591 for x in range(sizex):
1592 if nrecords < 0:
1593 break
1594 # pixel
1595 time_in_pixel = 0
1596
1597 # record all photons at different time
1598 for c in range(sizec):
1599 # channel
1600 for h in range(sizeh):
1601 # bin
1602 for _count in range(histogram[t, y, x, c, h]):
1603 # photon
1604 if nrecords >= maxrecords:
1605 nrecords = -1
1606 break
1607 records[nrecords] = encode(
1608 time, <uint32_t> h, <uint32_t> c, 0, 0
1609 )
1610 nrecords += 1
1611
1612 # increment time
1613 time += 1
1614 if time == maxtime:
1615 # overflow
1616 time = 0
1617 if nrecords >= maxrecords:
1618 nrecords = -1
1619 break
1620 records[nrecords] = encode(0, 0, 0, 1, 0)
1621 nrecords += 1
1622
1623 time_in_pixel += 1
1624 if time_in_pixel == pixel_time:
1625 break
1626 if nrecords < 0:
1627 break
1628 if time_in_pixel == pixel_time:
1629 break
1630 if nrecords < 0:
1631 break
1632 if time_in_pixel == pixel_time:
1633 break
1634
1635 if nrecords < 0:
1636 break
1637
1638 # move to next pixel
1639 # TODO: calculate overflows
1640 overflow = 0
1641 for _i in range(pixel_time - time_in_pixel):
1642 time += 1
1643 if time == maxtime:
1644 # overflow
1645 time = 0
1646 overflow += 1
1647 if overflow == maxoverflow:
1648 if nrecords >= maxrecords:
1649 nrecords = -1
1650 break
1651 records[nrecords] = encode(
1652 0, 0, 0, overflow, 0
1653 )
1654 nrecords += 1
1655 overflow = 0
1656 if nrecords >= 0 and overflow > 0:
1657 if nrecords >= maxrecords:
1658 nrecords = -1
1659 else:
1660 records[nrecords] = encode(0, 0, 0, overflow, 0)
1661 nrecords += 1
1662
1663 if nrecords < 0:
1664 break
1665 if nrecords >= maxrecords:
1666 nrecords = -1
1667 break
1668 # line end marker
1669 records[nrecords] = encode(time, 0, 0, 0, line_stop)
1670 nrecords += 1
1671
1672 if nrecords < 0:
1673 break
1674 if nrecords >= maxrecords:
1675 nrecords = -1
1676 break
1677 # frame change marker
1678 records[nrecords] = encode(time, 0, 0, 0, frame_change)
1679 nrecords += 1
1680
1681 if nrecords < 0:
1682 pass # error: records array too small
1683
1684 return nrecords
1685
1686
1687 cdef inline uint32_t encode_pt3(
1688 const uint32_t time,
1689 const uint32_t dtime,
1690 const uint32_t channel,
1691 const uint32_t overflow,
1692 const uint32_t marker,
1693 ) noexcept nogil:
1694 cdef:
1695 uint32_t record = time # & <uint32_t> 0xffff # 16 bit nsync
1696
1697 if marker != 0:
1698 # dtime is marker
1699 record |= marker << <uint32_t> 16
1700 # record |= (marker & 0xfff) << <uint32_t> 16
1701 # all channel bits set
1702 record |= <uint32_t> 0xf0000000
1703 elif overflow != 0:
1704 # dtime is 0, all channel bits set
1705 record |= <uint32_t> 0xf0000000
1706 else:
1707 # 12 bit dtime
1708 record |= dtime << <uint32_t> 16
1709 # record |= (dtime & 0xfff) << <uint32_t> 16
1710 # 4 bit channel, one-based
1711 record |= (channel + 1) << <uint32_t> 28
1712 # record |= ((channel + 1) & 0xf) << <uint32_t> 28
1713 return record
1714
1715
1716 cdef inline uint32_t encode_ht3(
1717 const uint32_t time,
1718 const uint32_t dtime,
1719 const uint32_t channel,
1720 const uint32_t overflow,
1721 const uint32_t marker,
1722 ) noexcept nogil:
1723 cdef:
1724 uint32_t record = time # & <uint32_t> 0x3ff # 10 bit nsync
1725
1726 record |= dtime << <uint32_t> 10 # 15 bit dtime
1727 # record |= (dtime & <uint32_t> 0x7fff) << <uint32_t> 10 # 15 bit dtime
1728
1729 if marker != 0:
1730 # 1 bit special
1731 record |= <uint32_t> 0x80000000
1732 # 4 bit marker instead of channel
1733 record |= marker << <uint32_t> 25
1734 # record |= (marker & 0xf) << <uint32_t> 25
1735 elif overflow != 0:
1736 # 1 bit special and all bits set instead of channel
1737 record |= <uint32_t> 0xfe000000
1738 # 10 bit multiple of 1024 increment of nsync instead of nsync
1739 record |= overflow
1740 # record |= overflow & <uint32_t> 0x3ff
1741 else:
1742 # 6 bit channel
1743 record |= channel << <uint32_t> 25
1744 # record |= (channel & <uint32_t> 0x3f) << <uint32_t> 25
1745 return record