RsbRstDepthImageExportPlugin.cpp

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

Download (5.79 KB)

 
1
#include "RsbRstDepthImageExportPlugin.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

    
17
#include <rsc/runtime/Properties.h>
18

    
19
#include <rsb/converter/Repository.h>
20
#include <rsb/converter/SerializationException.h>
21
#include <rsb/converter/ProtocolBufferConverter.h>
22

    
23

    
24

    
25

    
26
#include <rsb/util/QueuePushHandler.h>
27
#include <rsb/converter/ConverterSelectionStrategy.h>
28
#include <rsb/converter/PredicateConverterList.h>
29
#include <rsb/converter/RegexConverterPredicate.h>
30

    
31
#include <gui/Gimage.h>
32
#include <main/grab.h>
33

    
34

    
35
#include "rst/vision/SimpleXYZImage.pb.h"
36
#include "rst/vision/Image.pb.h"
37
#include "rst/math/MatrixDouble.pb.h"
38
#include "rst/math/VectorDouble.pb.h"
39

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

    
42
using namespace std;
43

    
44

    
45
static void freeObject(void* data) {
46
        if (data!=NULL){
47
                char* d = (char*) data;
48
                delete[] d;
49
        }
50
}
51

    
52

    
53
using namespace ICEWING;
54
using namespace std;
55
using namespace ICEWING;
56
using namespace rsc::runtime;
57
using namespace rsc::threading;
58
using namespace rsb;
59
using namespace rsb::converter;
60
using namespace rsb::filter;
61

    
62
namespace rsbrstdepthimageexport {
63

    
64

    
65
RsbRstDepthImageExportPlugin::RsbRstDepthImageExportPlugin(char *name) :
66
        ICEWING::Plugin(name), factory(rsb::getFactory()) {
67
}
68

    
69

    
70

    
71
RsbRstDepthImageExportPlugin::~RsbRstDepthImageExportPlugin() {
72
}
73

    
74
int RsbRstDepthImageExportPlugin::InitOptions() {
75
    return 0;
76
}
77

    
78
//create WireType
79
template<typename WireType>
80
typename ConverterSelectionStrategy<WireType>::Ptr createConverterSelectionStrategy() {
81
    list<pair<ConverterPredicatePtr, typename Converter<WireType>::Ptr> > converters;
82
    converters.push_back(
83
            make_pair(
84
                    ConverterPredicatePtr(
85
                            new RegexConverterPredicate(".*")),
86
                    typename Converter<WireType>::Ptr(
87
                            new rsb::converter::ProtocolBufferConverter<rst::vision::Image>())));
88
    return typename ConverterSelectionStrategy<WireType>::Ptr(
89
            new PredicateConverterList<WireType>(converters.begin(),
90
                    converters.end()));
91
}
92

    
93

    
94
void RsbRstDepthImageExportPlugin::help() {
95
    fprintf(stderr, "\n%s plugin for %s, (c) 2014 by Niels Kroemker\n"
96
            "Convert KinectDepth images to generic RST and export rst::vision::Image messages .\n"
97
            "\n"
98
            " Options:\n"
99
            "  -i <s>   iceWing indentifier for Image data to read\n"
100
            "  -o <s>   export RSB scope\n", name,
101
            ICEWING_NAME);
102
    gui_exit(1);
103
}
104

    
105
void RsbRstDepthImageExportPlugin::Init(grabParameter *para, int argc, char **argv) {
106

    
107
    void *arg;
108
    char ch;
109
    int nr = 0;
110
    while (nr < argc) {
111
        ch = iw_parse_args(argc, argv, &nr, &arg, "-h:H --help:H"
112
                        "-i:Ir --input:Ir"
113
                        "-o:Or --output:Or");
114
        switch (ch) {
115
        case 'H':
116
        case '\0':
117
            help();
118
            break;
119
        case 'I':
120
            imgIdent = (char*) arg;
121
            break;
122
        case 'O':
123
            scope = rsb::Scope((char*) arg);
124
            break;
125
        default:
126
            fprintf(stderr, "Unknown character %c!\n", ch);
127
            help();
128
        }
129
    }
130

    
131
    bool error = false;
132
    if (imgIdent.empty()) {
133
        cerr << "No image input identifier specified with command line arguments"
134
                << endl;
135
        error = true;
136
    }
137
    if (scope == rsb::Scope()) {
138
        cerr << "No output identifier specified with command line arguments" << endl;
139
        error = true;
140
    }
141
    if (error) {
142
        help();
143
    }
144

    
145

    
146

    
147
        //informer configuration
148
        ParticipantConfig config = factory.getDefaultParticipantConfig();
149
            set < ParticipantConfig::Transport > transports = config.getTransports();
150
        for (set<ParticipantConfig::Transport>::const_iterator it =
151
                    transports.begin(); it != transports.end(); ++it) {
152
                ParticipantConfig::Transport& transport = config.mutableTransport(
153
                        it->getName());
154
                Properties options = transport.getOptions();
155
                options["converters"] = createConverterSelectionStrategy<string>();
156
                transport.setOptions(options);
157
        }
158
        informer = rsb::getFactory().createInformer<rsb::AnyType>(scope, config);
159
    plug_observ_data(this, imgIdent.c_str());
160
}
161

    
162

    
163
bool RsbRstDepthImageExportPlugin::Process(char *ident, plugData *data) {
164
        if (string(ident) != imgIdent) {
165
                assert(false);
166
                return true;
167
            }
168

    
169
        imgInput = (iwImage*) data->data;
170
        
171
        //Create rst::vision::Image
172
            rst::vision::Image rstImage;
173

    
174
        if(imgInput->type == IW_16U) {
175
                rstImage.set_depth(rst::vision::Image::DEPTH_16U);
176
        }else if(imgInput->type == IW_8U) {
177
                rstImage.set_depth(rst::vision::Image::DEPTH_8U);
178
        }
179
        rstImage.set_channels(1);
180
        rstImage.set_width(imgInput->width);
181
        rstImage.set_height(imgInput->height);
182

    
183
        if (imgInput->rowstride == 0) {
184
                rstImage.set_data_order(rst::vision::Image::DATA_SEPARATE);
185
        } else {
186
                rstImage.set_data_order(rst::vision::Image::DATA_INTERLEAVED);
187
        }
188

    
189
        rstImage.set_color_mode(rst::vision::Image::COLOR_GRAYSCALE);
190

    
191

    
192
        // write data in rst::vision::Image
193
        if(imgInput->type == IW_16U) {
194
                rstImage.set_data(imgInput->data[0],
195
                        imgInput->width * imgInput->height * imgInput->planes*2);
196
        } else if(imgInput->type == IW_8U) {
197
                rstImage.set_data(imgInput->data[0],
198
                        imgInput->width * imgInput->height * imgInput->planes);
199
        }
200

    
201

    
202
        //Publish rst::vision::Image
203
        google::protobuf::Message *output = (google::protobuf::Message*) &rstImage;
204
            informer->publish(
205
            boost::shared_ptr<google::protobuf::Message>(output,
206
                    rsc::misc::NullDeleter()), rsc::runtime::typeName(*output));
207
            return true;
208

    
209
}
210

    
211
}
212

    
213
extern "C" plugDefinition* plug_get_info(int instCount, bool* append) {
214
    *append = TRUE;
215

    
216
    ICEWING::Plugin* newPlugin = new rsbrstdepthimageexport::RsbRstDepthImageExportPlugin(
217
            g_strdup_printf("convertdepthrst%d", instCount));
218

    
219
    return newPlugin;
220
}