Add C++ PNG/JPEG converter for foxglove CompressedImage
This gives us a converter that we can slot into C++ code to convert our
CameraImage type into foxglove CompressedImage types, for more efficient
display.
Change-Id: I6e831c341ca90ca26b38af504c1bb86d482cf561
Signed-off-by: James Kuszmaul <jabukuszmaul@gmail.com>
diff --git a/frc971/vision/foxglove_image_converter.h b/frc971/vision/foxglove_image_converter.h
new file mode 100644
index 0000000..add83a6
--- /dev/null
+++ b/frc971/vision/foxglove_image_converter.h
@@ -0,0 +1,36 @@
+#ifndef FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_
+#define FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_
+#include "external/com_github_foxglove_schemas/CompressedImage_generated.h"
+#include "frc971/vision/vision_generated.h"
+#include "aos/events/event_loop.h"
+
+namespace frc971::vision {
+// Empirically, from 2022 logs:
+// PNG is an ~2x space savings relative to raw images.
+// JPEG is an ~10x space savings relative to PNG.
+// Both perform significantly better than attempting to perform in-browser
+// conversion with a user-script in Foxglove Studio.
+enum class ImageCompression { kJpeg, kPng };
+
+flatbuffers::Offset<foxglove::CompressedImage> CompressImage(
+ const CameraImage *raw_image, flatbuffers::FlatBufferBuilder *fbb,
+ ImageCompression compression);
+
+// This class provides a simple converter that will take an AOS CameraImage
+// channel and output
+class FoxgloveImageConverter {
+ public:
+ // Watches for frc971.vision.CameraImage messages on the input_channel and
+ // sends foxglove.CompressedImage messages on the output_channel, using the
+ // specified image compression algorithm.
+ FoxgloveImageConverter(aos::EventLoop *event_loop,
+ std::string_view input_channel,
+ std::string_view output_channel,
+ ImageCompression compression);
+
+ private:
+ aos::EventLoop *event_loop_;
+ aos::Sender<foxglove::CompressedImage> sender_;
+};
+} // namespace frc971::vision
+#endif // FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_