|
| 1 | +/*========================================================================= |
| 2 | +
|
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +#include <fstream> |
| 20 | +#include <string> |
| 21 | +#include <iostream> |
| 22 | +#include <sstream> |
| 23 | +#include <vector> |
| 24 | +#include <iterator> |
| 25 | + |
| 26 | +#include "zstd.h" |
| 27 | +#include "cpp-base64/base64.h" |
| 28 | + |
| 29 | +#include "itkPipeline.h" |
| 30 | +#include "itkInputBinaryStream.h" |
| 31 | +#include "itkOutputTextStream.h" |
| 32 | +#include "itkOutputBinaryStream.h" |
| 33 | + |
| 34 | +int compress(itk::wasm::Pipeline & pipeline, itk::wasm::InputBinaryStream & inputBinaryStream, int compressionLevel) |
| 35 | +{ |
| 36 | + itk::wasm::OutputBinaryStream outputBinaryStream; |
| 37 | + pipeline.add_option("Output", outputBinaryStream, "Output compressed binary"); |
| 38 | + |
| 39 | + ITK_WASM_PARSE(pipeline); |
| 40 | + |
| 41 | + std::string inputBinary; |
| 42 | + inputBinary.assign( (std::istreambuf_iterator<char>(inputBinaryStream.Get()) ), |
| 43 | + (std::istreambuf_iterator<char>()) ); |
| 44 | + |
| 45 | + |
| 46 | + const size_t compressedBufferSize = ZSTD_compressBound(inputBinary.size()); |
| 47 | + std::vector<char> compressedBinary(compressedBufferSize); |
| 48 | + |
| 49 | + const size_t compressedSize = ZSTD_compress(compressedBinary.data(), compressedBufferSize, inputBinary.data(), inputBinary.size(), compressionLevel); |
| 50 | + compressedBinary.resize(compressedSize); |
| 51 | + |
| 52 | + std::ostream_iterator<char> oIt(outputBinaryStream.Get()); |
| 53 | + std::copy(compressedBinary.begin(), compressedBinary.end(), oIt); |
| 54 | + |
| 55 | + return EXIT_SUCCESS; |
| 56 | +} |
| 57 | + |
| 58 | +int compressStringify(itk::wasm::Pipeline & pipeline, itk::wasm::InputBinaryStream & inputBinaryStream, int compressionLevel, const std::string & dataURLPrefix) |
| 59 | +{ |
| 60 | + itk::wasm::OutputTextStream outputTextStream; |
| 61 | + pipeline.add_option("Output", outputTextStream, "Output dataURL+base64 compressed binary"); |
| 62 | + |
| 63 | + ITK_WASM_PARSE(pipeline); |
| 64 | + |
| 65 | + std::string inputBinary; |
| 66 | + inputBinary.assign( (std::istreambuf_iterator<char>(inputBinaryStream.Get()) ), |
| 67 | + (std::istreambuf_iterator<char>()) ); |
| 68 | + |
| 69 | + |
| 70 | + const size_t compressedBufferSize = ZSTD_compressBound(inputBinary.size()); |
| 71 | + std::string compressedBinary; |
| 72 | + compressedBinary.resize(compressedBufferSize); |
| 73 | + |
| 74 | + const size_t compressedSize = ZSTD_compress(compressedBinary.data(), compressedBufferSize, inputBinary.data(), inputBinary.size(), compressionLevel); |
| 75 | + compressedBinary.resize(compressedSize); |
| 76 | + |
| 77 | + // Do we want/need this? |
| 78 | + constexpr bool urlFriendly = false; |
| 79 | + auto outputText = base64_encode(compressedBinary, urlFriendly); |
| 80 | + |
| 81 | + outputTextStream.Get() << dataURLPrefix; |
| 82 | + outputTextStream.Get() << outputText; |
| 83 | + |
| 84 | + return EXIT_SUCCESS; |
| 85 | +} |
| 86 | + |
| 87 | +int main(int argc, char * argv[]) |
| 88 | +{ |
| 89 | + itk::wasm::Pipeline pipeline("Given a binary, compress optionally base64 encode", argc, argv); |
| 90 | + |
| 91 | + itk::wasm::InputBinaryStream inputBinaryStream; |
| 92 | + pipeline.add_option("InputBinary", inputBinaryStream, "Input binary"); |
| 93 | + |
| 94 | + bool stringify = false; |
| 95 | + pipeline.add_flag("-s,--stringify", stringify, "Stringify the output"); |
| 96 | + |
| 97 | + int compressionLevel = 3; |
| 98 | + pipeline.add_option("-c,--compression-level", compressionLevel, "Compression level, typically 1-9"); |
| 99 | + |
| 100 | + std::string dataURLPrefix("data:application/iwi+cbor+zstd;base64,"); |
| 101 | + pipeline.add_option("-p,--data-url-prefix", dataURLPrefix, "dataURL prefix"); |
| 102 | + |
| 103 | + ITK_WASM_PRE_PARSE(pipeline); |
| 104 | + |
| 105 | + if(stringify) |
| 106 | + { |
| 107 | + return compressStringify(pipeline, inputBinaryStream, compressionLevel, dataURLPrefix); |
| 108 | + } |
| 109 | + return compress(pipeline, inputBinaryStream, compressionLevel); |
| 110 | +} |
0 commit comments