File: roi_peaksearch.c

package info (click to toggle)
cbflib 0.9.7%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 65,272 kB
  • sloc: ansic: 131,361; python: 22,780; sh: 3,108; makefile: 2,088; yacc: 659; java: 223; f90: 214; xml: 210; cpp: 58
file content (566 lines) | stat: -rw-r--r-- 19,760 bytes parent folder | download | duplicates (3)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "cbf.h"
#include "cbf_simple.h"
#include "cbf_string.h"
#include "dps_peaksearch.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



void    bin2x2(unsigned short *data, int width, int height, unsigned short *out);
int     convertroi(char *roi, int fastdim, int slowdim,
                   int * fastlow, int * fasthigh, int * slowlow, int * slowhigh);





#define        MAX_PEAKS    20000

DPS_Peak    peaks[MAX_PEAKS];

int        max_peaks = MAX_PEAKS;

void    usage()
{
    fprintf(stderr,"Usage: roi_peaksearch [--binagain] [--min-peak ioversig] \\\n"
            "        [--region_of_interest fastlow,fasthigh,slowlow,showhigh] \\\n"
            "        [--overlay overlay_file.cbf] [--mask mask_file.cbf] \\\n"
            "        [--min-spacing min_spacing] \\\n"
            "        [--min-value min_value] \\\n"
            "        file.cbf peaklist \n");
}

