source: pacpusframework/branches/2.0-beta1/src/TestComponents/Video/dbt/DbtPlyImageManager.cpp@ 89

Last change on this file since 89 was 89, checked in by morasjul, 11 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

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