source: trunk/Cosy/videodev/Camera.cc@ 12789

Last change on this file since 12789 was 8869, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 4.8 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 "MLog.h"
27#include "MLogManip.h"
28
29#include "MVideo.h"
30#include "PixClient.h"
31
32//ClassImp(Camera);
33
34using namespace std;
35
36Camera::Camera(PixClient &client, Int_t nch) : MThread("Camera"), fClient(client), fVideo(0), fNumFrame(0), fChannel(nch)
37{
38 fVideo = new MVideo;
39 fVideo->Open(nch);
40
41 RunThread();
42}
43
44Camera::~Camera()
45{
46 // Shut down the thread
47 CancelThread();
48
49 // Now delete (close) the device
50 delete fVideo;
51}
52
53void Camera::ProcessFrame(unsigned char *img)
54{
55 gettimeofday(&fTime, NULL);
56
57 const Int_t W = fVideo->GetWidth();
58 const Int_t H = fVideo->GetHeight();
59/*
60 const Bool_t crop = W!=768 || H!=576;
61
62 // FIXME: This only works for one RGB24
63 if (!crop)
64 {
65 // FIXME: Grey scale assumed!
66 const unsigned char *end = img + 768*576*depth;
67 char *beg = fImg;
68 while (img < end)
69 {
70 *beg++ = *img;
71 img += depth;
72 }
73 }
74 else
75 */
76 {
77 const Int_t w = TMath::Min(W, 768);
78 const Int_t h = TMath::Min(H, 576);
79
80 memset(fImg, 0, 768*576);
81 for (int y=0; y<h; y++)
82 for (int x=0; x<w; x++)
83 {
84 const Int_t p = (x+y*W)*depth;
85 fImg[x+y*768] = ((UInt_t)img[p]+(UInt_t)img[p+1]+(UInt_t)img[p+2])/3;
86 }
87 }
88
89 fClient.ProcessFrame(fNumFrame-1, (byte*)fImg, &fTime);
90}
91
92Int_t Camera::Thread()
93{
94 fNumSkipped = 0;
95 fNumFrame = 0;
96
97 if (!fVideo->IsOpen())
98 {
99 gLog << err << "Camera::Thread: ERROR - Device not open." << endl;
100 return kFALSE;
101 }
102
103 gLog << dbg << "Start Camera::Thread at frame " << fNumFrame%fVideo->GetNumBuffers() << endl;
104
105 for (int f=0; f<fVideo->GetNumBuffers(); f++)
106 if (!fVideo->CaptureStart(f))
107 return kFALSE;
108
109 Int_t timeouts = 0;
110 while (1)
111 {
112 // Switch channel if necessary
113 switch (fVideo->SetChannel(fChannel))
114 {
115 case kFALSE: // Error swucthing channel
116 return kFALSE;
117 case kSKIP: // No channel switching necessary
118 break;
119 case kTRUE: // Channel switched (skip filled buffers)
120 for (int f=0; f<fVideo->GetNumBuffers(); f++)
121 if (!fVideo->CaptureWait(fNumFrame+f))
122 return kFALSE;
123 fNumFrame=0;
124 for (int f=0; f<fVideo->GetNumBuffers(); f++)
125 if (!fVideo->CaptureStart(f))
126 return kFALSE;
127 break;
128 }
129
130 // Check and wait until capture into the next buffer is finshed
131 unsigned char *img = 0;
132 switch (fVideo->CaptureWait(fNumFrame++, &img))
133 {
134 case kTRUE: // Process frame
135 // If cacellation is requested cancel here
136 TThread::CancelPoint();
137
138 ProcessFrame(img);
139
140 // If cacellation is requested cancel here
141 TThread::CancelPoint();
142
143 // Start to capture into the buffer which has just been processed
144 if (!fVideo->CaptureStart(fNumFrame-1))
145 break;
146
147 timeouts = 0;
148 continue;
149
150 case kFALSE: // Waiting failed
151 break;
152
153 case kSKIP: // Skip frame
154 fNumFrame--;
155 fNumSkipped++;
156 if (timeouts++<5)
157 continue;
158
159 cout << "ERROR - At least five captured images timed out." << endl;
160 break;
161 }
162
163 break;
164 }
165
166 // Is this necessary?!?
167 //for (int i=0; i<frames-1; i++)
168 // video.CaptureWait((f+i+1)%frames);
169
170 cout << fNumFrame-1 << " frames processed." << endl;
171 cout << fNumSkipped << " frames skipped." << endl;
172
173 return kTRUE;
174}
175
176//void Camera::Loop(unsigned long nof)
177//{
178//}
179
180void Camera::SetChannel(int chan)
181{
182 fChannel = chan;
183// CancelThread();
184// fVideo->SetChannel(chan);
185// RunThread();
186}
187
Note: See TracBrowser for help on using the repository browser.