blob: 872ac14703a52e802b098c734d3dc4da5903e4fe [file] [log] [blame]
James Kuszmaul0c593962023-01-28 16:04:20 -08001#ifndef FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_
2#define FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_
James Kuszmaul0c593962023-01-28 16:04:20 -08003#include "aos/events/event_loop.h"
James Kuszmaul59a308f2023-01-28 19:14:07 -08004#include "external/com_github_foxglove_schemas/CompressedImage_generated.h"
5#include "frc971/vision/charuco_lib.h"
6#include "frc971/vision/vision_generated.h"
James Kuszmaul0c593962023-01-28 16:04:20 -08007
8namespace frc971::vision {
9// Empirically, from 2022 logs:
10// PNG is an ~2x space savings relative to raw images.
11// JPEG is an ~10x space savings relative to PNG.
12// Both perform significantly better than attempting to perform in-browser
13// conversion with a user-script in Foxglove Studio.
14enum class ImageCompression { kJpeg, kPng };
15
James Kuszmaul59a308f2023-01-28 19:14:07 -080016std::string_view ExtensionForCompression(ImageCompression compression);
17
James Kuszmaul0c593962023-01-28 16:04:20 -080018flatbuffers::Offset<foxglove::CompressedImage> CompressImage(
James Kuszmaul59a308f2023-01-28 19:14:07 -080019 const cv::Mat image, const aos::monotonic_clock::time_point eof,
20 flatbuffers::FlatBufferBuilder *fbb, ImageCompression compression);
James Kuszmaul0c593962023-01-28 16:04:20 -080021
22// This class provides a simple converter that will take an AOS CameraImage
23// channel and output
24class FoxgloveImageConverter {
25 public:
26 // Watches for frc971.vision.CameraImage messages on the input_channel and
27 // sends foxglove.CompressedImage messages on the output_channel, using the
28 // specified image compression algorithm.
29 FoxgloveImageConverter(aos::EventLoop *event_loop,
30 std::string_view input_channel,
31 std::string_view output_channel,
32 ImageCompression compression);
33
34 private:
35 aos::EventLoop *event_loop_;
James Kuszmaul59a308f2023-01-28 19:14:07 -080036 ImageCallback image_callback_;
James Kuszmaul0c593962023-01-28 16:04:20 -080037 aos::Sender<foxglove::CompressedImage> sender_;
38};
39} // namespace frc971::vision
40#endif // FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_