forked from mattmcd/NaCl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_proc.cpp
116 lines (107 loc) · 3.9 KB
/
image_proc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "improc_instance.hpp"
#include "singleton_factory.hpp"
#include "url_loader_handler.hpp"
#include <vector>
#include <thread>
#include <functional>
pp::Module* pp::CreateModule()
{
return new InstanceFactory<ImageProcInstance>();
}
void ImageProcInstance::Process( cv::Mat im)
{
auto result = (*processor)( im );
auto nBytes = result.elemSize() * result.total();
pp::VarDictionary msg;
pp::VarArrayBuffer data(nBytes);
uint8_t* copy = static_cast<uint8_t*>( data.Map());
memcpy( copy, result.data, nBytes );
msg.Set( "Type", "completed" );
msg.Set( "Data", data );
msg.Set( "Parameters", processor->getParameters());
PostMessage( msg );
}
void ImageProcInstance::PostTest()
{
pp::VarDictionary msg;
msg.Set( "Type", "completed" );
msg.Set( "Data", "Processed ok" );
PostMessage( msg );
}
void ImageProcInstance::SendStatus(const std::string& status)
{
pp::VarDictionary msg;
msg.Set( "Type", "status" );
msg.Set( "Message", status );
PostMessage( msg );
}
void ImageProcInstance::HandleMessage( const pp::Var& var_message )
{
// Interface: receive a { cmd: ..., args... } dictionary
pp::VarDictionary var_dict( var_message );
auto cmd = var_dict.Get( "cmd" ).AsString();
if ( cmd == "process" ) {
// Message is number of simulations to run
auto width = var_dict.Get("width").AsInt();
auto height = var_dict.Get("height").AsInt();
auto data = pp::VarArrayBuffer( var_dict.Get("data") );
auto selectedProcessor = var_dict.Get("processor").AsString();
bool newProcessor = selectedProcessor != processorName;
if ( newProcessor ) {
SendStatus("Creating processor factory");
auto processorFactory = SingletonFactory<
std::function<std::unique_ptr<Processor>()> >::getInstance();
SendStatus("Creating processor");
processor = processorFactory.getObject( selectedProcessor )();
processorName = selectedProcessor;
} else {
SendStatus("Reusing processor");
}
// Convert data to CMat
// SendStatus("Casting to byte array");
uint8_t* byteData = static_cast<uint8_t*>(data.Map());
// SendStatus("Creating cv::Mat");
auto Img = cv::Mat(height, width, CV_8UC4, byteData );
// SendStatus("Calling processing");
// Special case: Smiley
if ( (selectedProcessor == "Smiley!") && newProcessor ) {
// Only send the image data for overlay on the first time we change
// processor
pp::VarDictionary sm_var_dict( var_dict.Get( "args" ));
auto sm_width = sm_var_dict.Get("width").AsInt();
auto sm_height = sm_var_dict.Get("height").AsInt();
auto sm_data = pp::VarArrayBuffer( sm_var_dict.Get("data") );
uint8_t* sm_byteData = static_cast<uint8_t*>(sm_data.Map());
auto sm_Img = cv::Mat(sm_height, sm_width, CV_8UC4, sm_byteData );
processor->init( sm_Img );
} else if ( var_dict.HasKey( "args" ) ) {
// Args key is json string that processor will parse
auto json = var_dict.Get("args").AsString();
processor->init( json.c_str() );
}
Process( Img );
} else if ( cmd == "test" ) {
PostTest();
} else if ( cmd == "echo" ) {
auto data = pp::VarArrayBuffer( var_dict.Get("data") );
// auto result = data.is_array_buffer();
pp::VarDictionary msg;
msg.Set( "Type", "completed" );
msg.Set( "Data", data );
PostMessage( msg );
} else if ( cmd == "load" ) {
// Load resource URL
auto url = var_dict.Get( "url" ).AsString();
URLLoaderHandler* handler = URLLoaderHandler::Create(this, url);
if (handler != NULL) {
// Starts asynchronous download. When download is finished or when an
// error occurs, |handler| posts the results back to the browser
// vis PostMessage and self-destroys.
handler->Start();
}
} else {
// Disable simulation - background thread will see this at start of
// next iteration and terminate early
run_simulation_ = false;
}
}