File: metagraph.pas

package info (click to toggle)
mricron 0.20140804.1~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,508 kB
  • sloc: pascal: 114,876; sh: 49; makefile: 35
file content (690 lines) | stat: -rwxr-xr-x 21,950 bytes parent folder | download | duplicates (4)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
unit metagraph;
interface

uses
  {$IFNDEF Unix}Windows, Messages, {$ENDIF}
  SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ToolWin, ComCtrls,define_types, ExtCtrls, StdCtrls,  Menus,ClipBrd;

const
     kMaxCond = 6;
     knMaxRow = 20;  //Niftiimgvie
     kMaxLines = kMaxCond* knMaxRow;
     kClrRA: array [1..kMaxCond] of TColor = (clRed,clGreen,clBlue,clTeal,clAqua,clSilver);
     kPenStyleRA: array[1..knMaxRow] of TPenStyle = (psSolid,psDot,psDash,psDashDot,psDashDotDot,psSolid,psDot,psDash,psDashDot,psDashDotDot,
     psSolid,psDot,psDash,psDashDot,psDashDotDot,psSolid,psDot,psDash,psDashDot,psDashDotDot);
type
    TEventOnset =  RECORD
      Events: integer;
      ELabel: string[16];
      EventRA,DurRA: SingleP;
    END;

    T4DTrace =  RECORD
      //Title: string[16];
      //Samples: integer;
      HorzMin,HorzWidPerBin,SampleMin,SampleMax,SamplePlotMin,SamplePlotMax
      {SampleMean,SampleSD,SampleSE,SampleVar}: Double;
      //SampleRA: SingleP;
      Lines: array [1..kMaxLines] of TEventOnset;
      Conditions: array [1..kMaxLines] of TEventOnset;
      //DurationRA: array [1..kMaxLines] of SingleP;
    END;
procedure Create4DTrace (var l4DTrace: T4DTrace);
procedure Init4DTrace(lnSample,lnLines: integer; var l4DTrace: T4DTrace; lErrorBars: boolean);
procedure Close4DTrace (var l4DTrace: T4DTrace; lCloseCond: boolean);
procedure MinMax4DTrace(var l4DTrace: T4DTrace);
procedure CorePlot4DTrace(var l4DTrace: T4DTrace; lImage: TImage; lStartSample,HSpeed,lnColors: integer;lTR,lVertMin,lVertMax: single; lErrorBars: boolean);
procedure GraphResize(lImage: TImage);
procedure CloseCond (var l4DTrace: T4DTrace; lCond: integer);
procedure InitCond (var l4DTrace: T4DTrace; lCond, lnEvents: integer);


{$IFNDEF FPC} var   gWmf: TMetafile; {$ENDIF}
implementation

procedure GraphResize(lImage: TImage);
var
  TempBitmap: TBitmap;
  lx,ly: integer;
begin

  lx := lImage.Width;
  ly := lImage.Height;
  if (lx < 1) or (ly < 1) then exit;
  TempBitmap := TBitmap.Create;
  TempBitmap.Width := lx;
  TempBitmap.Height := ly;
  //Draw32Bitmap(TempBitmap.Canvas.Handle, lx, ly,lBuff {Self});
  lImage.Picture.Bitmap := TempBitmap;
  lImage.Width := lx;//delphi
  lImage.Height := ly;//delphi
  TempBitmap.Free;
end;

function RealToStr(lR: double {was extended}; lDec: integer): string;
begin
     if lR > 99999 then
        RealTOStr := FloatToStrF(lR, ffExponent	,lDec,7)
     else
         RealTOStr := FloatToStrF(lR, ffFixed,7,lDec);
end;

procedure Create4DTrace (var l4DTrace: T4DTrace);
var
   lLine: integer;
begin
     with l4DTrace do begin
        for lLine := 1 to kMaxLines do begin
          Lines[lLine].events := 0;
          Lines[lLine].elabel := '';
          Conditions[lLine].events := 0;
          Conditions[lLine].elabel := '';

        end;
     end; //with trace
end;

