Usage with uint8_t array #5

Open
opened 3 years ago by cinderblock · 2 comments
cinderblock commented 3 years ago (Migrated from github.com)

I have image data in a uint8_t array that takes up a lot of flash space in my embedded project. It's rather simple image patterns so I suspect it should be possible to compress significantly.

At the lowest level, since C strings are basically just arrays of bytes, I feel it should be possible to use this awesome system to get the same compile time compression, but I'm not seeing the necessary syntax.

I bet I could coax my byte array into a string first and use this project directly, but with bytes often outside the ASCII range I expect that will end up rather... "ugly".

I could alternatively compress my image data ahead of time and use some runtime decompressor of my choosing, but that seems more complicated (for now).

So, is there some alternate API that I'm not seeing to use this with an array of bytes?

I have image data in a `uint8_t` array that takes up a lot of flash space in my embedded project. It's rather simple image patterns so I suspect it should be possible to compress significantly. At the lowest level, since C strings are basically just arrays of bytes, I feel it should be possible to use this awesome system to get the same compile time compression, but I'm not seeing the necessary syntax. I bet I could coax my byte array into a string first and use this project directly, but with bytes often outside the ASCII range I expect that will end up rather... "ugly". I could alternatively compress my image data ahead of time and use some runtime decompressor of my choosing, but that seems more complicated (for now). So, is there some alternate API that I'm not seeing to use this with an array of bytes?
tcsullivan commented 3 years ago (Migrated from github.com)

It looks like I never provided an easy way to use this with arrays, sorry. I've added two new methods to support this, they've been pushed so you can try them out:

// 1. Pass defined array into the compressor.
constexpr uint8_t array[] = {
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2
};
constinit auto numberData = huffman_compress<array>;

// 2. List array elements in-place.
constinit auto numberData = huffman_compress_array<uint8_t,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2,
    1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2
>;

The second option allows for a single declaration and "cleaner" syntax, though the first option compiles faster (20k elements in 10 seconds vs. 15 sec.). array is also kept out of the compiled binary. I tested on gcc 10.1 with no optimizations.

Do one of these options work for you? I'm open to any suggestions.

It looks like I never provided an easy way to use this with arrays, sorry. I've added two new methods to support this, they've been pushed so you can try them out: ```cpp // 1. Pass defined array into the compressor. constexpr uint8_t array[] = { 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2 }; constinit auto numberData = huffman_compress<array>; // 2. List array elements in-place. constinit auto numberData = huffman_compress_array<uint8_t, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2, 1, 1, 2, 3, 1, 4, 1, 2, 2, 5, 4, 7, 3, 3, 1, 2 >; ``` The second option allows for a single declaration and "cleaner" syntax, though the first option compiles faster (20k elements in 10 seconds vs. 15 sec.). `array` is also kept out of the compiled binary. I tested on gcc 10.1 with no optimizations. Do one of these options work for you? I'm open to any suggestions.
cinderblock commented 3 years ago (Migrated from github.com)

These looks great! Thank you.

I'd tried something similar but didn't find the right syntax.

I'll give them a try later today.

These looks great! Thank you. I'd tried something similar but didn't find the right syntax. I'll give them a try later today.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: clyne/consteval-huffman#5
Loading…
There is no content yet.