int     main (int argc, char **argv)
{
    unsigned short *out;
    char * filename = NULL;      /* The input file name */
    char * maskname = NULL;      /* An optional mask file (e.g. BKGINIT.cbf) */
    char * overlayname = NULL;   /* An optional peak/roi overlay file */
    int        min_spacing = 6;  /* Minimum spacing in pixels between spots */
    int        min_value = 0;    /* Minimum valid value */
    double     ioversig = 2.;    /* Minimum peak height in I/sigma(I) */
    int    xsize, ysize;
    char * roi = NULL;
    char * strstrhit;
    int    n_peaks;
    int    i;
    FILE   *fp;
    FILE   *ovlp=NULL;
    
    
    int nStat = 0;
    
    cbf_handle hCBFile;
    cbf_handle hCBFmask;
    cbf_handle hCBFoverlay;
    
    int    nBinary_id, mask_nBinary_id;
    unsigned int compression, mask_compression;
    int    binary_id, mask_binary_id;
    size_t elsize, mask_elsize;
    int    elsigned, mask_elsigned;
    int    elunsigned, mask_elunsigned;
    size_t elements, elements_read, mask_elements, mask_elements_read;
    int    minelement, mask_minelement;
    int    maxelement, mask_maxelement;
    char*  byteorder ="little_endian";
    char*  mask_byteorder;
    size_t dim1, mask_dim1;
    size_t dim2, mask_dim2;
    size_t dim3, mask_dim3;
    size_t padding, mask_padding;
    int    nDim0, nDim1;
    int    fastlow, fasthigh, slowlow, slowhigh;
    unsigned short    *ushort_data, *up;
    int*   pnData, *ip;
    int*   pmaskData=NULL;
    int    lastgood;
    int    error;
    
    error = 0;
    hCBFile = hCBFmask = hCBFoverlay = NULL;
    
    if ( argc < 3 )
    {
        usage();
        exit(-1);
    }
    
    while (argc > 3) {
        if (argc > 4 && (0 == cbf_cistrcmp("--overlay-image",argv[1])
                         || (0 == cbf_cistrcmp("--overlay",argv[1]))
                         || (0 == cbf_cistrcmp("--ovl",argv[1])))) {
            overlayname = argv[2];
            argv+=2;
            argc-=2;
            ovlp = fopen(overlayname, "w+b");
            if (!ovlp) {
                fprintf(stderr,"roi_peaksearch: unable to open overlay cbf `%s'\n\n",overlayname);
                usage();
                exit(-1);
            }
            continue;
        }
        
        if (argc > 4 && (0 == cbf_cistrcmp("--mask",argv[1])
                         || (0 == cbf_cistrcmp("--BKGINIT",argv[1])))) {
            maskname = argv[2];
            argv+=2;
            argc-=2;
            if ( ( fp = fopen(maskname, "rb" ) ) == NULL ) {
                fprintf(stderr,"Error opening file\n");
                return(1);
            }
            error |= cbf_make_handle(&hCBFmask);
            
            if (0 != error) {
                fprintf(stderr,"roi_peaksearch: ERROR in mask cbf_make_handle\n");
                usage();
                exit(-1);
            }
            
            error |= cbf_read_widefile(hCBFmask, fp, MSG_DIGEST);
            if (0 != error) {
                fprintf(stderr,"roi_peaksearch: ERROR in mask cbf_read_widefile\n");
                usage();
                exit(-1);
            }
            
            error |= cbf_find_tag(hCBFmask, "_array_data.data");
            if (0 != error) {
                fprintf(stderr,"roi_peaksearch: ERROR in cbf_find_tag\n");
                usage();
                exit(-1);
            }
            
            error |= cbf_find_column   (hCBFmask, "data");
            if (0 != error)  {
                fprintf(stderr,"roi_peaksearch: ERROR in cbf_find_column data\n");
                usage();
                exit(-1);
            }

        
            error|= cbf_get_integerarrayparameters_wdims (hCBFmask, &mask_compression,
                                                          &mask_nBinary_id,
                                                          &mask_elsize,
                                                          &mask_elsigned,
                                                          &mask_elunsigned,
                                                          &mask_elements,
                                                          &mask_minelement,
                                                          &mask_maxelement,
                                                          (const char **) &mask_byteorder,
                                                          &mask_dim1, &mask_dim2, &mask_dim3, &mask_padding);
            if (0 != error)  {
                fprintf(stderr,"roi_peaksearch: ERROR in cbf_get_integerarrayparameters_wdims\n");
                usage();
                exit(-1);
            }
            
            if (mask_dim3 == 0) mask_dim3 = 1;
            if (mask_dim2 == 0) mask_dim2 = 1;
            if (mask_dim1 == 0) mask_dim1 = mask_elements;

            continue;
        }

        
        if (argc > 4 && (0 == cbf_cistrcmp("--min-i-over-sigma",argv[1])
                         || (0 == cbf_cistrcmp("--min-peak",argv[1])))) {
            ioversig = atof(argv[2]);
            argv+=2;
            argc-=2;
            continue;
        }
        if (argc > 4 && (0 == cbf_cistrcmp("--min-spacing",argv[1]))) {
            min_spacing = (int)(0.5+atof(argv[2]));
            if (min_spacing < 1) min_spacing = 1;
            argv+=2;
            argc-=2;
            continue;
        }
        if (argc > 4 && (0 == cbf_cistrcmp("--min-value",argv[1]))) {
            min_value = (int)(atof(argv[2]));
            argv+=2;
            argc-=2;
            continue;
        }
        if(0 == cbf_cistrcmp("--binagain", argv[1])
           || 0 == cbf_cistrcmp("-binagain",argv[1]))
        {
            argv++;
            argc--;
            continue;
        }
        if(argc > 4 && (0 == cbf_cistrcmp("--region-of-interest", argv[1])
                        || 0==cbf_cistrcmp("roi",argv[1]))) {
            if (roi) {
                fprintf(stderr,"roi_peaksearch: only one roi supported\n\n");
                usage();
                exit(-1);
            }
            roi = argv[2];
            argv += 2;
            argc -= 2;
            continue;
        }
        strstrhit = strstr(argv[1], "--region_of_interest");
        if(NULL != strstrhit && strstrhit==argv[1] ) {
            roi = argv[1]+strlen("--region_of_interest")+1;
            if (*(roi-1)!='='||strlen(roi)<3){
                fprintf(stderr,"roi_peaksearch: %s should be --region_of_interest=fastlow,fasthigh,slowlow,slowhigh\n\n", argv[1]);
                usage();
                exit(-1);
            }
            argv++;
            argc--;
            continue;
        }
        usage();
        exit(-1);
    }
    filename=argv[1];
    if ( ( fp = fopen(filename, "rb" ) ) == NULL ) {
        fprintf(stderr,"Error opening file\n");
        exit(-1);
    }
    error |= cbf_make_handle(&hCBFile);
    
    if (0 != error)  {
        fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_make_handle\n", error);
        exit(-1);
    }
    error |= cbf_read_widefile(hCBFile, fp, MSG_DIGEST);
    if (0 != error)  {
        fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_read_widefile\n", error);
        exit(-1);
    }
    
    error |= cbf_find_tag(hCBFile, "_array_data.data");
    if (0 != error)  {
        fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_find_tag\n", error);
        exit(-1);
    }
    
    error |= cbf_find_column   (hCBFile, "data");
    if (0 != error)  {
        fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_find_column data\n", error);
        exit(-1);
    }
    
    error |= cbf_get_integerarrayparameters_wdims (hCBFile, &compression,
                                                  &nBinary_id,
                                                  &elsize,
                                                  &elsigned,
                                                  &elunsigned,
                                                  &elements,
                                                  &minelement,
                                                  &maxelement,
                                                  (const char **) &byteorder,
                                                  &dim1, &dim2, &dim3, &padding);
    if (dim1 == 0) dim1 = elements;
    if (dim2 == 0) dim2 = 1;
    if (dim3 == 0) dim3 = 1;
    
    if (0 != error)  {
        fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_get_integerarrayparameters_wdims\n", error);
        exit(-1);
    }

    
    nDim0 = (int)dim1;
    nDim1 = (int)dim2;
    xsize = nDim0;
    ysize = nDim1;
    
    if (convertroi ( roi, xsize, ysize, &fastlow, &fasthigh, &slowlow, &slowhigh) ) {
        fprintf(stderr,"roi_peaksearch: invalid region of interest '%s'\n", roi);
        usage();
        exit(-1);
    }
    
    out = (unsigned short *)malloc ((fasthigh-fastlow+1)*(slowhigh-slowlow+1)*sizeof( unsigned short ));
    if (out == NULL) {
        fprintf(stderr,"roi_peaksearch: ERROR can not allocate %ld bytes\n",(long unsigned int) (fasthigh-fastlow+1)*(slowhigh-slowlow+1)*sizeof( unsigned short ));
        fflush(stderr); exit(-1);
    }
    if(NULL == (pnData = (int *) malloc(dim1 * dim2 * dim2 * elsize)))
    {
        fprintf(stderr,"roi_peaksearch: ERROR can not allocate %ld bytes for data\n",(long unsigned int) (fasthigh-fastlow+1)*(slowhigh-slowlow+1)*sizeof( unsigned short ));
        fflush(stderr);
        exit(-1);
    }
    if(NULL == (ushort_data = (unsigned short *) malloc(nDim0 * nDim1 * sizeof (unsigned short))))
    {
        return(1);
    }
    
    if (hCBFmask) {
        
        if(NULL == (pmaskData = (int *) malloc(mask_dim1 * mask_dim2 * mask_dim3 * elsize)))
        {
            fprintf(stderr,"roi_peaksearch: ERROR unable to allocate %ld bytes for mask\n",
                    (unsigned long)(mask_dim1 * mask_dim2 * mask_dim3 * elsize));
            fflush(stderr); exit(-1);
        }

        error |=  cbf_get_integerarray (hCBFmask,
                                        &mask_binary_id, (void *)pmaskData,
                                        mask_elsize, mask_elsigned,
                                        mask_elements, &mask_elements_read);
        
        if (0 != error) {
            fprintf(stderr,"roi_peaksearch: ERROR %x in mask cbf_get_integerarray\n", error);
            fflush(stderr); exit(-1);
        }

        
        
    }
    
    error |=  cbf_get_integerarray (hCBFile,
                                   &binary_id, (void *)pnData,
                                   elsize, elsigned,
                                   elements, &elements_read);
    
    if (0 != error) {
        fprintf(stderr,"roi_peaksearch: ERROR in cbf_get_integerarray\n");
        fflush(stderr); exit(-1);
    }
    
    int k;
    up = ushort_data;
    ip = pnData;
    k = nDim0*nDim1;
    for(i = 0; i < k; i++) {
        if ((0xFFFF0000 &*ip) != 0) {
            *up++ = 0xFFFD;
            ip++; }
        else  {
            lastgood = *ip++;
            if ((0xFFF0 & lastgood) == 0xFFF0) lastgood = 0xFFFF;
            *up++ = (unsigned short) (lastgood &0xFFFF);
        }
    }
    
    
    //    *data = ushort_data;
    if ( !overlayname ) free(pnData);
    
    //          istat = cbf2adscimg_sub(filename, &header, &raw);
    //      istat = rdfile ( filename, &hp, &lhead, (char**) &raw, &dim, size, &type );
    
    
    {
        unsigned short * ip = out;
        int i, j;
        
        for(j = slowlow; j <= slowhigh; j++) {
            for(i = fastlow; i <= fasthigh; i++) {
                *ip = 0x0000ffff &
                (int)ushort_data[j*xsize   +i];
                if (hCBFmask && i < mask_dim1 && j < mask_dim2 && (pmaskData[j*mask_dim1+i]&0xFFFC)==0xFFFC)
                    *ip = 0xFFFF;
                ip++;
            }
        }
        
        
        
    }
    
    n_peaks = dps_peaksearch(out,
                             fasthigh-fastlow+1, slowhigh-slowlow+1,
                             max_peaks, ioversig,
                             min_spacing,
                             peaks,
                             min_value);
    //        n_peaks = dps_peaksearch_cn(raw, xsize, ysize, max_peaks, ioversig, min_spacing, peaks);
    
    fprintf(stdout, "Number of peaks found with I/sigma > %.2f is %d\n", ioversig, n_peaks);
    fprintf(stdout, "PeaksCount : %d\n", n_peaks);
    fprintf(stdout, "Top 10 peaks:\n");
    fprintf(stdout, "    x       y       I/sigma\n");
    for(i = 0; i < 10; i++)
        fprintf(stdout, "%7.2f %7.2f  %9.2f\n", peaks[i].x+fastlow, peaks[i].y+slowlow, peaks[i].isigma);
    if(NULL == (fp = fopen(argv[2], "w")))
    {
        fprintf(stderr, "roi_peaksearch: Cannot create %s as output peaklist file\n", argv[2]);
        exit(0);
    }
    for(i = 0; i < n_peaks; i++) {
        int xlow, xhigh, ylow, yhigh, xcen, ycen, ix, iy;
        int heat, min_crossw, min_crossh;
        fprintf(fp, "%7.2f %7.2f  %9.2f\n", peaks[i].x+fastlow, peaks[i].y+slowlow, peaks[i].isigma);
        if (overlayname) {
            int tempx, tempy=0;
            min_crossw = (peaks[i].peakfw+1)/2-1;
            min_crossh = (peaks[i].peakfh+1)/2-1;
            if (min_crossw<1) min_crossw=1;
            if (min_crossh<1) min_crossh=1;
            xcen = peaks[i].x+fastlow+0.5;
            ycen = peaks[i].y+slowlow+0.5;
            xlow = xcen - peaks[i].peakfw;
            if (xlow < 0) xlow = 0;
            xhigh = xcen + peaks[i].peakfw;
            if (xhigh >= nDim0) xhigh = nDim0-1;
            ylow = ycen - peaks[i].peakfh;
            if (ylow < 0) ylow = 0;
            yhigh = ycen + peaks[i].peakfh;
            if (yhigh >= nDim1) yhigh = nDim1-1;
            heat = (int)(peaks[i].isigma/10.0+0.5);
            if (heat < 7) heat = 7;
            if (heat > 65520) heat = 65520;
            for (ix = xlow; ix <=xhigh; ix++) {
                tempy = ylow+rint((double)(ix-xlow)/((double)(xhigh-xlow))*(double)(yhigh-ylow));
                if (ix < xcen-min_crossw || ix > xcen+min_crossw) {
                    pnData[ix+nDim0*tempy] ^= heat;
                }
                if (ix <= xlow+1 || ix >= xhigh-1) {
                    pnData[ix+nDim0*tempy] ^= 0xFF00;
                }
            }
            for (iy = ylow; iy <=yhigh; iy++) {
                tempx = xhigh-rint((double)(iy-ylow)/((double)(yhigh-ylow))*(double)(xhigh-xlow));
                if (iy < ycen-min_crossh || iy > ycen+min_crossh) {
                    pnData[tempx+nDim0*iy] ^= heat;
                }
                if (iy <= ylow+1 || iy >= yhigh-1) {
                    pnData[tempy+nDim0*iy] ^= 0xFF00;
                }

            }
        }
    }
    fclose(fp);
    
    if (overlayname) {
        if (roi) {
            int xlow, xhigh, ylow, yhigh, ix, iy;
            if (fastlow > 0){
                xlow = fastlow-1;
                for (iy = slowlow; iy < slowhigh; iy+=2){
                    pnData[xlow+nDim0*iy] ^= 0xFF00;
                }
            }
            if (fasthigh < nDim0-1){
                xhigh = fasthigh+1;
                for (iy = slowlow; iy < slowhigh; iy+=2){
                    pnData[xhigh+nDim0*iy] ^= 0xFF00;
                }
            }
            if (slowlow > 0){
                ylow = slowlow-1;
                for (ix = fastlow; ix < fasthigh; ix+=2){
                    pnData[ix+nDim0*ylow] ^= 0xFF00;
                }
            }
            if (slowhigh < nDim1-1){
                yhigh = slowhigh+1;
                for (ix = fastlow; ix < fasthigh; ix+=2){
                    pnData[ix+nDim0*yhigh] ^= 0xFF00;
                }
            }

        }
        if (hCBFmask) {
            int i, j;
            
            for(j = slowlow; j <= slowhigh; j++) {
                for(i = fastlow; i <= fasthigh; i++) {
                    if (i < mask_dim1 && j < mask_dim2 && (pmaskData[j*mask_dim1+i]&0xFFFC)==0xFFFC)
                        pnData[i+nDim0*j] = -3;
                    ip++;
                }
            }

            
        }
        nStat = cbf_set_integerarray_wdims_fs(hCBFile,
                                     compression,
                                     binary_id,
                                     (void *)pnData,
                                     elsize,
                                     elsigned,
                                     elements,
                                     byteorder,
                                     dim1,dim2,dim3,padding);
        if (0 != nStat) fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_set_integerarray_wdims_fs\n", nStat);
        else {
            nStat = cbf_write_widefile (hCBFile, ovlp, 1, CBF, MIME_HEADERS | MSG_DIGEST, ENC_NONE|ENC_CRTERM | ENC_LFTERM);
            if (0 != nStat) fprintf(stderr,"roi_peaksearch: ERROR %x in cbf_write_widefile\n", nStat);
        }
        free(pnData);
        
    }
    
    // cbf_free_handle hopefully closes the file?
    
    nStat = cbf_free_handle (hCBFile);
    
    exit(0);
}