procedure Init4DTrace(lnSample,lnLines: integer; var l4DTrace: T4DTrace; lErrorBars: boolean);
var
   lLine: integer;
begin
     Close4DTrace(l4DTrace,lErrorBars);
     if (lnSample < 1) or (lnLines < 1) then
        exit;
     with l4DTrace do begin
          HorzMin := 0;
          HorzWidPerBin := 1;
          for lLine := 1 to lnLines do begin
               //getmem(DurationRA[lLine],lnSample*sizeof(single));
               //fx(lLine,lnSample);

            getmem(Lines[lLine].EventRA,lnSample*sizeof(single));
            Lines[lLine].events := lnSample;
            if lErrorBars then begin
               getmem(Conditions[lLine].EventRA,lnSample*sizeof(single));
               getmem(Conditions[lLine].DurRA,lnSample*sizeof(single));
               Conditions[lLine].events := lnSample;
            end;
          end; //for each line
     end; //with trace
end;

procedure Close4DTrace (var l4DTrace: T4DTrace; lCloseCond: boolean);
var
   lLine: integer;
begin
     with l4DTrace do begin
        for lLine := 1 to kMaxLines do begin
          if Lines[lLine].events > 0  then begin

             freemem(Lines[lLine].EventRA);
          end;
          Lines[lLine].events := 0;
          if lCloseCond then begin
             if Conditions[lLine].events > 0 then begin
                freemem(Conditions[lLine].EventRA);
                freemem(Conditions[lLine].DurRA); //1/1/2008
             end;
             Conditions[lLine].events := 0;
          end;
        end; //for each Line
     end; //with trace
end;

procedure CloseCond (var l4DTrace: T4DTrace; lCond: integer);
begin
     if (lCond < 1) or (lCond > kMaxLines) then
        exit;
     if l4DTrace.Conditions[lCond].events > 0 then begin
        freemem(l4DTrace.Conditions[lCond].EventRA);
        freemem(l4DTrace.Conditions[lCond].DurRA);
      end;
     l4DTrace.Conditions[lCond].events := 0;
end;

procedure InitCond (var l4DTrace: T4DTrace; lCond, lnEvents: integer);
begin
     if (lCond < 1) or (lCond > kMaxLines) then
        exit;
     CloseCond (l4DTrace, lCond);
     if lnEvents > 0 then begin
        getmem(l4DTrace.Conditions[lCond].EventRA, lnEvents * sizeof(single));
        //getmem(l4DTrace.DurationRA[lCond],lnEvents*sizeof(single));
        getmem(l4DTrace.Conditions[lCond].DurRA,lnEvents*sizeof(single));
        //fx(lLine,lnSample);
     end;
     l4DTrace.Conditions[lCond].events := lnEvents;
end;

procedure MinMax4DTrace(var l4DTrace: T4DTrace);
var lPos,lLine: integer;
l1stLine :boolean;
begin
  l1stLine := true;
  with l4DTrace do begin
     for lLine := 1 to kMaxLines do begin
         if Lines[lLine].events > 0  then begin
            if l1stLine then begin
                   SampleMin := Lines[lLine].EventRA^[1];
                   SampleMax:= Lines[lLine].EventRA^[1];
            end;
            l1stLine := false;

            for lPos := 1 to Lines[lLine].events do begin
                if Lines[lLine].EventRA^[lPos] > SampleMax then
                   SampleMax := Lines[lLine].EventRA^[lPos];
                if Lines[lLine].EventRA^[lPos] < SampleMin then
                   SampleMin := Lines[lLine].EventRA^[lPos];
            end; //for each event
         end; //if events > 0
     end; //for each line
     SamplePlotMin := SampleMin-0.1*abs(SampleMax-SampleMin);
     SamplePlotMax := SampleMax+0.1*abs(SampleMax-SampleMin);
  end; //with trace
end;

{$IFDEF FPC}
procedure HText(lImage: TImage; lX,lY,lDec: integer; lVal: single);
{$ELSE}
procedure HText(lImage: TMetafileCanvas; lX,lY,lDec: integer; lVal: single);
{$ENDIF}
var
   lStr: string;
