Skip to content

Commit

Permalink
Add zlib compress (which is surprisingly fast)
Browse files Browse the repository at this point in the history
  • Loading branch information
kripton committed Jun 8, 2021
1 parent dca6449 commit ffdb902
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
[submodule "lib/heatshrink"]
path = lib/heatshrink
url = https://github.com/kripton/heatshrink.git
[submodule "lib/snappy"]
path = lib/snappy
url = https://github.com/kripton/snappy.git
[submodule "lib/zstd"]
path = lib/zstd
url = https://github.com/kripton/zstd.git
[submodule "lib/zlib"]
path = lib/zlib
url = https://github.com/kripton/zlib.git
1 change: 1 addition & 0 deletions lib/snappy
Submodule snappy added at b638eb
1 change: 1 addition & 0 deletions lib/zlib
Submodule zlib added at 4773d8
1 change: 1 addition & 0 deletions lib/zstd
Submodule zstd added at 8a3bdf
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ pico_sdk_init()

## Include the CMakeLists-files of (non-pico-sdk)-libraries we will be using
include(../lib/heatshrink/CMakeLists.txt)
include(../lib/zlib/interfaceLibForPicoSDK.cmake)

## Add our own C/C++ files here
## Sorted alphabetically
add_executable(${CMAKE_PROJECT_NAME}
${TINYUSB_LIBNETWORKING_SOURCES}
algo-heatshrink.c
algo-zlib.c
main.c
)

Expand All @@ -29,6 +31,12 @@ target_compile_definitions(${CMAKE_PROJECT_NAME}
PUBLIC HEATSHRINK_STATIC_LOOKAHEAD_BITS=6
)

## Config for the zlib library to reduce memory usage
target_compile_definitions(${CMAKE_PROJECT_NAME}
PUBLIC MAX_WBITS=9
PUBLIC MAX_MEM_LEVEL=4
)

## Needed so tusb_config.h is found by the tinyUSB-library included in the pico-sdk
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}
Expand All @@ -39,6 +47,7 @@ target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
target_link_libraries(${CMAKE_PROJECT_NAME}
heatshrink
pico_stdlib
zlib
)

## enable usb output, disable uart output
Expand Down
2 changes: 1 addition & 1 deletion src/algo-heatshrink.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ void heatshrink_uncompress(uint8_t *input, size_t insize, uint8_t *output, size_
}

*outsize = polled;
}
}
20 changes: 20 additions & 0 deletions src/algo-zlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "algo-zlib.h"

#include "../lib/zlib/zlib.h"

void zlib_init()
{
}

void zlib_compress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize)
{
long unsigned int outsize2 = *outsize;
compress2(output, &outsize2, input, insize, 4);
*outsize = outsize2;
}

void zlib_uncompress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize)
{
long unsigned int outsize2 = *outsize;
uncompress(output, &outsize2, input, insize);
}
10 changes: 10 additions & 0 deletions src/algo-zlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __ALGO_ZLIB_H__
#define __ALGO_ZLIB_H__

#include "pico/stdlib.h"

void zlib_init();
void zlib_compress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize);
void zlib_uncompress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize);

#endif // __ALGO_ZLIB_H__
9 changes: 9 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "picotool_binary_information.h"

#include "algo-heatshrink.h"
#include "algo-zlib.h"


// TEST VECTORS
Expand Down Expand Up @@ -50,6 +51,7 @@ struct algo {

enum {
ALGO_HEATSHRINK,
ALGO_ZLIB,
// Add more HERE
ALGO_NUM_TOTAL
};
Expand Down Expand Up @@ -180,6 +182,13 @@ int main() {
algos[ALGO_HEATSHRINK].uncompress = heatshrink_uncompress;
algos[ALGO_HEATSHRINK].init();

// Algo 0: zlib
sprintf(algos[ALGO_ZLIB].name, "ZLIB");
algos[ALGO_ZLIB].init = zlib_init;
algos[ALGO_ZLIB].compress = zlib_compress;
algos[ALGO_ZLIB].uncompress = zlib_uncompress;
algos[ALGO_ZLIB].init();

finish_time = time_us_32();
elapsed_time = finish_time - start_time;
printf("%luµs\n", elapsed_time);
Expand Down

0 comments on commit ffdb902

Please sign in to comment.