int convertroi(char *roi, int fastdim, int slowdim,
               int * fastlow, int * fasthigh, int * slowlow, int * slowhigh) {
    char * endptr;
    char * str;
    if (!fastlow || !fasthigh || !slowlow || !slowhigh) return CBF_ARGUMENT;
    *fastlow = *slowlow = 0;
    *fasthigh = fastdim-1;
    *slowhigh = slowdim-1;
    if (!roi) {
        return CBF_SUCCESS;
    }
    str = roi;
    *fastlow = (int)strtol(str,&endptr,0);
    if (*fastlow < 0) *fastlow = 0;
    if (*fastlow > fastdim-1) *fastlow = fastdim-1;
    if (*endptr == '\0') return CBF_SUCCESS;
    if (*endptr != ',') return CBF_FORMAT;
    str = endptr+1;
    *fasthigh = (int)strtol(str,&endptr,0);
    if (*fasthigh < *fastlow) *fasthigh = *fastlow;
    if (*fasthigh > fastdim-1) *fasthigh = fastdim-1;
    if (*endptr == '\0') return CBF_SUCCESS;
    if (*endptr != ',') return CBF_FORMAT;
    str = endptr+1;
    *slowlow = (int)strtol(str,&endptr,0);
    if (*slowlow < 0) *slowlow = 0;
    if (*slowlow > slowdim-1) *slowlow = slowdim-1;
    if (*endptr == '\0') return CBF_SUCCESS;
    if (*endptr != ',') return CBF_FORMAT;
    str = endptr+1;
    *slowhigh = (int)strtol(str,&endptr,0);
    if (*slowhigh < *slowlow) *slowhigh = *slowlow;
    if (*slowhigh > slowdim-1) *slowhigh = slowdim-1;
    if (*endptr == '\0') return CBF_SUCCESS;
    return CBF_FORMAT;
}



/*
 * bin an image 2x2
 */

void    bin2x2(unsigned short *data, int width, int height, unsigned short *out)
{
    register int j, i;
    int pixel;
    
    for(j=0;j<(height-1);j+=2)
        for(i=0;i<(width-1);i+=2)  {
            pixel = data[j*width + i] +
            data[j*width + i + 1] +
            data[(j+1)*width + i] +
            data[(j+1)*width + i + 1] ;
            if(pixel > 65535)
                *out++ = 65535;
            else
                *out++ = (pixel + 2)/4;
        }
}