begin
    if lDec >= 0 then
       lStr := realtostr(round(lVal),0)
    else
        lStr := realtostr(lVal,abs(lDec));
{$IFDEF FPC}
    lImage.Canvas.TextOut(lX-(lImage.Canvas.TextWidth(lStr) shr 1),lY,lStr);
{$ELSE}
    lImage.TextOut(lX-(lImage.TextWidth(lStr) shr 1),lY,lStr);
{$ENDIF}
end;

{$IFDEF FPC}
procedure VText(lImage: TImage; lX,lY,lDec: integer; lVal: single);
{$ELSE}
procedure VText(lImage: TMetafileCanvas; lX,lY,lDec: integer; lVal: single);
{$ENDIF}
var
   lStr: string;
begin
    if lDec >= 0 then
       lStr := realtostr(round(lVal),0)
    else
        lStr := realtostr(lVal,abs(lDec));
{$IFDEF FPC}
    lImage.Canvas.TextOut(lX-lImage.Canvas.TextWidth(lStr) ,lY,lStr);
{$ELSE}
    lImage.TextOut(lX-lImage.TextWidth(lStr) ,lY,lStr);
{$ENDIF}

end;

{$IFDEF FPC}
procedure VTextLeftJustified(lImage: TIMage; lX,lY,lDec: integer; lVal: single);
{$ELSE}
procedure VTextLeftJustified(lImage: TMetafileCanvas; lX,lY,lDec: integer; lVal: single);
{$ENDIF}
var
   lStr: string;
begin
    if lDec >= 0 then
       lStr := inttostr(round(lVal))
    else
        lStr := realtostr(lVal,abs(lDec));
{$IFDEF FPC}
    lImage.Canvas.TextOut(lX ,lY,lStr);
{$ELSE}
    lImage.TextOut(lX ,lY,lStr);
{$ENDIF}

end;

{$IFDEF FPC}
procedure ShowRange(lImage: TImage; lMin,lMax: single; lL,lT,lR,lB,lPosition: integer);
{$ELSE}
procedure ShowRange(lImage: TMetafileCanvas; lMin,lMax: single; lL,lT,lR,lB,lPosition: integer);
{$ENDIF}
//position 1=L,2=T,3=R,4=B
var
   lRangeR,lRange,lD,lV: double;
   lDecimals,lPos,lLo,lHi,lHPos,lOffset : integer;
begin
{$IFDEF FPC}
with lImage.Canvas do begin
{$ELSE}
with lImage do begin
{$ENDIF}
                Font.color := clBlack;
    lRange := abs(lMax-lMin);
    lRangeR := lRange;
    lDecimals := 0;
    lD := 1;
    if lRangeR = 0 then
       exit;
    while lRangeR > 10 do begin//get range 1..10
          lRangeR := lRangeR / 10;
          inc(lDecimals);
          lD := lD * 10;
    end;
    while lRangeR < 1 do begin//get range 1..10
          lRangeR := lRangeR * 10;
          dec(lDecimals);
          lD := lD / 10;
    end;
    lLo := round((lMin + (lD/2)) / lD);
    lHi := trunc((lMax + (lD/20) ) / lD);//2007
    //lHi := trunc((lMax ) / lD);
    if lHi <= (lLo+2) then begin
        lD := lD /2;
        if lDecimals <= 0 then
           dec(lDecimals)
        else
           inc(lDecimals);
        lLo := round((lMin + (lD/2)) / lD);
        lHi := trunc((lMax + (lD/20) ) / lD);//2007
    end;
    if (lPosition = 2{T}) or (lPosition = 4{B}) then begin
       lOffset :=  TextHeight('0');
       for lPos := lLo to lHi do begin
        lV := lPos * lD;
        lHPos := lL+ round( ((lV-lMin) / lRange)* abs(lR-lL));
        if (lPosition = 2{T}) then
           HText(lImage, lHPos,lT- lOffset,lDecimals,lV)
        else
            HText(lImage, lHPos,lB+1,lDecimals,lV);
       end;
    end else if (lPosition = 1{L}) or (lPosition = 3{R}) then begin //vertical values
       lOffset :=  TextHeight('0') div 2; //2007
       for lPos := lLo to lHi do begin
        lV := lPos * lD;
        {lHPos := lB- round( ((lV-lMin) / lRange)* abs(lT-lB));
        lImage.MoveTo(1,lHPos);
        lImage.LineTo(1000,lHPos);}
        lHPos := lB- round( ((lV-lMin) / lRange)* abs(lT-lB))-lOffset;

        if (lPosition = 1{L}) then
           VText(lImage, lL-1,lHPos,lDecimals,lV)
        else
            VTextLeftJustified(lImage, lR+1,lHPos,lDecimals,lV);
       end;

    end; //if vertical
  end; //with limage
