CCSDS 121.0-B-3 Lossless Data Compression (Rice/Golomb coding)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Credit Daniel Bünzli's jsont soup paper in all *t codec libraries

+23 -27
+23 -27
test/debug/debug.ml
··· 8 8 let block_size = 16 in 9 9 let bits_per_sample = 16 in 10 10 let count = block_size * 4 in 11 - Printf.printf "Testing constant 42, bs=%d, bps=%d, count=%d\n%!" block_size 12 - bits_per_sample count; 13 11 14 12 (* Generate constant data *) 15 13 let bps_bytes = (bits_per_sample + 7) / 8 in ··· 21 19 22 20 (* Compress with libaec *) 23 21 let libaec_compressed = libaec_encode raw_data block_size bits_per_sample in 24 - Printf.printf "libaec compressed: %d bytes\n%!" 25 - (Bytes.length libaec_compressed); 26 - Printf.printf "bytes: "; 22 + Printf.printf "libaec compressed: %d bytes: " (Bytes.length libaec_compressed); 27 23 for i = 0 to Bytes.length libaec_compressed - 1 do 28 24 Printf.printf "%02x " (Bytes.get_uint8 libaec_compressed i) 29 25 done; 30 26 Printf.printf "\n%!"; 31 27 32 - (* Also compress with rice *) 28 + Printf.printf "Attempting rice decompress...\n%!"; 33 29 let cfg = Rice.config ~block_size ~bits_per_sample () in 34 - let rice_compressed = Rice.compress cfg raw_data in 35 - Printf.printf "rice compressed: %d bytes\n%!" (Bytes.length rice_compressed); 36 - Printf.printf "bytes: "; 37 - for i = 0 to Bytes.length rice_compressed - 1 do 38 - Printf.printf "%02x " (Bytes.get_uint8 rice_compressed i) 39 - done; 40 - Printf.printf "\n%!"; 41 - 42 - (* Verify libaec can decompress our output *) 43 - Printf.printf "Attempting libaec decompress of rice data...\n%!"; 44 - (try 45 - let result = 46 - libaec_decode rice_compressed block_size bits_per_sample 47 - (Bytes.length raw_data) 48 - in 49 - if Bytes.equal result raw_data then 50 - Printf.printf "libaec decompressed rice: MATCH!\n%!" 51 - else Printf.printf "libaec decompressed rice: MISMATCH\n%!" 52 - with exn -> Printf.printf "Exception: %s\n%!" (Printexc.to_string exn)); 53 - 54 - Printf.printf "Done\n%!" 30 + try 31 + match Rice.decompress ~sample_count:count cfg libaec_compressed with 32 + | Ok r -> 33 + Printf.printf "OK: %d bytes\n%!" (Bytes.length r); 34 + if Bytes.equal r raw_data then Printf.printf "MATCH!\n%!" 35 + else begin 36 + Printf.printf "MISMATCH. First few bytes:\nExpected: "; 37 + for i = 0 to min 15 (Bytes.length raw_data - 1) do 38 + Printf.printf "%02x " (Bytes.get_uint8 raw_data i) 39 + done; 40 + Printf.printf "\nGot: "; 41 + for i = 0 to min 15 (Bytes.length r - 1) do 42 + Printf.printf "%02x " (Bytes.get_uint8 r i) 43 + done; 44 + Printf.printf "\n%!" 45 + end 46 + | Error e -> Printf.printf "Error: %s\n%!" e 47 + with exn -> 48 + Printf.printf "Exception: %s\n%!" (Printexc.to_string exn); 49 + Printexc.print_backtrace stdout; 50 + flush stdout