FACT++  1.0
Camera.cc
Go to the documentation of this file.
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 
31 using namespace std;
32 
33 Camera::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 
42 {
43  // Shut down the thread
44  CancelThread();
45 
46  // Now delete (close) the device
47  delete fVideo;
48 }
49 
50 void 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 
70 }
71 
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 
173 void Camera::SetChannel(int chan)
174 {
175  fChannel = chan;
176 // CancelThread();
177 // fVideo->SetChannel(chan);
178 // RunThread();
179 }
180 
UInt_t fNumSkipped
Definition: Camera.h:33
Definition: MThread.h:8
virtual ~Camera()
Definition: Camera.cc:41
Int_t CancelThread(void **ret=0)
Definition: MThread.h:48
STL namespace.
struct timeval fTime
Definition: Camera.h:26
Bool_t IsOpen() const
Definition: MVideo.h:105
Int_t Thread()
Definition: Camera.cc:72
Bool_t Open(Int_t channel=0)
Definition: MVideo.cc:486
UInt_t fNumFrame
Definition: Camera.h:32
PixClient & fClient
Definition: Camera.h:28
UInt_t fChannel
Definition: Camera.h:35
Int_t CaptureWait(unsigned int frame, unsigned char **ptr=0) const
Definition: MVideo.cc:782
void ProcessFrame(unsigned char *img)
Definition: Camera.cc:50
unsigned char byte
Definition: MGImage.h:17
Int_t GetNumBuffers() const
Definition: MVideo.cc:878
MVideo * fVideo
Definition: Camera.h:30
Bool_t CaptureStart(unsigned int frame) const
Definition: MVideo.cc:736
Bool_t Start()
Definition: MVideo.cc:677
unsigned char fImg[cols *rows]
Definition: Camera.h:25
Camera(PixClient &client, Int_t ch=0)
Definition: Camera.cc:33
Definition: MVideo.h:49
void SetChannel(int)
Definition: Camera.cc:173
Int_t RunThread(void *arg=0)
Definition: MThread.h:43
virtual void ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
Definition: PixClient.cc:7