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

Last change on this file since 8859 was 8859, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 3.9 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::ProcessFrame(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 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 const Int_t w = TMath::Min(W, 768);
77 const Int_t h = TMath::Min(H, 576);
78
79 memset(fImg, 0, 768*576);
80 for (int y=0; y<h; y++)
81 for (int x=0; x<w; x++)
82 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;
83 }
84
85 fClient.ProcessFrame(fNumFrame-1, (byte*)fImg, &fTime);
86}
87
88Int_t Camera::Thread()
89{
90 fNumSkipped = 0;
91 fNumFrame = 0;
92
93 if (!fVideo->IsOpen())
94 {
95 gLog << err << "Camera::Thread: ERROR - Device not open." << endl;
96 return kFALSE;
97 }
98
99 //Print();
100
101 for (int f=1; f<fVideo->GetNumBuffers(); f++)
102 if (!fVideo->CaptureStart(f))
103 return kFALSE;
104
105 Int_t timeouts = 0;
106
107 while (1)
108 {
109 // If cacellation is requested cancel here
110 TThread::CancelPoint();
111
112 // Start to capture into the buffer which has just been processed
113 if (!fVideo->CaptureStart(fNumFrame))
114 break;
115
116 // If cacellation is requested cancel here
117 TThread::CancelPoint();
118
119 // Check and wait until capture into the next buffer is finshed
120 char *img = 0;
121 switch (fVideo->CaptureWait(++fNumFrame, &img))
122 {
123 case kTRUE: // Process frame
124 ProcessFrame(img);
125 timeouts = 0;
126 continue;
127
128 case kFALSE: // Waiting failed
129 break;
130
131 case kSKIP: // Skip frame
132 fNumFrame--;
133 fNumSkipped++;
134 if (timeouts++<5)
135 continue;
136
137 cout << "ERROR - At least five captured images timed out." << endl;
138 break;
139 }
140
141 break;
142 }
143
144 // Is this necessary?!?
145 //for (int i=0; i<frames-1; i++)
146 // video.CaptureWait((f+i+1)%frames);
147
148 cout << fNumFrame-1 << " frames processed." << endl;
149 cout << fNumSkipped << " frames skipped." << endl;
150
151 return kTRUE;
152}
153
154//void Camera::Loop(unsigned long nof)
155//{
156//}
157
158void Camera::SetChannel(int chan)
159{
160 CancelThread();
161 fVideo->SetChannel(chan);
162 RunThread();
163}
164
Note: See TracBrowser for help on using the repository browser.