Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoShMFeeder.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 // for gcc 4.7
26 #ifndef WIN32
27 #include <unistd.h>
28 #endif
29 
30 #include "VideoShMFeeder.hh"
31 #include <Base/Common/BIASpragma.hh>
32 
33 
34 
35 using namespace BIAS;
36 using namespace std;
37 
39 {
40 #ifdef WIN32
41  ShmContext_ = NULL;
42  Mapping_ = NULL;
43 #else
44  ShmContext_ = 0;
45 #endif
46  MemoryLengthTotal_ = 0;
47  DataLength_ = 0;
48  Shm_ = NULL;
49  Header_ = NULL;
50  Data_ = NULL;
51  bIsInitialized_ = false;
52 #ifdef WIN32
53  FileName_ = "/VideoSource_Shm.shm";
54 #else
55  FileName_ = "VideoSource_Shm.shm";
56 #endif
57 }
58 
59 
60 
61 VideoShMFeeder::VideoShMFeeder(const string &filename)
62 {
63 #ifdef WIN32
64  ShmContext_ = NULL;
65  Mapping_ = NULL;
66 #else
67  ShmContext_ = 0;
68 #endif
69  MemoryLengthTotal_ = 0;
70  DataLength_ = 0;
71  Shm_ = NULL;
72  Header_ = NULL;
73  Data_ = NULL;
74  bIsInitialized_ = false;
75  FileName_ = filename;
76 }
77 
78 
80 {
81  bIsInitialized_ = false;
82 #ifdef WIN32
83  UnmapViewOfFile(Shm_);
84  CloseHandle(Mapping_ );
85  DeleteFile(FileName_.c_str());
86 #else
87  shm_unlink(FileName_.c_str());
88 #endif
89 }
90 
92 {
93  MemoryLengthTotal_ = DataLength_ + sizeof(VideoSource_Shm::ShmImageHeader);
94 
95 #ifdef WIN32
96  SYSTEM_INFO sSysInfo;
97  GetSystemInfo( &sSysInfo );
98  Granularity_ = sSysInfo.dwAllocationGranularity;
99  while( long(MemoryLengthTotal_) > Granularity_ )
100  Granularity_ += sSysInfo.dwAllocationGranularity;
101  MemoryLengthTotal_ = Granularity_;
102 
103  //check if file exists
104  if( Mapping_ == NULL )
105  Mapping_ = OpenFileMapping( FILE_MAP_ALL_ACCESS,FALSE,FileName_.c_str());
106 
107  if(Mapping_ == NULL)
108  {
109  HANDLE File_ = CreateFile(FileName_.c_str(),GENERIC_WRITE
110  | GENERIC_READ,
111  FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
112  FILE_ATTRIBUTE_TEMPORARY,NULL );
113  //create a new filemapping
114  Mapping_= CreateFileMapping(File_, // use paging file
115  NULL, // default security
116  PAGE_READWRITE, // read/write access
117  0, // max. object size
118  // buffer size
119  Granularity_ + MemoryLengthTotal_,
120  FileName_.c_str()); // name of mapping object
121  }
122  if (Mapping_ == NULL) {
123  BIASERR("Failed to memory map file");
124  return false;
125  };
126 
127  Shm_= (unsigned char*)MapViewOfFile(Mapping_,FILE_MAP_ALL_ACCESS,0,0,0);
128 
129  if (Shm_ == NULL) {
130  BIASERR("Failed to memory map file");
131  return false;
132  };
133 
134 #else // win32
135  ShmContext_ = shm_open(FileName_.c_str(),O_RDWR,S_IRWXU| S_IRWXG| S_IRWXO);
136  if (ShmContext_ <0) {
137  // cout <<"Creating new context"<<endl;
138  ShmContext_ = shm_open(FileName_.c_str(),O_RDWR| O_CREAT,
139  S_IRWXU| S_IRWXG| S_IRWXO);
140  }
141  // else cout <<"Using old context"<<endl;
142 
143 
144  if (ShmContext_ <0) {
145  BIASERR("Unable to create VideoSource_Shm.shm");
146  return false;
147  }
148  int res;
149  res = ftruncate(ShmContext_, MemoryLengthTotal_);
150  if (res <0) {
151  perror(FileName_.c_str());
152  return -1;
153  }
154 
155  Shm_ = mmap(0,MemoryLengthTotal_, PROT_WRITE,MAP_SHARED, ShmContext_, 0 );
156  if (Shm_ == MAP_FAILED) {
157  BIASERR("Unable to create memory-mapping!!!");
158  return false;
159  }
160 #endif
161 
162  //pointer to header
163  Header_ = (VideoSource_Shm::ShmImageHeader*)Shm_;
164  Header_->Time_ = 0;
165  Header_->AxisX_ = Header_->AxisY_ = Header_->AxisZ_ = Header_->Angle_ = 0;
166  Header_->C1_ = Header_->C2_ = Header_->C3_ = 0;
167 
168  //pointer to data array behind header
169  Data_ = (unsigned char*)Shm_ +sizeof(VideoSource_Shm::ShmImageHeader);
170 
171  bIsInitialized_ = true;
172  return true;
173 
174 }
175 
176 
177 
179 {
180 
181  ImgBuffer_.clear();
182  ImgBuffer_.str("");
183  ImgBuffer_ <<*Image;
184 
185  int size = ImgBuffer_.str().length();
186  if (!bIsInitialized_) {
187  DataLength_ = size;
188  Init_();
189  }
190  BIASASSERT(size <=DataLength_);
191 
192  Header_->Size_ = DataLength_;
193 
194  memcpy(Data_,ImgBuffer_.str().c_str() ,Header_->Size_);
195  //Set time != 0 to indicate new image data
196  Header_->Time_ = Image->GetTime();
197  //Set data for RMatrix and CVector
198  BIAS::Vector3<double> tmpVec;
199  (Image->GetR()).GetRotationAxisAngle(tmpVec, Header_->Angle_);
200  Header_->AxisX_ = tmpVec[0];
201  Header_->AxisY_ = tmpVec[1];
202  Header_->AxisZ_ = tmpVec[2];
203  tmpVec = Image->GetC();
204  Header_->C1_ = tmpVec[0];
205  Header_->C2_ = tmpVec[1];
206  Header_->C3_ = tmpVec[2];
207 // cout <<"VideoShmFeeder: Header is: "<<setw(20)<<setprecision(20)
208 // <<Header_->Time_<<" , "<<Header_->Size_<<endl;
209 
210  return true;
211 }
212 
class BIASImageBase_EXPORT Image
Definition: ImageBase.hh:91
bool ProcessImage(BIAS::Camera< unsigned char > *Image)
The image template class for specific storage types.
Definition: Image.hh:78