end;

{$IFDEF FPC}
function ShowLegend(var l4DTrace: T4DTrace; lImage: TImage; lL,lT: integer): integer;
{$ELSE}
function ShowLegend(var l4DTrace: T4DTrace; lImage: TMetafileCanvas; lL,lT: integer): integer;
{$ENDIF}
var
   lC,lLegendLeft: integer;
begin
{$IFDEF FPC}
with lImage.Canvas do begin
{$ELSE}
with lImage do begin
{$ENDIF}
  lLegendLeft := lL;
  font.color := clBlack;
  for lC := 1 to kMaxCond do begin
      //lImage.canvas.pen.color := kClrRA[lC];
      font.color := kClrRA[lC] ;
      if (l4DTrace.Conditions[lC].events > 0)  then begin
        TextOut(lLegendLeft,lT,l4DTrace.Conditions[lC].ELabel);
        lLegendLeft := lLegendLeft + TextWidth(l4DTrace.Conditions[lC].ELabel)+5;
      end; //for each tevent
  end; //if cond has events
  result := lLegendLeft;
end; //with limage
end; //for each cond

function n4DTrace(var l4DTrace: T4DTrace;var  lSamples: integer; lErrorBars: boolean): integer;
var lLine: integer;
l1stLine :boolean;
begin
  lSamples:= 0;
  result := 0;
  l1stLine := true;
  with l4DTrace do begin
     for lLine := 1 to kMaxLines do begin
         if Lines[lLine].events > 0  then begin
            if l1stLine then
               lSamples := Lines[lLine].events;
            l1stLine := false;
            if  (lErrorBars) and (Lines[lLine].events <> lSamples) then
               exit; //all lines must have same number of samples
            inc(result);
         end; //if events > 0
     end; //for each line
  end; //with trace
end;

{$IFDEF FPC}
function SetColorStyle (lImage: TImage; lLine,lnColors: integer): TPenStyle;
{$ELSE}
function SetColorStyle (lImage: TMetafileCanvas; lLine,lnColors: integer): TPenStyle;
{$ENDIF}
var
   lC: integer;
begin
{$IFDEF FPC}
with lImage.Canvas do begin
{$ELSE}
with lImage do begin
{$ENDIF}
     if lnColors < 1 then begin
        pen.color := clBlack;//clRed
        pen.style := kPenStyleRA[lLine];
        result := kPenStyleRA[lLine];
        exit;
     end;
     lC := lLine mod lnColors;
     if lC = 0 then
        lC := lnColors;
     pen.color := kClrRA[lC];
     lC := ((lLine-1) div lnColors)+1;
     pen.style := kPenStyleRA[lC];
     result := kPenStyleRA[lC];
end; //with lImage.
end;

{$IFDEF FPC}
procedure ShowLineLegend(var l4DTrace: T4DTrace; lImage: TImage;  lL, lT,lnLines,lnColors: integer);
{$ELSE}
procedure ShowLineLegend(var l4DTrace: T4DTrace; lImage: TMetafileCanvas;  lL, lT,lnLines,lnColors: integer);
{$ENDIF}
var
   lLineTop,lStyle,lnStyles,lLegendLeft: integer;
