Skip to content

Commit

Permalink
frameset support added to the processing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
matkatz committed Aug 15, 2018
1 parent f78e562 commit 6850677
Show file tree
Hide file tree
Showing 36 changed files with 1,733 additions and 1,313 deletions.
4 changes: 3 additions & 1 deletion CMake/realsense.def
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ EXPORTS
rs2_keep_frame
rs2_frame_add_ref
rs2_pose_frame_get_pose_data

rs2_frame_apply_filter

rs2_get_option
rs2_set_option
rs2_supports_option
Expand Down Expand Up @@ -147,6 +148,7 @@ EXPORTS
rs2_clone_stream_profile

rs2_allocate_synthetic_video_frame
rs2_allocate_points
rs2_allocate_composite_frame
rs2_synthetic_frame_ready
rs2_create_processing_block
Expand Down
91 changes: 86 additions & 5 deletions examples/example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ inline void draw_text(int x, int y, const char * text)
class texture
{
public:
void render(const rs2::frameset& frames, int window_width, int window_height)
{
std::vector<rs2::video_frame> supported_frames;
for (auto f : frames)
{
if (can_render(f))
supported_frames.push_back(f);
}
if (supported_frames.empty())
return;

std::sort(supported_frames.begin(), supported_frames.end(), [](rs2::frame first, rs2::frame second)
{ return first.get_profile().stream_type() < second.get_profile().stream_type(); });

auto image_grid = calc_grid(float2{ (float)window_width, (float)window_height }, supported_frames);

int image_index = 0;
for (auto f : supported_frames)
{
upload(f);
show(image_grid.at(image_index));
image_index++;
}
}

void render(const rs2::video_frame& frame, const rect& r)
{
upload(frame);
Expand Down Expand Up @@ -122,20 +147,76 @@ class texture

draw_text((int)r.x + 15, (int)r.y + 20, rs2_stream_to_string(stream));
}

private:
GLuint gl_handle = 0;
int width = 0;
int height = 0;
rs2_stream stream = RS2_STREAM_ANY;

bool can_render(const rs2::frame& f) const
{
auto format = f.get_profile().format();
switch (format)
{
case RS2_FORMAT_RGB8:
case RS2_FORMAT_RGBA8:
case RS2_FORMAT_Y8:
return true;
default:
return false;
}
}

rect calc_grid(float2 window, int streams)
{
float ratio = window.x / window.y;
auto x = sqrt(ratio * (float)streams);
auto y = (float)streams / x;
auto w = round(x);
auto h = round(y);
while (w*h > streams)
h > w ? h-- : w--;
while (w*h < streams)
h > w ? w++ : h++;
auto new_w = round(window.x / w);
auto new_h = round(window.y / h);
return rect{ w, h, new_w, new_h}; //column count, line count, cell width cell height
}

std::vector<rect> calc_grid(float2 window, std::vector<rs2::video_frame>& frames)
{
auto grid = calc_grid(window, frames.size());

int index = 0;
std::vector<rect> rv;
int curr_line = -1;
for (auto f : frames)
{
auto mod = index % (int)grid.x;
auto fw = (float)frames[index].get_width();
auto fh = (float)frames[index].get_height();

float cell_x_postion = (float)(mod * grid.w);
if (mod == 0) curr_line++;
float cell_y_position = curr_line * grid.h;

auto r = rect{ cell_x_postion, cell_y_position, grid.w, grid.h };
rv.push_back(r.adjust_ratio(float2{ fw, fh }));
index++;
}

return rv;
}
};

class window
{
public:
std::function<void(bool)> on_left_mouse = [](bool) {};
std::function<void(bool)> on_left_mouse = [](bool) {};
std::function<void(double, double)> on_mouse_scroll = [](double, double) {};
std::function<void(double, double)> on_mouse_move = [](double, double) {};
std::function<void(int)> on_key_release = [](int) {};
std::function<void(double, double)> on_mouse_move = [](double, double) {};
std::function<void(int)> on_key_release = [](int) {};

window(int width, int height, const char* title)
: _width(width), _height(height)
Expand Down Expand Up @@ -209,7 +290,7 @@ class window
operator GLFWwindow*() { return win; }

private:
GLFWwindow* win;
GLFWwindow * win;
int _width, _height;
};

Expand Down Expand Up @@ -323,4 +404,4 @@ void register_glfw_callbacks(window& app, glfw_state& app_state)
app_state.yaw = app_state.pitch = 0; app_state.offset_x = app_state.offset_y = 0.0;
}
};
}
}
6 changes: 3 additions & 3 deletions examples/post-processing/rs-post-processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ Class to encapsulate a filter alongside its options
class filter_options
{
public:
filter_options(const std::string name, rs2::process_interface& filter);
filter_options(const std::string name, rs2::processing_block& filter);
filter_options(filter_options&& other);
std::string filter_name; //Friendly name of the filter
rs2::process_interface& filter; //The filter in use
rs2::processing_block& filter; //The filter in use
std::map<rs2_option, filter_slider_ui> supported_options; //maps from an option supported by the filter, to the corresponding slider
std::atomic_bool is_enabled; //A boolean controlled by the user that determines whether to apply the filter or not
};
Expand Down Expand Up @@ -348,7 +348,7 @@ bool filter_slider_ui::is_all_integers(const rs2::option_range& range)
/**
Constructor for filter_options, takes a name and a filter.
*/
filter_options::filter_options(const std::string name, rs2::process_interface& filter) :
filter_options::filter_options(const std::string name, rs2::processing_block& filter) :
filter_name(name),
filter(filter),
is_enabled(true)
Expand Down
19 changes: 18 additions & 1 deletion include/librealsense2/h/rs_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ int rs2_is_frame_extendable_to(const rs2_frame* frame, rs2_extension extension_t
rs2_frame* rs2_allocate_synthetic_video_frame(rs2_source* source, const rs2_stream_profile* new_stream, rs2_frame* original,
int new_bpp, int new_width, int new_height, int new_stride, rs2_extension frame_type, rs2_error** error);

/**
* Allocate new points frame using a frame-source provided form a processing block
* \param[in] source Frame pool to allocate the frame from
* \param[in] new_stream New stream profile to assign to newly created frame
* \param[in] original A reference frame that can be used to fill in auxilary information like format, width, height, bpp, stride (if applicable)
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
* \return reference to a newly allocated frame, must be released with release_frame
* memory for the frame is likely to be re-used from previous frame, but in lack of available frames in the pool will be allocated from the free store
*/
rs2_frame* rs2_allocate_points(rs2_source* source, const rs2_stream_profile* new_stream, rs2_frame* original, rs2_error** error);

/**
* Allocate new composite frame, aggregating a set of existing frames
* \param[in] source Frame pool to allocate the frame from
Expand Down Expand Up @@ -323,7 +334,13 @@ void rs2_synthetic_frame_ready(rs2_source* source, rs2_frame* frame, rs2_error**
*/
void rs2_pose_frame_get_pose_data(const rs2_frame* frame, rs2_pose* pose, rs2_error** error);


/**
* Apply a processing block on the frame and return a new processed composite frame
* \param[in] frame Frame to process
* \param[out] processing_block Pointer to a user provided processing block
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
*/
rs2_frame* rs2_frame_apply_filter(const rs2_frame* frame, rs2_processing_block* processing_block, rs2_error** error);


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 6850677

Please sign in to comment.