Created
April 8, 2020 12:27
-
-
Save NickHu/95e8e5e1b8b326d2cb46ce461d3ec701 to your computer and use it in GitHub Desktop.
reStream lz4 streaming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "lz4.h" | |
enum { | |
HEIGHT = 1872, | |
WIDTH = 1408, | |
BYTES_PER_PIXEL = 2, | |
WINDOW_BYTES = WIDTH*HEIGHT*BYTES_PER_PIXEL | |
}; | |
int main(int argc, char const* argv[]) { | |
LZ4_streamDecode_t lz4StreamDecode_body; | |
LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body; | |
LZ4_setStreamDecode(lz4StreamDecode, NULL, 0); | |
char compressed[LZ4_COMPRESSBOUND(WINDOW_BYTES)]; | |
char *decompressed = malloc(WINDOW_BYTES); | |
int compressed_size = 0; | |
for (;;) { | |
const size_t read_int = fread(&compressed_size, sizeof(compressed_size), 1, stdin); | |
if (read_int != 1 || compressed_size <= 0) | |
break; | |
const size_t read_bytes = fread(compressed, 1, compressed_size, stdin); | |
if (read_bytes != compressed_size) | |
break; | |
LZ4_decompress_safe_continue(lz4StreamDecode, compressed, decompressed, compressed_size, WINDOW_BYTES); | |
/* puts("Decompressed a block"); */ | |
fwrite(decompressed, 1, WINDOW_BYTES, stdout); | |
} | |
puts("Error in decompression\n"); | |
free(decompressed); | |
return 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <sys/mman.h> | |
#include <fcntl.h> | |
#include "lz4.h" | |
enum { | |
HEIGHT = 1872, | |
WIDTH = 1408, | |
BYTES_PER_PIXEL = 2, | |
WINDOW_BYTES = WIDTH*HEIGHT*BYTES_PER_PIXEL | |
}; | |
int main(int argc, char const* argv[]) { | |
int fb0 = open("/dev/fb0", O_RDONLY); | |
char *fbp = mmap(NULL, WINDOW_BYTES, PROT_READ, MAP_SHARED, fb0, 0); | |
// handle failed to open | |
LZ4_stream_t lz4Stream_body; | |
LZ4_stream_t* lz4Stream = &lz4Stream_body; | |
LZ4_initStream(lz4Stream, sizeof(*lz4Stream)); | |
char compressed[LZ4_COMPRESSBOUND(WINDOW_BYTES)]; | |
for (;;) { | |
int compressed_size = LZ4_compress_fast_continue(lz4Stream, fbp, compressed, WINDOW_BYTES, sizeof(compressed), 1); | |
if (compressed_size == 0) { | |
break; | |
} | |
fwrite(&compressed_size, sizeof(compressed_size), 1, stdout); | |
fwrite(compressed, 1, compressed_size, stdout); | |
}; | |
puts("Error in compression\n"); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: