source: pacpussensors/trunk/StdDbtPlayerComponents/DbtPlyImageManager.cpp@ 67

Last change on this file since 67 was 50, checked in by phudelai, 10 years ago

Flea3Component: Shared memory changed for stereovision
StereoVisionDisparity: Added for the PFE of Pierre

File size: 5.9 KB
Line 
1/*********************************************************************
2// created: 2007/04/12 - 16:30
3//
4// author: Elie Al Alam & Gerald Dherbomez
5//
6// version: $Id: DbtPlyImageManager.cpp 1239 2012-11-28 16:30:00Z kurdejma $
7//
8// purpose: Dbite Player Image Manager implementation
9*********************************************************************/
10
11#include "DbtPlyImageManager.h"
12
13#include "ImageViewer.h"
14
15#include "Pacpus/kernel/ComponentFactory.h"
16#include "Pacpus/kernel/ComponentManager.h"
17#include "Pacpus/kernel/DbiteFileTypes.h"
18#include "Pacpus/kernel/Log.h"
19#include "Pacpus/PacpusTools/ShMem.h"
20
21#include <iostream>
22#include <QImage>
23#include <QMutex>
24
25namespace pacpus {
26
27using namespace std;
28
29DECLARE_STATIC_LOGGER("pacpus.base.DbtPlyImageManager");
30
31static const string kDefaultMemoryName = "IMAGE";
32
33/// Construction de la fabrique de composant DbtPlyImageManager
34static ComponentFactory<DbtPlyImageManager> sFactory("DbtPlyImageManager");
35
36/// Constructor
37DbtPlyImageManager::DbtPlyImageManager(QString name)
38 : DbtPlyFileManager (name)
39 , im_(NULL)
40 , shMem_(NULL)
41 , imageMutex_(new QMutex())
42 , firstTime(true)
43{
44}
45
46/// Destructor
47DbtPlyImageManager::~DbtPlyImageManager()
48{
49 // #if WIN32
50 delete imageMutex_;
51 // #endif
52 if (shMem_) {
53 delete shMem_;
54 shMem_ = NULL;
55 }
56}
57
58/// TODO: doc
59void DbtPlyImageManager::processData(road_time_t /*t*/, road_timerange_t /*tr*/, void * buf)
60{
61 if (!buf) {
62 LOG_WARN("no data to process: empty data buffer");
63 return;
64 }
65
66 // look at the dbt index in file manager and get the identifier of dbt structure
67 hdfile_header_t::DataTypeT id = dbt_[dbtIndex_].pfile->getType();
68
69 switch (id) {
70 case STEREO_LEFT_IMAGE:
71 case STEREO_RIGHT_IMAGE:
72 //{
73 // QMutexLocker mutexLocker(imageMutex_);
74 // Q_UNUSED(mutexLocker);
75 // if (firstTime) {
76 // im_ = new QImage(320, 240, QImage::Format_RGB32);
77 // if (!im_) {
78 // LOG_ERROR("invalid image");
79 // }
80 // shMem_ = new ShMem(kDefaultMemoryName.c_str(), im_->numBytes());
81 // firstTime = false;
82 // }
83 // // convert image to 32 bits for the display
84 // YtoRGB32(im_->bits(), (unsigned char *)buf);
85 // shMem_->write(im_->bits(), im_->numBytes());
86 //}
87 // break;
88
89 case FILE_JPEG:
90 imageFile_ = mDbtDataPath + (char *)buf;
91 LOG_TRACE("image path = " << imageFile_);
92 if (mVerbose) {
93 cout << "[IMAGE]:\t"
94 << imageFile_.toStdString() << endl
95 ;
96 }
97
98 {
99 QMutexLocker mutexLocker(imageMutex_);
100 Q_UNUSED(mutexLocker);
101 if (!im_) {
102 LOG_TRACE("allocating image");
103 im_ = new QImage();
104 if (!im_) {
105 LOG_ERROR("cannot allocate image");
106 }
107 }
108 static int shmemSize;
109 bool imageLoaded = im_->load(imageFile_);
110 if (!imageLoaded) {
111 LOG_WARN("cannot load image file '" << imageFile_ << "'");
112 return;
113 }
114 LOG_TRACE("image loaded");
115 if (firstTime) {
116 // send image in shared memory
117 shmemSize = im_->byteCount();
118 LOG_TRACE("'" << mShMemName << "'" << " memory size = " << shmemSize);
119 shMem_ = new ShMem(mShMemName.toStdString().c_str(), shmemSize);
120 firstTime = false;
121 }
122 shMem_->write(im_->bits(), shmemSize);
123 }
124 break;
125
126 default:
127 break;
128 }
129
130 Q_EMIT displayIm(im_);
131}
132
133ComponentBase::COMPONENT_CONFIGURATION DbtPlyImageManager::configureComponent(XmlComponentConfig config)
134{
135 mShMemName = kDefaultMemoryName.c_str();
136 if (config.getProperty("shmem") != QString::null) {
137 mShMemName = config.getProperty("shmem");
138 }
139 return DbtPlyFileManager::configureComponent(config);
140}
141
142void DbtPlyImageManager::startActivity()
143{
144 DbtPlyFileManager::startActivity();
145}
146
147void DbtPlyImageManager::stopActivity()
148{
149 delete im_; im_ = NULL;
150 DbtPlyFileManager::stopActivity();
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Pour l'affichage de l'image
155void DbtPlyImageManager::displayUI()
156{
157 imviewer_ = new ImageViewer;
158 imviewer_->setMutex(imageMutex_);
159 imviewer_->show();
160 imviewer_->setWindowTitle(name());
161 connect(this, SIGNAL(displayIm(QImage*)), imviewer_, SLOT(display(QImage*)));
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Convert the image from Y to RGB32
166/// The image is stored using a 32-bit RGB format (0xffRRGGBB)
167void DbtPlyImageManager::YtoRGB32(unsigned char * dest, unsigned char * src)
168{
169 unsigned char *srcptr, *srcend, *destptr;
170
171 srcptr = src;
172 srcend = srcptr + (320 * 240);
173 destptr = dest;
174
175 // single-stage idiotproofing
176 if (src == NULL || dest == NULL)
177 return;
178
179 // just Y's (monochrome)
180
181 // unroll it to 4 per cycle
182
183 while(srcptr < srcend) {
184 *destptr++ = *srcptr;
185 *destptr++ = *srcptr;
186 *destptr++ = *srcptr;
187 destptr++;//A
188 srcptr++;
189
190 *destptr++ = *srcptr;
191 *destptr++ = *srcptr;
192 *destptr++ = *srcptr;
193 destptr++;//A
194 srcptr++;
195
196 *destptr++ = *srcptr;
197 *destptr++ = *srcptr;
198 *destptr++ = *srcptr;
199 destptr++;//A
200 srcptr++;
201
202 *destptr++ = *srcptr;
203 *destptr++ = *srcptr;
204 *destptr++ = *srcptr;
205 destptr++;//A
206 srcptr++;
207 }
208}
209
210void DbtPlyImageManager::tic()
211{
212 tic_ = road_time();
213}
214
215void DbtPlyImageManager::toc(char * text)
216{
217 cout << "duration = " << (int)(road_time() - tic_) << "\t"
218 << text << "\n"
219 ;
220}
221
222} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.