-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_ReadGG.cs
964 lines (848 loc) · 39.9 KB
/
mod_ReadGG.cs
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using System.Data;
namespace OctTools
{
public class mod_GG
{
public const int RAWHEADER_SZ_SIZE = 101;
public const int RAWHEAD_SZNOTE_SIZE = 512;
public struct ggHeader_struct
{
public Int32 cbsize; // raw header size of bytes
public string szCompanyName; // company name
public string szVersion; // version
public string szDate; // stamp date
public string szTime; // stamp time
public string szPatientid; // patient id
public string szLastname;
public string szFirstname;
public string szSex;
public string szDateofBirth;
public string szPhysican1;
public string szPhysican2;
public string szNote;
public int video_on; // capture video is enable
public int video_cx;
public int video_cy;
public int video_bytesPerRow;
public int video_pixelDepth;
public int video_ColorTables,
video_BmpBytes,
video_MasksBytes;
public int numberof_frames; //number of frame
public int elapseof_frame; //elaspe time of even frame
public int DD; //DD start pix for full range OCT display
public int G; //start pix for select range OCT display
public int SN; //Start Number for select range OCT display
public int NumberOfline_360; //nuber of scan ling of a round
public float Angle2;
public int numberofline; //number of scan lines
public int numberofpointofline; //number of point for ever scan line
public uint offsetframe; // number of byte for first raw block
public uint cbsize_frame; // frame size of bytes
}
public struct ggFrame_struct
{
public Int32 cb_size;
public uint rawdatasize; //OCT 原始数据的大小
public uint rawdataoffset; //number of bytes for raw data offset
public uint videoimagesize; //视频捕捉图像的数据大小
public uint imageoffset; //number of bytes for video capture image data offset
public double raw_maxvalue; //OCT raw 数据的最大值
public double raw_minvalue; //OCT raw 数据的最小值
public Int32 m_savetickcount; // OCT 数据保存的时间间隔 原定义DWORD
public double m_saveposition; // OCT pull back 的位置
}
public struct ggData_struct
{
public string m_szFileName;
public ggHeader_struct m_ggheader;
public ggFrame_struct m_ggframe_info;
public uint m_currpos; // current read data position for second thread
// oct raw data information
public int elapseof_frame; //elaspe time of even frame
public uint numberof_frames;
public int numberofline; //number of scan lines
public int numberofpointofline; //number of point for ever scan line
public int DD; //DD start pix for full range OCT display
public int G; //start pix for select range OCT display
public int SN; //Start Number for select range OCT display
public int NumberOfline_360; //nuber of scan ling of a round
public float Angle2;
// other image data information
public int video_on; // capture video is enable
public int video_cx;
public int video_cy;
public int video_bytesPerRow;
public int video_pixelDepth;
public int video_ColorTables,
video_BmpBytes,
video_MasksBytes;
public uint m_sizeofrawdata;
public uint m_sizeofimagedata;
public byte[] m_lpframedata; //whole frame and oct's data and image's data buffer
public byte[] m_lpframeinfo; // frame infomation from file
public byte[] m_lprawdata; //temp raw data buf for second thread
public byte[] m_lpimagedata; //temp image data buf for second thread
}
public ggData_struct ggData;
public double[,] ggRecordBeltData;
public byte[] ggAllFrameInfo;
public byte[] ggAllRawData;
//public mod_ReadGen2.Gen2_Struct[] Gen2Array;
public string[,] ggMark;
public int iCurrentPos = 0;
//BinaryReader m_hfile; // save file handle
BinaryReader br = null;
FileStream fs = null;
// 3、一些参数
const double PI = 3.1415926535897932384626433832795028841971;
const double PI_45 = PI / 4;
const double PI_90 = PI / 2;
const double PI_180 = PI; // 180度的圆周值
const double PI_360 = PI * 2; // 360 度的圆周值
const double PI_360_Div = PI * 2 / 360.0; // 细分每一度的圆周值
public mod_GG()
{
mod_GG_Init();
}
public void mod_GG_Init()
{
ggData = new ggData_struct();
ggData.m_ggheader = new ggHeader_struct();
ggData.m_ggframe_info = new ggFrame_struct();
ggAllFrameInfo = null;
ggAllRawData = null;
ggRecordBeltData = null;
ggMark = null;
//Gen2Array = null;
iCurrentPos = -1;
br = null;
fs = null;
}
public int ReadggHeader(string ggFilePath, ref string sErrString)
{
int iResult = MyData.iErr_UnknowErr;
int iR = MyData.iErr_UnknowErr;
int i = 0, j = 0;
string sTmp = "";
int DataLen = 0;
float fData = 0.0f;
if (ggFilePath == "")
{
sErrString = "没有文件名";
return MyData.iErr_NoFile;
}
if (!File.Exists(ggFilePath))
{
sErrString = "文件不存在";
return MyData.iErr_NoFile;
}
ggData.m_szFileName = ggFilePath;
try
{
iResult = MyData.iErr_Succ;
sErrString = "";
fs = new FileStream(ggData.m_szFileName, FileMode.Open, FileAccess.Read);
//sr = new StreamReader(fs);
br = new BinaryReader(fs);
ggData.m_ggheader.cbsize = br.ReadInt32();
for (i = 0; i < 12; i++)
{
if (i < 11)
DataLen = RAWHEADER_SZ_SIZE;
else
DataLen = RAWHEAD_SZNOTE_SIZE;
if (ReadFileString(br, DataLen, ref sTmp, ref sErrString) == MyData.iErr_Succ)
{
switch (i)
{
case 0:
ggData.m_ggheader.szCompanyName = sTmp; // company name
break;
case 1:
ggData.m_ggheader.szVersion = sTmp; // version
break;
case 2:
ggData.m_ggheader.szDate = sTmp; // stamp date
break;
case 3:
ggData.m_ggheader.szTime = sTmp; // stamp time
break;
case 4:
ggData.m_ggheader.szPatientid = sTmp; // patient id
break;
case 5:
ggData.m_ggheader.szLastname = sTmp;
break;
case 6:
ggData.m_ggheader.szFirstname = sTmp;
break;
case 7:
ggData.m_ggheader.szSex = sTmp;
break;
case 8:
if (sTmp.Trim() == "")
{
ggData.m_ggheader.szDateofBirth = "1900-01-01 12:00:00";
}
else
{
ggData.m_ggheader.szDateofBirth = sTmp.Substring(6, 4) + "-" + sTmp.Substring(0, 5);
}
break;
case 9:
ggData.m_ggheader.szPhysican1 = sTmp;
break;
case 10:
ggData.m_ggheader.szPhysican2 = sTmp;
break;
case 11:
ggData.m_ggheader.szNote = sTmp;
break;
}
}
else
{
goto Eend;
}
}
// 跳过对齐字节
br.BaseStream.Seek(1, SeekOrigin.Current);
for (i = 0; i < 19; i++)
{
if (i != 14)
iR = ReadFileInt(br, ref j, ref sErrString);
else
iR = ReadFileFloat(br, ref fData, ref sErrString);
if (iR == MyData.iErr_Succ)
{
switch (i)
{
case 0:
ggData.m_ggheader.video_on = j; // company name
break;
case 1:
ggData.m_ggheader.video_cx = j; // version
break;
case 2:
ggData.m_ggheader.video_cy = j; // stamp date
break;
case 3:
ggData.m_ggheader.video_bytesPerRow = j; // stamp time
break;
case 4:
ggData.m_ggheader.video_pixelDepth = j; // patient id
break;
case 5:
ggData.m_ggheader.video_ColorTables = j;
break;
case 6:
ggData.m_ggheader.video_BmpBytes = j;
break;
case 7:
ggData.m_ggheader.video_MasksBytes = j;
break;
case 8:
ggData.m_ggheader.numberof_frames = j;
break;
case 9:
ggData.m_ggheader.elapseof_frame = j;
break;
case 10:
ggData.m_ggheader.DD = j;
break;
case 11:
ggData.m_ggheader.G = j;
break;
case 12:
ggData.m_ggheader.SN = j;
break;
case 13:
ggData.m_ggheader.NumberOfline_360 = j;
break;
case 14:
ggData.m_ggheader.Angle2 = fData;
break;
case 15:
ggData.m_ggheader.numberofline = j;
break;
case 16:
ggData.m_ggheader.numberofpointofline = j;
break;
case 17:
ggData.m_ggheader.offsetframe = (uint)j;
break;
case 18:
ggData.m_ggheader.cbsize_frame = (uint)j;
break;
}
}
else
{
goto Eend;
}
} // 至此应该到达:读完ThisggHeader.cbsize长度的位置
ggData.m_currpos = 0; // current write data position for second thread
// oct raw data information
ggData.elapseof_frame = ggData.m_ggheader.elapseof_frame; //elaspe time of even frame
ggData.numberof_frames = (uint)ggData.m_ggheader.numberof_frames; // number of frames in the serial file
ggData.numberofline = ggData.m_ggheader.numberofline; //number of scan lines
//lpdata->numberofpointofline = lpdata->m_rawheader.numberofpointofline;
//number of point for ever scan line
ggData.numberofpointofline = ggData.m_ggheader.numberofpointofline; //number of point for ever scan line
ggData.DD = ggData.m_ggheader.DD; //DD start pix for full range OCT display
ggData.G = ggData.m_ggheader.G; //start pix for select range OCT display
ggData.SN = ggData.m_ggheader.SN; //Start Number for select range OCT display
ggData.NumberOfline_360 = ggData.m_ggheader.NumberOfline_360; //nuber of scan ling of a round
ggData.Angle2 = ggData.m_ggheader.Angle2;
// other image data information
ggData.video_on = ggData.m_ggheader.video_on; // capture video is enable
ggData.video_cx = ggData.m_ggheader.video_cx;
ggData.video_cy = ggData.m_ggheader.video_cy;
ggData.video_bytesPerRow = ggData.m_ggheader.video_bytesPerRow;
ggData.video_pixelDepth = ggData.m_ggheader.video_pixelDepth;
ggData.video_ColorTables = ggData.m_ggheader.video_ColorTables;
ggData.video_BmpBytes = ggData.m_ggheader.video_BmpBytes;
ggData.video_MasksBytes = ggData.m_ggheader.video_MasksBytes;
//lpdata->m_lpframedata = malloc(lpdata->m_rawheader.cbsize_frame);
//if (lpdata->m_lpframedata)
//{
// blResult = ReadRawFrameDataFromFile_FirstFrame(lpdata);
// //if(SetFilePointer(lpdata->m_hfile, (LONG)lpdata->m_rawheader.offsetframe, NULL, FILE_BEGIN) == lpdata->m_rawheader.offsetframe)
// //{
// // read first frame
// // blResult = ReadRawFrameDataFromFile(lpdata);
// //}else
// // blResult = FALSE;
//}
//else
//blResult = FALSE;
iResult = MyData.iErr_Succ;
}
catch (Exception ex)
{
sErrString = "读gg文件出错(" + ex.Message + ") j = " + j.ToString() + ",i = " + i.ToString();
iResult = MyData.iErr_Exception;
}
Eend:
return iResult;
}
public void ggClose()
{
if (br != null)
{
br.Close();
}
if (fs != null)
{
fs.Close();
}
ggData.m_ggheader = new ggHeader_struct();
ggData.m_ggframe_info = new ggFrame_struct();
ggData = new ggData_struct();
}
public void ggFileClose()
{
if (br != null)
{
br.Close();
}
if (fs != null)
{
fs.Close();
}
}
// ------------------------------------------------------------------------------------------
// 从已经打开的序列文件中读出一个帧的数据
// ------------------------------------------------------------------------------------------
public bool ReadRawFrameDataFromFile(ref ggFrame_struct FrameInfoBuff, ref byte[] FrameRawBuff, ref byte[] FrameImageBuff, ref string sErrString)
{
uint dwlen, dwsize, dwcpsize;
byte[] Datatmp;
IntPtr buffer = Marshal.AllocHGlobal(1);
bool bResult = false;
try
{
dwlen = ggData.m_ggheader.cbsize_frame;
Datatmp = br.ReadBytes((int)dwlen);
if (Datatmp.Length == dwlen)
{
dwcpsize = (uint)Datatmp.Length;
dwcpsize = dwcpsize < dwlen ? dwcpsize : dwlen;
//lpdata->m_lpframeinfo = (LPRAWFRAME_INFO)lpdata->m_lpframedata;
//CopyMemory(lpframeinfo, lpdata->m_lpframedata, sizeof(RAWFRAME_INFO));
int size = Marshal.SizeOf(typeof(ggFrame_struct));
Marshal.FreeHGlobal(buffer); // 释放以前分配的空间
buffer = Marshal.AllocHGlobal(size); // 重新分配
Marshal.Copy(Datatmp, 0, buffer, size);
FrameInfoBuff = (ggFrame_struct)Marshal.PtrToStructure(buffer, typeof(ggFrame_struct));
if (FrameInfoBuff.cb_size >= size)
{
FrameRawBuff = new byte[FrameInfoBuff.rawdatasize];
Array.Copy(Datatmp, FrameInfoBuff.rawdataoffset, FrameRawBuff, 0, FrameInfoBuff.rawdatasize);
//FrameImageBuff = new byte[FrameInfoBuff.videoimagesize];
//Array.Copy(Datatmp, FrameInfoBuff.imageoffset, FrameImageBuff, 0, FrameInfoBuff.videoimagesize);
}
else
{
FrameImageBuff = null;
}
sErrString = "成功";
bResult = true;
}
else
{
sErrString = "数据帧长度错误";
bResult = false;
}
}
catch (Exception ex)
{
sErrString = "读帧数据错误(" + ex.Message + ")";
bResult = false;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
return bResult;
}
public bool ReadAllFramDataFromFile(ref byte[] AllFrameInfo, ref byte[] AllRaw, ref double[,] BeltData, double Angle0, UserControl_ReadFileBar RBar, ref string sErrString)
{
bool bResult = false;
sErrString = "";
double dBarStep = RBar.iBarValue;
if (ggData.m_szFileName == "") // 还没关联gg文件
{
sErrString = "尚未打开相关文件。";
return bResult;
}
if (ggData.numberof_frames <= 0)
{
AllRaw = null; // 没有数据
BeltData = null;
return bResult;
}
dBarStep += 5.0;
RBar.iBarValue = (int)(dBarStep);
MyTools.DoEvents();
double Angle = 2 * PI_360 * Angle0 / 360;
BeltData = null;
IntPtr buffer = Marshal.AllocHGlobal(1);
try
{
int iFrameInfoSize = Marshal.SizeOf(typeof(ggFrame_struct));
AllFrameInfo = new byte[ggData.numberof_frames * iFrameInfoSize];
mod_ReadGen2.Gen2_Struct Gen2Current = new mod_ReadGen2.Gen2_Struct();
//Gen2Array = new mod_ReadGen2.Gen2_Struct[ggData.numberof_frames];
mod_ShowPict PB = new mod_ShowPict();
byte[] FirstLine = new byte[ggData.numberof_frames * ggData.numberofpointofline * 2];
byte[] SecondLine = new byte[ggData.numberof_frames * ggData.numberofpointofline * 2];
int iFirstLine = 0, iSeconLine = 0; // 取出第几线
iFirstLine = (int)((ggData.numberofline / (PI_360 * 2)) * (Angle + ggData.Angle2));
if (iFirstLine > ggData.numberofline)
iFirstLine -= ggData.numberofline;
iSeconLine = iFirstLine + ggData.numberofline / 2;
if (iSeconLine > ggData.numberofline)
iSeconLine -= ggData.numberofline;
for (int i = 0; i < ggData.numberof_frames; i++)
{
if (ggFileMoveToFrame("Goto", i, ref sErrString))
{
// 计算从第几帧取数据
if (ReadRawFrameDataFromFile(ref ggData.m_ggframe_info, ref ggData.m_lprawdata, ref ggData.m_lpimagedata, ref sErrString))
{
if (i == 0)
{
ggData.m_sizeofrawdata = ggData.m_ggframe_info.rawdatasize;
AllRaw = new byte[ggData.numberof_frames * ggData.m_sizeofrawdata];
}
Marshal.FreeHGlobal(buffer); // 释放以前分配的空间
buffer = Marshal.AllocHGlobal(iFrameInfoSize); // 重新分配
Marshal.StructureToPtr((ggFrame_struct)ggData.m_ggframe_info, buffer, false);
Marshal.Copy(buffer, AllFrameInfo, i * iFrameInfoSize, iFrameInfoSize);
Array.Copy(ggData.m_lprawdata, 0, AllRaw, i * ggData.m_sizeofrawdata, ggData.m_sizeofrawdata);
//Gen2Current = new mod_ReadGen2.Gen2_Struct();
//if (mod_ReadGen2.ReadGen2FromGGClass(this, ref Gen2Current, ref sErrString) != MyData.iErr_Succ)
//{
// MyTools.ShowMsg("读取数据失败!", "从gg类读数据失败 = " + sErrString);
// goto Eend;
//}
//PB.pGen2 = Gen2Current;
//PB.Gen2ToData(); // 转变成 PictData
//Gen2Array[i] = PB.pGen2;
Array.Copy(ggData.m_lprawdata, iFirstLine * ggData.numberofpointofline * 2, FirstLine, i * ggData.numberofpointofline * 2, ggData.numberofpointofline * 2); // To First Line
Array.Copy(ggData.m_lprawdata, iSeconLine * ggData.numberofpointofline * 2, SecondLine, i * ggData.numberofpointofline * 2, ggData.numberofpointofline * 2); // To First Line
dBarStep += (double)(85.0 / ggData.numberof_frames);
RBar.iBarValue = (int)(dBarStep);
MyTools.DoEvents();
}
else
{
sErrString = "移动到第" + i.ToString() + "帧时出错";
goto Eend;
}
}
else
{
sErrString = "移动到第" + i.ToString() + "帧时出错";
goto Eend;
}
}
BeltData = new double[ggData.numberof_frames * 2, ggData.numberofpointofline];
ushort uSi = 0;
ushort uMax = 0, uMin = 0;
for (int i = 0; i < ggData.numberof_frames; i++)
{
for (int j = 0; j < ggData.numberofpointofline; j++)
{
uSi = BitConverter.ToUInt16(FirstLine, i * ggData.numberofpointofline * 2 + j * 2);
BeltData[i, j] = (double)uSi;
if (uMin > uSi)
uMin = uSi;
if (uMax < uSi)
uMax = uSi;
uSi = BitConverter.ToUInt16(SecondLine, i * ggData.numberofpointofline * 2 + j * 2);
BeltData[ggData.numberof_frames + i, j] = (double)uSi;
if (uMin > uSi)
uMin = uSi;
if (uMax < uSi)
uMax = uSi;
}
}
for (int i = 0; i < ggData.numberof_frames; i++)
{
for (int j = 0; j < ggData.numberofpointofline; j++)
{
//BeltData[i, j] = (BeltData[i, j] / 65535.0) * (uMax - uMin) + uMin;
//BeltData[ggData.numberof_frames + i, j] = (BeltData[ggData.numberof_frames + i, j] / 65535.0) * (uMax - uMin) + uMin;
BeltData[i, j] = BeltData[i, j] * 255 / 65535.0;
BeltData[ggData.numberof_frames + i, j] = BeltData[ggData.numberof_frames + i, j] * 255 / 65535.0;
}
}
bResult = true;
sErrString = "";
}
catch (OutOfMemoryException)
{
sErrString = "内存不足";
}
catch (Exception ex)
{
sErrString = ex.Message;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
Eend:
return bResult;
}
public bool ReadFramDataFromMemo(int iFrameID, ref ggFrame_struct FrameInfoBuff, ref byte[] FrameRawBuff, ref byte[] FrameImageBuff, ref string sErrString)
{
bool bResult = false;
IntPtr buffer = Marshal.AllocHGlobal(1);
if (ggData.m_szFileName == "") // 还没关联gg文件
{
sErrString = "尚未打开相关文件。";
return bResult;
}
if (ggData.numberof_frames <= 0)
{
FrameInfoBuff = new ggFrame_struct(); // 没有数据
FrameRawBuff = null;
return bResult;
}
try
{
int size = Marshal.SizeOf(typeof(ggFrame_struct));
Marshal.FreeHGlobal(buffer); // 释放以前分配的空间
buffer = Marshal.AllocHGlobal(size); // 重新分配
Marshal.Copy(ggAllFrameInfo, iFrameID * size, buffer, size);
FrameInfoBuff = (ggFrame_struct)Marshal.PtrToStructure(buffer, typeof(ggFrame_struct));
Array.Copy(ggAllRawData, iFrameID * ggData.m_sizeofrawdata, FrameRawBuff, 0, ggData.m_sizeofrawdata);
bResult = true;
}
catch (Exception ex)
{
sErrString = "读取记录数据失败: " + ex.Message;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
return bResult;
}
public bool ReadRecordBeltData(byte[] AllRaw, ref double[,] BeltData, double Angle0, ref string sErrString)
{
bool bResult = false;
sErrString = "";
BeltData = null;
if (AllRaw == null ) // 还没关联gg文件
{
sErrString = "尚未打开相关文件。";
return bResult;
}
if (ggData.numberof_frames <= 0)
{
return bResult;
}
double Angle = Angle0;
try
{
byte[] FirstLine = new byte[ggData.numberof_frames * ggData.numberofpointofline * 2];
byte[] SecondLine = new byte[ggData.numberof_frames * ggData.numberofpointofline * 2];
int iFirstLine = 0, iSeconLine = 0; // 取出第几线
iFirstLine = (int)( (ggData.numberofline /PI_360) * (Angle + ggData.Angle2) );
if (iFirstLine > ggData.numberofline)
iFirstLine -= ggData.numberofline;
iSeconLine = iFirstLine + ggData.numberofline / 2;
if (iSeconLine > ggData.numberofline)
iSeconLine -= ggData.numberofline;
int iL = 0;
for (int i = 0; i < ggData.numberof_frames; i++)
{
// 计算从第几帧取数据
if (i == ggData.numberof_frames - 2)
iL = 1;
Array.Copy(AllRaw, i * ggData.m_sizeofrawdata + iFirstLine * ggData.numberofpointofline * 2, FirstLine, i * ggData.numberofpointofline * 2, ggData.numberofpointofline * 2); // To First Line
Array.Copy(AllRaw, i * ggData.m_sizeofrawdata + iSeconLine * ggData.numberofpointofline * 2, SecondLine, i * ggData.numberofpointofline * 2, ggData.numberofpointofline * 2); // To First Line
}
BeltData = new double[ggData.numberof_frames * 2, ggData.numberofpointofline];
ushort uSi = 0;
ushort uMax = 0, uMin = 0;
for (int i = 0; i < ggData.numberof_frames; i++)
{
for (int j = 0; j < ggData.numberofpointofline; j++)
{
uSi = BitConverter.ToUInt16(FirstLine, i * ggData.numberofpointofline * 2 + j * 2);
BeltData[i, j] = (double)uSi;
if (uMin > uSi)
uMin = uSi;
if (uMax < uSi)
uMax = uSi;
uSi = BitConverter.ToUInt16(SecondLine, i * ggData.numberofpointofline * 2 + j * 2);
BeltData[ggData.numberof_frames + i, j] = (double)uSi;
if (uMin > uSi)
uMin = uSi;
if (uMax < uSi)
uMax = uSi;
}
}
for (int i = 0; i < ggData.numberof_frames; i++)
{
for (int j = 0; j < ggData.numberofpointofline; j++)
{
//BeltData[i, j] = (BeltData[i, j] / 65535.0) * (uMax - uMin) + uMin;
//BeltData[ggData.numberof_frames + i, j] = (BeltData[ggData.numberof_frames + i, j] / 65535.0) * (uMax - uMin) + uMin;
BeltData[i, j] = BeltData[i, j] * 255 / 65535.0;
BeltData[ggData.numberof_frames + i, j] = BeltData[ggData.numberof_frames + i, j] * 255 / 65535.0;
}
}
bResult = true;
sErrString = "";
}
catch (Exception ex)
{
sErrString = "读取记录带数据失败: " + ex.Message;
}
Eend:
return bResult;
}
public bool ReadMarkFromDB(int iFileID, int iRecordID, ref string[,] Marks, ref string sErrString)
{
bool bResult = false;
string sSql = "";
DataSet DS = null;
sErrString = "";
Marks = new string[ggData.numberof_frames, 4];
for (int i = 0; i < ggData.numberof_frames; i++)
{
Marks[i, 0] = "False"; // 有无
Marks[i, 1] = i.ToString(); // FrameID
Marks[i, 2] = "-1"; // MarkID
Marks[i, 3] = ""; // MarkInfo
}
try
{
sSql = "Select * From Mark Where FileID = " + iFileID.ToString() + " AND RecordID = " + iRecordID;
if (!MyData.MySqlite.ReadData(sSql, ref DS, ref sErrString))
{
goto Eend;
}
if (DS != null && DS.Tables[0].Rows.Count > 0)
{
int iFrameID = 0;
for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
{
iFrameID = int.Parse(DS.Tables[0].Rows[i]["FRAMEID"].ToString());
Marks[iFrameID, 0] = "True";
Marks[iFrameID, 1] = iFrameID.ToString();
Marks[iFrameID, 2] = DS.Tables[0].Rows[i]["MARKID"].ToString();
Marks[iFrameID, 3] = DS.Tables[0].Rows[i]["MARK"].ToString();
}
}
sErrString = "";
bResult = true;
}
catch (Exception ex)
{
sErrString = ex.Message;
}
Eend:
if (DS != null)
DS = null;
return bResult;
}
/// <summary>
/// 从已经打开的序列文件中,移动到指定的帧的起始位置 --------------------------------------
/// iFrameID = 指定的帧编号
/// MoveMode:移动方式, Top,Bottom, Last,Next,Goto
/// iFrameID:当MoveMode == Goto时, iFrameID >= 0, 等于具体那一帧=0--Count-1
/// 当MoveMode == 其他值时, iFrameID没有意义
/// ------------------------------------------------------------------------------------------
/// </summary>
public bool ggFileMoveToFrame(string MoveMode, int iFrameID, ref string sErrString)
{
int dwlen = 0;
int iMoveFramNum = 0;
uint iCurrentFramID = ggData.m_currpos;
long dwcode;
long lpos;
bool bResult = false;
sErrString = "";
if (MoveMode != "Top" && MoveMode != "Bottom" && MoveMode != "Last" && MoveMode != "Next" && MoveMode != "Goto")
{
sErrString = "参数错误(ggFileMoveToFrame-->MoveMode = '" + MoveMode + "')";
return false;
}
try
{
if (MoveMode == "Top")
{
iMoveFramNum = 0;
}
else if (MoveMode == "Bottom")
{
iMoveFramNum = ggData.m_ggheader.numberof_frames - 1;
}
else if (MoveMode == "Last")
{
if (ggData.m_currpos > 0)
iMoveFramNum = (int)ggData.m_currpos - 1;
else
{
sErrString = "已经移动到第一帧。";
return false;
}
}
else if (MoveMode == "Next")
{
if (ggData.m_currpos < (int)ggData.m_currpos - 1)
iMoveFramNum = (int)ggData.m_currpos + 1;
else
{
sErrString = "已经移动到最后帧。";
return false;
}
}
else if (MoveMode == "Goto")
{
if (iFrameID < 0)
{
sErrString = "移动帧数不能为负数。";
return false;
}
else if (iFrameID > (ggData.m_ggheader.numberof_frames -1 ) )
{
sErrString = "移动帧数不能超过总帧数。";
return false;
}
iMoveFramNum = iFrameID;
}
// else 只能有上面5种情况
// 将文件的读写位置移动到 “第一帧 + 指定帧” 的开始位置
dwlen = (int)ggData.m_ggheader.cbsize_frame; //一帧的数据的数据长度
lpos = ggData.m_ggheader.offsetframe; // 第一帧其实位置
lpos += dwlen * iMoveFramNum; // 移动多少个帧,实际需要移动多少个数据长度
dwcode = br.BaseStream.Seek(lpos, SeekOrigin.Begin);
if (dwcode != lpos)
{
sErrString = "图像不能移动到指定位置。";
bResult = false;
}
else
{
ggData.m_currpos = (uint)iMoveFramNum;
bResult = true;
}
}
catch (Exception ex)
{
sErrString = "移动帧数出现意外错误(" + ex.Message + ")";
bResult = false;
}
return bResult;
}
static int ReadFileString(BinaryReader br, int DataLen, ref string ItemString, ref string ErrString)
{
byte[] FileData = new byte[DataLen];
int iResult = MyData.iErr_UnknowErr;
ItemString = "";
try
{
FileData = br.ReadBytes(DataLen);
ItemString = System.Text.Encoding.Default.GetString(FileData, 0, DataLen).TrimEnd('\0');
iResult = MyData.iErr_Succ;
}
catch (Exception ex)
{
ErrString = "Read string from gg exception = " + ex.Message;
iResult = MyData.iErr_Exception;
}
return iResult;
}
static int ReadFileInt(BinaryReader br, ref int ItemInt, ref string ErrString)
{
int iFileData = 0;
int iResult = MyData.iErr_UnknowErr;
try
{
iFileData = br.ReadInt32();
ItemInt = iFileData;
iResult = MyData.iErr_Succ;
}
catch (Exception ex)
{
ErrString = "Read int from gg exception = " + ex.Message;
iResult = MyData.iErr_Exception;
}
return iResult;
}
static int ReadFileFloat(BinaryReader br, ref float ItemFloat, ref string ErrString)
{
float fFileData = 0;
int iResult = MyData.iErr_UnknowErr;
try
{
fFileData = br.ReadSingle();
ItemFloat = fFileData;
iResult = MyData.iErr_Succ;
}
catch (Exception ex)
{
ErrString = "Read int from gg exception = " + ex.Message;
iResult = MyData.iErr_Exception;
}
return iResult;
}
}
}