begin
  if lnColors < 1 then
     lnStyles := lnLines
  else
      lnStyles := lnLines div lnColors;
  if lnStyles < 1 then
     lnStyles := 1;
{$IFDEF FPC}
with lImage.Canvas do begin
{$ELSE}
with lImage do begin
{$ENDIF}

  font.color := clBlack;
  pen.color := clBlack;
  lLegendLeft := lL;
  lLineTop := lT+(TextHeight('X') div 2);
  for lStyle := 1 to lnStyles do begin
      pen.style := kPenStyleRA[lStyle];
      MoveTo(lLegendLeft,lLineTop);
      lLegendLeft := lLegendLeft +40;
      LineTo(lLegendLeft,lLineTop);
      lLegendLeft := lLegendLeft + 2;
      TextOut(lLegendLeft,lT,l4DTrace.Lines[lStyle].ELabel);
      lLegendLeft := lLegendLeft + TextWidth(l4DTrace.Lines[lStyle].ELabel)+5;
  end;
  pen.style := psSolid;
end;//with lImage.
end;

{$IFDEF FPC}
procedure ShowPlot(var l4DTrace: T4DTrace; lImage: TImage; lL,lT,lR,lB,lStartSample,lHSpeedIn,lScalePos,lnColors: integer; lSecPerSample,lVertMin,lVertMax: single; lShowHRange,lErrorBars: boolean);
{$ELSE}
procedure ShowPlot(var l4DTrace: T4DTrace; lImage: TMetafileCanvas; lL,lT,lR,lB,lStartSample,lHSpeedIn,lScalePos,lnColors: integer; lSecPerSample,lVertMin,lVertMax: single; lShowHRange,lErrorBars: boolean);
{$ENDIF}
const
     kMinMax = 0;
     k2SD = 1;
     k12bit = 2;
     kMaxPt = 16000;
type
    TPtRA= array [1..kMaxPt] of TPoint;
var
   lnPt,lnLines,lLine,lnSamples,lC,lStartSamp,lEndSamp,lEndPix,lPos,lI: integer;
   lVert,lHorz,lVMax,lVMin,lScale,lHSpeed: single;
   lPenStyle: TPenStyle;
   lPtRA: TPtRA;
