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

Last change on this file since 121 was 115, checked in by DHERBOMEZ Gérald, 9 years ago

add outputs to image replay manager

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