| 1 | #ifndef BGCODE_BINARIZE_HPP |
| 2 | #define BGCODE_BINARIZE_HPP |
| 3 | |
| 4 | #include "binarize/export.h" |
| 5 | #include "core/core.hpp" |
| 6 | |
| 7 | namespace bgcode { namespace binarize { |
| 8 | |
| 9 | struct BGCODE_BINARIZE_EXPORT BaseMetadataBlock |
| 10 | { |
| 11 | // type of data encoding |
| 12 | uint16_t encoding_type{ 0 }; |
| 13 | // data in key/value form |
| 14 | std::vector<std::pair<std::string, std::string>> raw_data; |
| 15 | |
| 16 | // read block data in encoded format |
| 17 | core::EResult read_data(FILE& file, const core::BlockHeader& block_header); |
| 18 | }; |
| 19 | |
| 20 | struct BGCODE_BINARIZE_EXPORT FileMetadataBlock : public BaseMetadataBlock |
| 21 | { |
| 22 | // write block header and data |
| 23 | core::EResult write(FILE& file, core::ECompressionType compression_type, core::EChecksumType checksum_type) const; |
| 24 | // read block data |
| 25 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 26 | }; |
| 27 | |
| 28 | struct BGCODE_BINARIZE_EXPORT PrintMetadataBlock : public BaseMetadataBlock |
| 29 | { |
| 30 | // write block header and data |
| 31 | core::EResult write(FILE& file, core::ECompressionType compression_type, core::EChecksumType checksum_type) const; |
| 32 | // read block data |
| 33 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 34 | }; |
| 35 | |
| 36 | struct BGCODE_BINARIZE_EXPORT PrinterMetadataBlock : public BaseMetadataBlock |
| 37 | { |
| 38 | // write block header and data |
| 39 | core::EResult write(FILE& file, core::ECompressionType compression_type, core::EChecksumType checksum_type) const; |
| 40 | // read block data |
| 41 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 42 | }; |
| 43 | |
| 44 | struct BGCODE_BINARIZE_EXPORT ThumbnailBlock |
| 45 | { |
| 46 | core::ThumbnailParams params; |
| 47 | std::vector<std::byte> data; |
| 48 | |
| 49 | // write block header and data |
| 50 | core::EResult write(FILE& file, core::EChecksumType checksum_type); |
| 51 | // read block data |
| 52 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 53 | }; |
| 54 | |
| 55 | struct BGCODE_BINARIZE_EXPORT GCodeBlock |
| 56 | { |
| 57 | uint16_t encoding_type{ 0 }; |
| 58 | std::string raw_data; |
| 59 | |
| 60 | // write block header and data |
| 61 | core::EResult write(FILE& file, core::ECompressionType compression_type, core::EChecksumType checksum_type) const; |
| 62 | // read block data |
| 63 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 64 | }; |
| 65 | |
| 66 | struct BGCODE_BINARIZE_EXPORT SlicerMetadataBlock : public BaseMetadataBlock |
| 67 | { |
| 68 | // write block header and data |
| 69 | core::EResult write(FILE& file, core::ECompressionType compression_type, core::EChecksumType checksum_type) const; |
| 70 | // read block data |
| 71 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 72 | }; |
| 73 | |
| 74 | struct BGCODE_BINARIZE_EXPORT Slicer3MetadataBlock : public BaseMetadataBlock |
| 75 | { |
| 76 | Slicer3MetadataBlock() : BaseMetadataBlock() { |
| 77 | encoding_type = static_cast<std::underlying_type_t<core::EMetadataEncodingType>>( |
| 78 | core::EMetadataEncodingType::JSON); |
| 79 | } |
| 80 | |
| 81 | void set_json(std::string_view); |
| 82 | const std::string& json() const; |
| 83 | |
| 84 | // write block header and data |
| 85 | core::EResult write(FILE& file, core::ECompressionType compression_type, core::EChecksumType checksum_type) const; |
| 86 | // read block data |
| 87 | core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header); |
| 88 | }; |
| 89 | |
| 90 | enum class EPeekSlicerMetadataResult { |
| 91 | Slicer3MetadataFound, |
| 92 | SlicerMetadataFound, |
| 93 | OtherBlockFound, |
| 94 | ReadError |
| 95 | }; |
| 96 | |
| 97 | // Peek the block content (just metadata extra "header") and decide what kind of block follows |
| 98 | extern BGCODE_BINARIZE_EXPORT EPeekSlicerMetadataResult peek_slicer_metadata_block(FILE& file, const core::BlockHeader& block_header); |
| 99 | |
| 100 | |
| 101 | struct BinarizerConfig |
| 102 | { |
| 103 | struct Compression |
| 104 | { |
| 105 | core::ECompressionType file_metadata{ core::ECompressionType::None }; |
| 106 | core::ECompressionType printer_metadata{ core::ECompressionType::None }; |
| 107 | core::ECompressionType print_metadata{ core::ECompressionType::None }; |
| 108 | core::ECompressionType slicer_metadata{ core::ECompressionType::None }; |
| 109 | core::ECompressionType gcode{ core::ECompressionType::None }; |
| 110 | core::ECompressionType slicer3_metadata{ core::ECompressionType::None }; |
| 111 | }; |
| 112 | Compression compression; |
| 113 | core::EGCodeEncodingType gcode_encoding{ core::EGCodeEncodingType::None }; |
| 114 | core::EMetadataEncodingType metadata_encoding{ core::EMetadataEncodingType::INI }; |
| 115 | core::EChecksumType checksum{ core::EChecksumType::CRC32 }; |
| 116 | }; |
| 117 | |
| 118 | struct BGCODE_BINARIZE_EXPORT BinaryData |
| 119 | { |
| 120 | FileMetadataBlock file_metadata; |
| 121 | PrinterMetadataBlock printer_metadata; |
| 122 | std::vector<ThumbnailBlock> thumbnails; |
| 123 | SlicerMetadataBlock slicer_metadata; |
| 124 | Slicer3MetadataBlock slicer3_metadata; |
| 125 | PrintMetadataBlock print_metadata; |
| 126 | }; |
| 127 | |
| 128 | class BGCODE_BINARIZE_EXPORT Binarizer |
| 129 | { |
| 130 | public: |
| 131 | bool is_enabled() const; |
| 132 | void set_enabled(bool enable); |
| 133 | |
| 134 | BinaryData& get_binary_data(); |
| 135 | const BinaryData& get_binary_data() const; |
| 136 | |
| 137 | size_t get_max_gcode_cache_size() const; |
| 138 | void set_max_gcode_cache_size(size_t size); |
| 139 | |
| 140 | core::EResult initialize(FILE& file, const BinarizerConfig& config); |
| 141 | core::EResult append_gcode(const std::string& gcode); |
| 142 | core::EResult finalize(); |
| 143 | |
| 144 | private: |
| 145 | FILE* m_file{ nullptr }; |
| 146 | bool m_enabled{ false }; |
| 147 | BinarizerConfig m_config; |
| 148 | BinaryData m_binary_data; |
| 149 | std::string m_gcode_cache; |
| 150 | size_t m_gcode_cache_size{ 65536 }; |
| 151 | }; |
| 152 | |
| 153 | } // namespace binarize |
| 154 | } // namespace bgcode |
| 155 | |
| 156 | #endif // BGCODE_BINARIZE_HPP_ |