begin
  lnLines := n4DTrace(l4DTrace,lnSamples,lErrorBars);
  if (lnLines < 1) or (lnSamples < 2) or (lB <= lT) then exit;
  lStartSamp := lStartSample;
  if  (lStartSamp > lnSamples) then
     exit;
  if lStartSamp < 1 then
     lStartSamp := 1;
  lHSpeed := lHSpeedIn;
  if lHSpeed < 1 then begin
     lStartSamp := 1;
     lHSpeed := (lnSamples-1)/(lR-lL);
  end;
{$IFDEF FPC}
with lImage.Canvas do begin
{$ELSE}
with lImage do begin
{$ENDIF}
  lEndSamp := trunc(lStartSamp + ((lR-lL)*lHSpeed))+1;
     ShowRange(lImage, l4DTrace.HorzMin+((lStartSamp-1)*l4DTrace.HorzWidPerBin),l4DTrace.HorzMin+((lEndSamp-1)*l4DTrace.HorzWidPerBin),lL,lT,lR,lB,4);
  if lShowHRange then
     ShowRange(lImage, l4DTrace.HorzMin+((lStartSamp-1)*l4DTrace.HorzWidPerBin),l4DTrace.HorzMin+((lEndSamp-1)*l4DTrace.HorzWidPerBin),lL,lT,lR,lB,4);

  lI := ShowLegend(l4DTrace,lImage, lL,5);
  ShowLineLegend(l4DTrace, lImage, lI+10, 5,lnLines,lnColors);
  //next show event onsets
  if not lErrorBars then
  for lC := 1 to kMaxCond do begin
      pen.color := kClrRA[lC];
      if (l4DTrace.Conditions[lC].events > 0) and (lSecPerSample > 0) then begin
        //canvas.TextOut(lLegendLeft,lT-canvas.TextHeight('X')-2,l4DTrace.Conditions[lC].ELabel);
        //lLegendLeft := lLegendLeft + canvas.TextWidth(l4DTrace.Conditions[lC].ELabel)+5;
        for lPos := 1 to l4DTrace.Conditions[lC].events do begin
          lHorz := l4DTrace.Conditions[lC].EventRA^[lPos] / lSecPerSample;
          if (lHorz < lEndSamp)  and (lHorz > lStartSamp) then begin
           lVert := ((lHorz - lStartSamp) / lHSpeed)+lL;
           moveto(round(lVert),lT);
           lineto(round(lVert),lB);
          end; //if event in range
        end; //for each tevent
      end; //if cond has events
  end; //for each cond
  if (lEndSamp > lnSamples) then begin
     lEndSamp := lnSamples;
     lEndPix := lL+trunc((lnSamples-lStartSamp) / lHSpeed);
  end else
      lEndPix := lR;
  lVMax := lVertMax;
  lVMin := lVertMin;
  if (lVMax <= lVMin) then begin
     lVMax := l4DTrace.SamplePlotMax;
     lVMin :=  l4DTrace.SamplePlotMin;
  end;
  if (lVMax <  l4DTrace.SampleMin) or (lVMin >  l4DTrace.SampleMax)  then begin
     lVMax := l4DTrace.SamplePlotMax;
     lVMin :=  l4DTrace.SamplePlotMin;
  end;

  ShowRange(lImage,lVMin,lVMax,lL,lT,lR,lB,lScalePos);
  moveto(lL,lT);
  if lVMax <= lVMin then
     lScale := 1
  else
      lScale := (lB-lT)/ (lVMax-lVMin);
  if lHSpeed < 1 then begin
   //lHSpeed := (l4DTrace.Samples-1)/(lR-lL);
   for lLine := 1 to lnLines do begin
     lPenStyle := SetColorStyle (lImage, lLine,lnColors);
     lnPt := 0;
     for lPos := lStartSamp to lEndSamp do begin
      lVert := l4DTrace.Lines[lLine].EventRA^[lPos];
      if lVert > lVMax then
         lVert := lVMax
      else if lVert < lVMin then
           lVert := lVMin;
      lVert := round((lVert-lVMin)*lScale);
      lVert := lB-lVert;
      lHorz := lL+round((lPos-lStartSamp)/lHSpeed);
      inc(lnPt);
      if lnPt < kMaxPt then
         lPtRA[lnPt] := Point(round(lHorz),round(lVert));
      if lErrorBars then begin
         pen.style := psSolid;
         moveto(round(lHorz),round(lVert-(l4DTrace.Conditions[lLine].EventRA^[lPos]*lScale)));
         lineto(round(lHorz) ,round(lVert+(l4DTrace.Conditions[lLine].EventRA^[lPos]*lScale)));
         moveto(round(lHorz) ,round(lVert));
         pen.style := lPenStyle;
      end;
     end; //for lPos
      if lnPt > kMaxPt then
         lnPt := kMaxPt;
      if lnPt > 0 then
         PolyLine( Slice(lPtRA, lnPt));

   end; //for each line
  end else begin //HSpeed >=1 so every pixel unique
     for lLine := 1 to lnLines do begin
     lPenStyle := SetColorStyle (lImage, lLine,lnColors);
   lI := lStartSamp;
   lnPt := 0;
   for lPos := lL to lEndPix do begin
      lVert := l4DTrace.Lines[lLine].EventRA^[lI];
      if lVert > lVMax then
         lVert := lVMax
      else if lVert < lVMin then
           lVert := lVMin;
      lVert := round((lVert-lVMin)*lScale);
      //lVert := lVert + lT;
      lVert := lB-lVert;
      inc(lnPt);
      if lnPt < kMaxPt then
         lPtRA[lnPt] := Point(lPos,round(lVert));

      if lErrorBars then begin
           pen.style := psSolid;
         moveto(lPos,round(lVert-(l4DTrace.Conditions[lLine].EventRA^[lPos]*lScale)));
         lineto(lPos ,round(lVert+(l4DTrace.Conditions[lLine].EventRA^[lPos]*lScale)));
         moveto(lPos ,round(lVert));
         pen.style := lPenStyle;
      end;
      lI := round( lStartSamp+((lPos-lL)*lHSpeed) );
      if lI < 1 then
         lI := 1;
      if lI > lEndSamp then
         lI := lEndSamp;
    end; //for lPos
      if lnPt > kMaxPt then
         lnPt := kMaxPt;
      if lnPt > 0 then
         PolyLine( Slice(lPtRA, lnPt))

      end; //for each line
   end; //hspeed >= 1
   pen.style := psSolid;
