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

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