source: trunk/FACT++/drive/Camera.cc@ 20115

Last change on this file since 20115 was 18626, checked in by tbretz, 8 years ago
Removed base class PixGetter, use unsigned char as base type for image and a depth of 4 byte, disentengle from root were possible, some minor cleanup
File size: 4.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 1/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24#include "Camera.h"
25
26#include <iostream>
27
28#include "MVideo.h"
29#include "PixClient.h"
30
31using namespace std;
32
33Camera::Camera(PixClient &client, Int_t nch) : MThread("Camera"), fClient(client), fVideo(0), fNumFrame(0), fChannel(nch)
34{
35 fVideo = new MVideo;
36 fVideo->Open(nch);
37
38 RunThread();
39}
40
41Camera::~Camera()
42{
43 // Shut down the thread
44 CancelThread();
45
46 // Now delete (close) the device
47 delete fVideo;
48}
49
50void Camera::ProcessFrame(unsigned char *img)
51{
52 gettimeofday(&fTime, NULL);
53
54#if 1
55 for (int y=0; y<576; y++)
56 for (int x=0; x<768; x++)
57 {
58 const Int_t p = (x+y*768)*4;
59 fImg[x+y*768] = ((UInt_t)img[p+1]+(UInt_t)img[p+2]+(UInt_t)img[p+3])/3;
60 }
61#endif
62
63#if 0
64 unsigned char *dest = fImg;
65 for (const unsigned char *ptr=img; ptr<img+768*576*4; ptr+=4)
66 *dest++ = (UShort_t(ptr[1])+UShort_t(ptr[2])+UShort_t(ptr[3]))/3;
67#endif
68
69 fClient.ProcessFrame(fNumFrame-1, (byte*)fImg, &fTime);
70}
71
72Int_t Camera::Thread()
73{
74 fNumSkipped = 0;
75 fNumFrame = 0;
76
77 if (!fVideo->IsOpen())
78 {
79 cout << "Camera::Thread: ERROR - Device not open." << endl;
80 return kFALSE;
81 }
82
83 cout << "Start Camera::Thread at frame " << fNumFrame%fVideo->GetNumBuffers() << endl;
84
85 for (int f=0; f<fVideo->GetNumBuffers(); f++)
86 {
87 if (!fVideo->CaptureStart(f))
88 return kFALSE;
89 }
90
91 if (!fVideo->Start())
92 return kFALSE;
93
94 Int_t timeouts = 0;
95 while (1)
96 {
97
98 /*
99 // Switch channel if necessary
100 switch (fVideo->SetChannel(fChannel))
101 {
102 case kFALSE: // Error swucthing channel
103 return kFALSE;
104 case kSKIP: // No channel switching necessary
105 break;
106 case kTRUE: // Channel switched (skip filled buffers)
107 for (int f=0; f<fVideo->GetNumBuffers(); f++)
108 if (!fVideo->CaptureWait(fNumFrame+f))
109 return kFALSE;
110 fNumFrame=0;
111 for (int f=0; f<fVideo->GetNumBuffers(); f++)
112 if (!fVideo->CaptureStart(f))
113 return kFALSE;
114 break;
115 }*/
116
117 //cout << "*** Wait " << fNumFrame << endl;
118
119 // Check and wait until capture into the next buffer is finshed
120 unsigned char *img = 0;
121 switch (fVideo->CaptureWait(fNumFrame, &img))
122 {
123 case kTRUE: // Process frame
124 // If cacellation is requested cancel here
125 TThread::CancelPoint();
126
127 ProcessFrame(img);
128
129 // If cacellation is requested cancel here
130 TThread::CancelPoint();
131
132 // Start to capture into the buffer which has just been processed
133 if (!fVideo->CaptureStart(fNumFrame-1))
134 break;
135
136 fNumFrame++;
137 timeouts = 0;
138 continue;
139
140 case kFALSE: // Waiting failed
141 break;
142
143 case -1: // Skip frame
144 usleep(10000); // Wait half a frame
145 continue;
146
147 fNumFrame--;
148 fNumSkipped++;
149 if (timeouts++<5)
150 continue;
151
152 cout << "ERROR - At least five captured images timed out." << endl;
153 break;
154 }
155
156 break;
157 }
158
159 // Is this necessary?!?
160 //for (int i=0; i<frames-1; i++)
161 // video.CaptureWait((f+i+1)%frames);
162
163 cout << fNumFrame-1 << " frames processed." << endl;
164 cout << fNumSkipped << " frames skipped." << endl;
165
166 return kTRUE;
167}
168
169//void Camera::Loop(unsigned long nof)
170//{
171//}
172
173void Camera::SetChannel(int chan)
174{
175 fChannel = chan;
176// CancelThread();
177// fVideo->SetChannel(chan);
178// RunThread();
179}
180
Note: See TracBrowser for help on using the repository browser.