Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions zstd_image_transport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ find_package(image_transport REQUIRED)
find_package(pluginlib REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(ZLIB REQUIRED)
find_package(zstd REQUIRED)

include_directories(include)

add_library(
${PROJECT_NAME} SHARED
src/zlib_cpp.cpp
src/zstd_wrapper.cpp
src/zstd_publisher.cpp
src/zstd_subscriber.cpp
src/manifest.cpp
)

target_link_libraries(${PROJECT_NAME}
ZLIB::ZLIB
zstd::libzstd_shared
image_transport::image_transport
rclcpp::rclcpp
pluginlib::pluginlib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef ZSTD_IMAGE_TRANSPORT__ZSTD_PUBLISHER_HPP_
#define ZSTD_IMAGE_TRANSPORT__ZSTD_PUBLISHER_HPP_

#include <memory>
#include <string>
#include <vector>
#include <unordered_set>
Expand All @@ -44,6 +45,9 @@

#include "zstd_image_transport/zstd_common.hpp"

// Forward declaration — defined in the private zstd_wrapper.hpp header.
namespace zstd_wrapper {class Compressor;}

namespace zstd_image_transport
{

Expand All @@ -70,6 +74,9 @@ class ZstdPublisher : public image_transport::SimplePublisherPlugin<CompressedIm
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_param_interface_;
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface_;

// Reusable compression context — avoids per-frame ZSTD_CCtx allocation.
mutable std::unique_ptr<zstd_wrapper::Compressor> compressor_;

private:
std::vector<std::string> parameters_;
std::unordered_set<std::string> deprecated_parameters_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef ZSTD_IMAGE_TRANSPORT__ZSTD_SUBSCRIBER_HPP_
#define ZSTD_IMAGE_TRANSPORT__ZSTD_SUBSCRIBER_HPP_

#include <memory>
#include <string>
#include <vector>

Expand All @@ -44,6 +45,9 @@

#include "zstd_image_transport/zstd_common.hpp"

// Forward declaration — defined in the private zstd_wrapper.hpp header.
namespace zstd_wrapper {class Decompressor;}

namespace zstd_image_transport
{

Expand All @@ -67,6 +71,9 @@ class ZstdSubscriber final
const Callback & user_cb) override;

rclcpp::Logger logger_;

// Reusable decompression context — avoids per-frame ZSTD_DCtx allocation.
std::unique_ptr<zstd_wrapper::Decompressor> decompressor_;
};

} // namespace zstd_image_transport
Expand Down
2 changes: 1 addition & 1 deletion zstd_image_transport/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<buildtool_depend>ament_cmake</buildtool_depend>

<depend>image_transport</depend>
<depend>zlib</depend>
<depend>libzstd-dev</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
Expand Down
164 changes: 0 additions & 164 deletions zstd_image_transport/src/zlib_cpp.cpp

This file was deleted.

16 changes: 6 additions & 10 deletions zstd_image_transport/src/zstd_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <rclcpp/rclcpp.hpp>

#include "zlib_cpp.hpp"
#include "zstd_wrapper.hpp"

namespace zstd_image_transport
{
Expand Down Expand Up @@ -97,23 +97,19 @@ void ZstdPublisher::publish(
const sensor_msgs::msg::Image & message,
const PublisherT & publisher) const
{
// Fresh Configuration
int cfg_zstd_level =
node_param_interface_->get_parameter(
parameters_[ZSTD_LEVEL]).as_int();

zlib::Comp comp(static_cast<zlib::Comp::Level>(cfg_zstd_level), true);
auto g_compressed_data =
comp.Process(&message.data[0], message.data.size(), true);

size_t total_size = 0;
for (const auto & data : g_compressed_data) {
total_size += data->size;
// Lazily initialize the reusable compression context.
if (!compressor_) {
compressor_ = std::make_unique<zstd_wrapper::Compressor>();
}

auto compressed = std::make_unique<sensor_msgs::msg::CompressedImage>();

int metadata = 4 + 4 + 1 + 4 + 4 + message.encoding.size();
sensor_msgs::msg::CompressedImage compressed;
compressed.data.resize(metadata + bound);

compressed->data.resize(total_size + metadata);

Expand Down
Loading
Loading