RsbRstDepthImageImportPlugin.cpp

I. Berger, 04/16/2014 11:14 AM

Download (6.47 KB)

 
1
#include "RsbRstDepthImageImportPlugin.h"
2

    
3
#include <iostream>
4

    
5
#include <stdio.h>
6

    
7
#include <gui/Ggui.h>
8
#include <gui/Goptions.h>
9

    
10
#include <google/protobuf/message.h>
11

    
12
#include <boost/shared_ptr.hpp>
13

    
14
#include <stdexcept>
15

    
16
#include <rsb/converter/Repository.h>
17
#include <rsb/converter/SerializationException.h>
18
#include <rsb/converter/ProtocolBufferConverter.h>
19

    
20
#include <gui/Gimage.h>
21
#include <main/grab.h>
22

    
23

    
24
#include "rst/vision/SimpleXYZImage.pb.h"
25
#include "rst/vision/Image.pb.h"
26
#include "rst/math/MatrixDouble.pb.h"
27
#include "rst/math/VectorDouble.pb.h"
28

    
29
#include <opencv/cv.h>
30

    
31
#include <rsc/runtime/Properties.h>
32

    
33
#include <rsb/converter/Repository.h>
34
#include <rsb/util/QueuePushHandler.h>
35
#include <rsb/converter/ConverterSelectionStrategy.h>
36
#include <rsb/converter/PredicateConverterList.h>
37
#include <rsb/converter/RegexConverterPredicate.h>
38

    
39

    
40
#include "rst/vision/Image.pb.h"
41

    
42

    
43

    
44
using namespace std;
45

    
46
using namespace rsc::runtime;
47
using namespace rsc::threading;
48
using namespace rsb;
49
using namespace rsb::util;
50
using namespace rsb::converter;
51
using namespace rsb::filter;
52

    
53

    
54

    
55
static void iw_free_img_kinect(void* data) {
56
        iw_img_free((iwImage*) data, IW_IMG_FREE_ALL);
57
}
58

    
59
static void render16bit(prevBuffer *b, guchar **planes, int width, int height) {
60
        prevDataImage img;
61
        iwImage i;
62

    
63
        iw_img_init(&i);
64
        i.planes = 1;
65
        i.type = IW_16U;
66
        i.width = width;
67
        i.height = height;
68
        i.ctab = IW_GRAY;
69
        i.data = planes;
70

    
71
        img.i = &i;
72
        img.x = 0;
73
        img.y = 0;
74

    
75
        prev_render_imgs(b, &img, 1, RENDER_CLEAR, width, height);
76
}
77

    
78

    
79
using namespace ICEWING;
80

    
81
namespace rsbrstdepthimageimport {
82

    
83
class iwImageDeleter {
84
public:
85
    void operator()(void *i) {
86
        iwImage *image = (iwImage *) i;
87
        iw_img_free(image, IW_IMG_FREE_ALL );
88
    }
89
};
90

    
91

    
92
RsbRstDepthImageImportPlugin::RsbRstDepthImageImportPlugin(char *name) : ICEWING::Plugin(name), factory(rsb::getFactory()),
93
                                                           imageQueue(new SynchronizedQueue<boost::shared_ptr<rst::vision::Image> >(1)) {
94
}
95

    
96

    
97

    
98
RsbRstDepthImageImportPlugin::~RsbRstDepthImageImportPlugin() {
99
}
100

    
101

    
102
int RsbRstDepthImageImportPlugin::InitOptions() {
103
        plug_observ_data(this, "start");
104
        window = prev_new_window(plug_name(this, ".input"), -1, -1, false, false);
105
            return 0;
106
}
107

    
108
//wireType for rst::vision::Image
109
template<typename WireType>
110
typename ConverterSelectionStrategy<WireType>::Ptr createConverterSelectionStrategy() {
111
    list<pair<ConverterPredicatePtr, typename Converter<WireType>::Ptr> > converters;
112
    converters.push_back(
113
            make_pair(
114
                    ConverterPredicatePtr(
115
                            new RegexConverterPredicate("^.rst.vision.Image$")),
116
                    typename Converter<WireType>::Ptr(
117
                            new rsb::converter::ProtocolBufferConverter<rst::vision::Image>())));
118
    return typename ConverterSelectionStrategy<WireType>::Ptr(
119
            new PredicateConverterList<WireType>(converters.begin(),
120
                    converters.end()));
121
}
122

    
123

    
124
void RsbRstDepthImageImportPlugin::help() {
125
    fprintf(stderr, "\n%s plugin for %s, (c) 2014 by Niels Kroemker\n"
126
            "Import rst::vision::SimpleXYZImages messages and convert to generic KinectDepth.\n"
127
            "\n"
128
            " Options:\n"
129
            "  -i <s>   input RSB scope\n"
130
            "  -o <s>   iceWing indentifier for Image data data to output\n", name,
131
            ICEWING_NAME);
132
    gui_exit(1);
133
}
134

    
135

    
136

    
137
void RsbRstDepthImageImportPlugin::Init(grabParameter* /*para*/, int argc, char **argv) {
138

    
139
    void *arg;
140
    char ch;
141
    int nr = 0;
142
    while (nr < argc) {
143
        ch = iw_parse_args(argc, argv, &nr, &arg, "-h:H --help:H"
144
                        "-i:Ir --input:Ir"
145
                        "-o:Or --output:Or");
146
        switch (ch) {
147
        case 'H':
148
        case '\0':
149
            help();
150
            break;
151
        case 'I':
152
            scope = rsb::Scope((char*) arg);
153
            break;
154
        case 'O':
155
            imgIdent = (char*) arg;
156
            break;
157
        default:
158
            fprintf(stderr, "Unknown character %c!\n", ch);
159
            help();
160
        }
161
    }
162

    
163

    
164
        bool error = false;
165
        if (imgIdent.empty()) {
166
                cerr << "No image input identifier specified with command line arguments" << endl;
167
                error = true;
168
        }
169
        if (scope == rsb::Scope()) {
170
                cerr << "No input identifier specified with command line arguments" << endl;
171
                error = true;
172
        }
173
        if (error) {
174
        help();
175
        }
176

    
177

    
178
        //listener configuration
179
        ParticipantConfig config = factory.getDefaultParticipantConfig();
180
            set < ParticipantConfig::Transport > transports = config.getTransports();
181
        for (set<ParticipantConfig::Transport>::const_iterator it =
182
                    transports.begin(); it != transports.end(); ++it) {
183
                ParticipantConfig::Transport& transport = config.mutableTransport(
184
                        it->getName());
185
                Properties options = transport.getOptions();
186
                options["converters"] = createConverterSelectionStrategy<string>();
187
                transport.setOptions(options);
188
        }
189
        listener = rsb::getFactory().createListener(scope, config);
190

    
191
        
192

    
193
        boost::shared_ptr<QueuePushHandler<rst::vision::Image> > dh(
194
            new QueuePushHandler<rst::vision::Image>(imageQueue));
195
        boost::shared_ptr<Handler> h = boost::static_pointer_cast<Handler>(dh);
196
        listener->addHandler(h);
197

    
198

    
199
}
200

    
201

    
202

    
203

    
204

    
205

    
206
bool RsbRstDepthImageImportPlugin::Process(char *ident, plugData */*data*/) {
207
        
208
        if (string(ident) != "start") {
209
                assert(false);
210
                return true;
211
        }
212

    
213
        boost::shared_ptr<rst::vision::Image> newImage = imageQueue->pop();
214

    
215

    
216
        //create and draw icewing image from rst::vision::Image
217
        if (newImage->depth() == rst::vision::Image::DEPTH_16U) {
218
                iwImage* imgOut = iw_img_new_alloc(newImage->width(), newImage->height(), 1, IW_16U);
219

    
220
                memcpy(imgOut->data[0], newImage->data().c_str(), newImage->width() * newImage->height()*2);
221
                imgOut->ctab = IW_GRAY;
222

    
223
                render16bit(window, imgOut->data, imgOut->width, imgOut->height);
224

    
225
                prev_draw_buffer(window);
226
                plug_data_set(this, imgIdent.c_str(), imgOut, iw_free_img_kinect);
227

    
228
        } else if (newImage->depth() == rst::vision::Image::DEPTH_8U) {
229
                iwImage* imgOut = iw_img_new_alloc(newImage->width(), newImage->height(), 1, IW_8U);
230

    
231
                memcpy(imgOut->data[0], newImage->data().c_str(), newImage->width() * newImage->height());
232
                imgOut->ctab = IW_GRAY;
233
                
234

    
235
                prev_render(window, imgOut->data, imgOut->width, imgOut->height, imgOut->ctab);
236

    
237
                prev_draw_buffer(window);
238
                plug_data_set(this, imgIdent.c_str(), imgOut, iw_free_img_kinect);
239

    
240
        }
241
            return true;
242

    
243
}
244

    
245
}
246

    
247

    
248

    
249
extern "C" plugDefinition* plug_get_info(int instCount, bool* append) {
250
    *append = TRUE;
251

    
252
    ICEWING::Plugin* newPlugin = new rsbrstdepthimageimport::RsbRstDepthImageImportPlugin(
253
            g_strdup_printf("convertrstdepth%d", instCount));
254

    
255
    return newPlugin;
256
}