source: trunk/MagicSoft/Cosy/videodev/Camera.cc@ 8847

Last change on this file since 8847 was 8843, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 4.1 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)
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::ExitLoop()
54{
55 CancelThread();
56
57 // Some information
58 //cout << fNumFrame-1 << "frames processed." << endl;
59 //cout << fNumSkipped << "frames skipped." << endl;
60}
61
62void Camera::ProcessFrame(char *img)
63{
64 gettimeofday(&fTime, NULL);
65
66 const Int_t W = fVideo->GetWidth();
67 const Int_t H = fVideo->GetHeight();
68
69 const Bool_t crop = W!=768 || H!=576;
70
71 // FIXME: This only works for one RGB24
72 if (!crop)
73 {
74 // FIXME: Grey scale assumed!
75 const char *end = img + 768*576*depth;
76 char *beg = fImg;
77 while (img < end)
78 {
79 *beg++ = *img;
80 img += depth;
81 }
82 }
83 else
84 {
85 const Int_t w = TMath::Min(W, 768);
86 const Int_t h = TMath::Min(H, 576);
87
88 memset(fImg, 0, 768*576);
89 for (int y=0; y<h; y++)
90 for (int x=0; x<w; x++)
91 fImg[x+y*768] = ((UInt_t)(UChar_t)img[(x+y*W)*3]+(UInt_t)(UChar_t)img[(x+y*fVideo->GetWidth())*3+1]+(UInt_t)(UChar_t)img[(x+y*fVideo->GetWidth())*3+2])/3;
92 }
93
94 fClient.ProcessFrame(fNumFrame-1, (byte*)fImg, &fTime);
95}
96
97Int_t Camera::Thread()
98{
99 fNumSkipped = 0;
100 fNumFrame = 1;
101
102 if (!fVideo->IsOpen())
103 {
104 gLog << "ERROR - Process: Device not open." << endl;
105 return kFALSE;
106 }
107
108 //Print();
109
110 for (int f=0; f<fVideo->GetNumBuffers()-1; f++)
111 if (!fVideo->CaptureStart(f))
112 return kFALSE;
113
114 Int_t timeouts = 0;
115
116 while (1)
117 {
118 // If cacellation is requested cancel here
119 TThread::CancelPoint();
120
121 // Start to capture into the buffer which has just been processed
122 if (!fVideo->CaptureStart(fNumFrame))
123 break;
124
125 // If cacellation is requested cancel here
126 TThread::CancelPoint();
127
128 // Check and wait until capture into the next buffer is finshed
129 char *img = 0;
130 switch (fVideo->CaptureWait(++fNumFrame, &img))
131 {
132 case kTRUE: // Process frame
133 ProcessFrame(img);
134 timeouts = 0;
135 continue;
136
137 case kFALSE: // Waiting failed
138 break;
139
140 case kSKIP: // Skip frame
141 fNumFrame--;
142 fNumSkipped++;
143 if (timeouts++<5)
144 continue;
145
146 cout << "ERROR - At least five captured images timed out." << endl;
147 break;
148 }
149
150 break;
151 }
152
153 // Is this necessary?!?
154 //for (int i=0; i<frames-1; i++)
155 // video.CaptureWait((f+i+1)%frames);
156
157 cout << fNumFrame-1 << " frames processed." << endl;
158 cout << fNumSkipped << " frames skipped." << endl;
159
160 return kTRUE;
161}
162
163void Camera::Loop(unsigned long nof)
164{
165}
166
167void Camera::SetChannel(int chan)
168{
169 CancelThread();
170 fVideo->SetChannel(chan);
171 RunThread();
172}
173
Note: See TracBrowser for help on using the repository browser.