Skip to content

Commit

Permalink
support blosc2 meta compressors
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jun 5, 2023
1 parent d37a386 commit cf57326
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 528 deletions.
50 changes: 50 additions & 0 deletions blosc2decode.m
@@ -0,0 +1,50 @@
function varargout = blosc2decode(varargin)
%
% output = blosc2decode(input,codec)
% output = blosc2decode(input)
% or
% output = blosc2decode(input,info)
%
% Decompressing an blosc2-compressed byte-stream to recover the original data
% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
%
% authors:Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% input: a string, int8/uint8 vector or numerical array to store blosc2-compressed data
% codec: if the 2nd input is a string, it is treated as a compression method that
% blosc2 supports, it can be one of:
% 'blosc2blosclz', 'blosc2lz4', 'blosc2lz4hc', 'blosc2zlib' and 'blosc2zstd'
% if no codec is specified, 'blosc2blosclz' method is assumed
% info (optional): a struct produced by the zmat/blosc2encode function during
% compression; if not given, the inputs/outputs will be treated as a
% 1-D vector
%
% output:
% output: the decompressed byte stream stored in a uint8 vector; if info is
% given, output will restore the original data's type and dimensions
%
% examples:
% [bytes, info]=blosc2encode(eye(10));
% orig=blosc2decode(bytes,info);
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

if (nargin == 0)
error('you must provide at least 1 input');
end
if (exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3)
if (nargin >= 2 && ischar(varargin{2}))
[varargout{1:nargout}] = zmat(varargin{1}, 0, varargin{2:end});
elseif (nargin > 1)
[varargout{1:nargout}] = zmat(varargin{1}, varargin{2:end});
else
[varargout{1:nargout}] = zmat(varargin{1}, 0, 'blosc2blosclz', varargin{2:end});
end
else
error('you must install ZMat toolbox to use this feature: http://github.com/NeuroJSON/zmat');
end
43 changes: 43 additions & 0 deletions blosc2encode.m
@@ -0,0 +1,43 @@
function varargout = blosc2encode(varargin)
%
% output = blosc2encode(input)
% or
% [output, info] = blosc2encode(input)
%
% Compress a string or a numerical array using LZ4-compression
%
% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
%
% authors:Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% input: the original data, can be a string, a numerical vector or array
%
% output:
% output: the compressed byte stream stored in a uint8 vector
% info: (optional) a struct storing the metadata of the input, see "help zmat" for details
%
% examples:
% [bytes, info]=blosc2encode(eye(10));
% orig=blosc2decode(bytes,info);
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

if (nargin == 0)
error('you must provide at least 1 input');
end

if (exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3)
if (nargin > 1 && ischar(varargin{2}))
[varargout{1:nargout}] = zmat(varargin{1}, 1, varargin{2:end});
else
[varargout{1:nargout}] = zmat(varargin{1}, 1, 'blosc2blosclz', varargin{2:end});
end
return
else
error('you must install ZMat toolbox to use this feature: http://github.com/NeuroJSON/zmat');
end
14 changes: 10 additions & 4 deletions jdatadecode.m
Expand Up @@ -126,18 +126,24 @@
if(isfield(data,N_('_ArrayZipType_')))
zipmethod=data(j).(N_('_ArrayZipType_'));
end
if(ismember(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc','base64'}))
decompfun=str2func([zipmethod 'decode']);
if(ismember(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc','base64'}) || ~isempty(regexp(zipmethod,'^blosc2', 'once')))
decodeparam={};
if(~isempty(regexp(zipmethod,'^blosc2', 'once')))
decompfun=@blosc2decode;
decodeparam={zipmethod};
else
decompfun=str2func([zipmethod 'decode']);
end
arraytype=data(j).(N_('_ArrayType_'));
chartype=0;
if(strcmp(arraytype,'char') || strcmp(arraytype,'logical'))
chartype=1;
arraytype='uint8';
end
if(needbase64 && strcmp(zipmethod,'base64')==0)
ndata=reshape(typecast(decompfun(base64decode(data(j).(N_('_ArrayZipData_')))),arraytype),dims);
ndata=reshape(typecast(decompfun(base64decode(data(j).(N_('_ArrayZipData_'))),decodeparam{:}),arraytype),dims);
else
ndata=reshape(typecast(decompfun(data(j).(N_('_ArrayZipData_'))),arraytype),dims);
ndata=reshape(typecast(decompfun(data(j).(N_('_ArrayZipData_')),decodeparam{:}),arraytype),dims);
end
if(chartype)
ndata=char(ndata);
Expand Down
8 changes: 6 additions & 2 deletions jdataencode.m
Expand Up @@ -314,12 +314,16 @@
end

if(~isempty(zipmethod) && numel(item)>minsize)
compfun=str2func([zipmethod 'encode']);
compfun=str2func([regexprep(zipmethod,'blosc2[a-z0-9]+','blosc2') 'encode']);
newitem.(N('_ArrayZipType_'))=lower(zipmethod);
if(~isfield(newitem,N('_ArrayZipSize_')))
newitem.(N('_ArrayZipSize_'))=size(newitem.(N('_ArrayData_')));
end
newitem.(N('_ArrayZipData_'))=compfun(typecast(newitem.(N('_ArrayData_'))(:).','uint8'));
if(strcmp(zipmethod,'blosc2'))
newitem.(N('_ArrayZipData_'))=compfun(typecast(newitem.(N('_ArrayData_'))(:).','uint8'), zipmethod);
else
newitem.(N('_ArrayZipData_'))=compfun(typecast(newitem.(N('_ArrayData_'))(:).','uint8'));
end
newitem=rmfield(newitem,N('_ArrayData_'));
if(varargin{1}.base64)
newitem.(N('_ArrayZipData_'))=char(base64encode(newitem.(N('_ArrayZipData_'))));
Expand Down
2 changes: 1 addition & 1 deletion savebj.m
Expand Up @@ -192,7 +192,7 @@

dozip=opt.compression;
if(~isempty(dozip))
if(~ismember(dozip,{'zlib','gzip','lzma','lzip','lz4','lz4hc'}))
if (~ismember(dozip, {'zlib', 'gzip', 'lzma', 'lzip', 'lz4', 'lz4hc'}) && isempty(regexp(dozip,'^blosc2', 'once')))
error('compression method "%s" is not supported',dozip);
end
if(exist('zmat','file')~=2 && exist('zmat','file')~=3)
Expand Down

0 comments on commit cf57326

Please sign in to comment.