end;//with .lImage
end;



procedure DrawBMP( lx, ly: integer;  {lBuff: RGBQuadp;} var lImage: TImage);
var
  TempBitmap: TBitmap;
begin
  TempBitmap := TBitmap.Create;
    TempBitmap.Width := lx;
    TempBitmap.Height := ly;
    //Draw32Bitmap(TempBitmap.Canvas.Handle, lx, ly,lBuff {Self});
    lImage.Picture.Bitmap := TempBitmap;
  lImage.Width := lx;//delphi
  lImage.Height := ly;//delphi
    TempBitmap.Free;
end;


 {$IFDEF FPC}
procedure PrepPlot(var lImage: TIMage; lL,lT,lR,lB,lWid,lHt,lFontSize: integer);
{$ELSE}
procedure PrepPlot(var lImage: TMetafileCanvas; lL,lT,lR,lB,lWid,lHt,lFontSize: integer);
{$ENDIF}
begin
{$IFDEF FPC}
   with lImage.Canvas do begin
{$ELSE}
   with lImage do begin
{$ENDIF}
     Font.Name := 'Arial';
     Font.Size := 12;
     pen.color := clBlack;
     Font.color := clBlack;
     Brush.Style := bsSolid;
     Brush.color := clWhite;
     Rectangle(1,1,lWid,lHt);
     Rectangle(lL,lT,lR,lB);
   end;
end;

procedure CorePlot4DTrace(var l4DTrace: T4DTrace; lImage: TImage; lStartSample,HSpeed,lnColors: integer;lTR,lVertMin,lVertMax: single; lErrorBars: boolean);
var
   lWid,lHt,lBorder,lL,lT,lR,lB,lFontSize: integer;
{$IFDEF FPC}
  //WmfCanvas: TCanvas;

{$ELSE}
  WmfCanvas: TMetafileCanvas;
{$ENDIF}
begin
     lWid := lImage.Width;
     lHt := lImage.Height;
     lFontSize := 12;
     lBorder := lFontSize * 4;
     if (lWid <= (2*lBorder)) or (lHt <= (2*lBorder)) then
        exit;

     lL := round(1.3*lBorder);
     lT :=lFontSize*2;
     lR := lWid - lBorder;
     lB := lHt-(lFontSize*2);
{$IFDEF FPC}
     //WmfCanvas := TCanvas.Create;
          PrepPlot(lImage,lL,lT,lR,lB,lWid,lHt,lFontSize);
          ShowPlot(l4DTrace,lImage,lL,lT,lR,lB,lStartSample,HSpeed,1,lnColors, lTR,lVertMin,lVertMax,true,lErrorBars);
//abba lImage.Canvas.Draw (0, 0, WmfCanvas);
     //WmfCanvas.Free;
{$ELSE}
     gWmf.clear;
     gWmf.Width := lWid;
     gWmf.Height := lHt;
     WmfCanvas := TMetafileCanvas.CreateWithComment(gWmf, 0, 'mricron', 'plot metafile');
     try
          PrepPlot(WmfCanvas,lL,lT,lR,lB,lWid,lHt,lFontSize);
          ShowPlot(l4DTrace,WmfCanvas,lL,lT,lR,lB,lStartSample,HSpeed,1,lnColors, lTR,lVertMin,lVertMax,true,lErrorBars);
     finally
            WmfCanvas.Free;
     end;//finally
     lImage.Canvas.Draw (0, 0, gWmf);
{$ENDIF}
end;


initialization
begin
{$IFDEF FPC}
{$ELSE}
  gWmf := TMetafile.Create;
  gWmf.Enhanced := True;
{$ENDIF}
  //   Create4DTrace(g4Ddata);
end;

finalization
begin
 //Close4DTrace(g4Ddata);
{$IFDEF FPC}
{$ELSE}
     gWmf.free;
{$ENDIF}

end;


end.