Skip to content

Commit

Permalink
smarter compression
Browse files Browse the repository at this point in the history
  • Loading branch information
xiuliren committed Dec 19, 2017
1 parent e198eb0 commit a6344eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/BigArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DATATYPE_MAP = Dict{String, DataType}(

const CODING_MAP = Dict{String,Any}(
# note that the raw encoding in cloud storage will be automatically encoded using gzip!
"raw" => RawCoding,
"raw" => GZipCoding,
"jpeg" => JPEGCoding,
"blosclz" => BlosclzCoding,
"gzip" => GZipCoding
Expand Down
8 changes: 7 additions & 1 deletion src/Codings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ abstract type AbstractBigArrayCoding end
export AbstractBigArrayCoding, JPEGCoding, RawCoding, BlosclzCoding, GZipCoding
export encode, decode

const GZIP_MAGIC_NUMBER = UInt8[0x1f, 0x8b, 0x08]

function __init__()
# use the same number of threads with Julia
if haskey(ENV, "BLOSC_NUM_THREADS")
Expand Down Expand Up @@ -43,7 +45,11 @@ function encode(data::Array, coding::Type{GZipCoding})
end

function decode(data::Vector{UInt8}, coding::Type{GZipCoding})
Libz.inflate(data)
if all(data[1:3] .== GZIP_MAGIC_NUMBER)
return Libz.inflate(data)
else
return data
end
end

function encode( data::Array, coding::Type{BlosclzCoding} )
Expand Down
17 changes: 8 additions & 9 deletions src/backends/BinDicts.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module BinDicts
using Libz
#using Libz

import ..BackendBase: AbstractBigArrayBackend, get_info, get_scale_name
export BinDict, get_info, get_scale_name
Expand All @@ -24,18 +24,17 @@ function get_scale_name( self::BinDict )
end

function Base.getindex( self::BinDict, key::AbstractString)
f = open( joinpath( get_path(self), key ))
data = read(f)
close(f)
Libz.inflate(data)
open( joinpath( get_path(self), key )) do f
return read(f)
#Libz.inflate(data)
end
end

function Base.setindex!( self::BinDict, value::Array, key::AbstractString )
data = Libz.deflate(reinterpret(UInt8, value[:]))
data = reinterpret(UInt8, value[:])
#data = Libz.deflate( data )
fileName = joinpath( get_path(self), key )
f = open(fileName, "w")
write(f, data)
close(f)
write(fileName, data)
end

end # module
4 changes: 2 additions & 2 deletions test/BinDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mkdir(tempDir)
mkdir(datasetDir)
infoString = """
{"num_channels": 1, "type": "image", "data_type": "uint8", "scales": [
{"encoding": "raw", "chunk_sizes": [[100, 100, 5]], "key": "6_6_30", "resolution": [6, 6, 30], "voxel_offset": [0, 0, 0], "size": [12286, 11262, 2046]},
{"encoding": "raw", "chunk_sizes": [[100, 100, 5]], "key": "12_12_30", "resolution": [12, 12, 30], "voxel_offset": [103, 103, 7], "size": [12286, 11262, 2046]}
{"encoding": "gzip", "chunk_sizes": [[100, 100, 5]], "key": "6_6_30", "resolution": [6, 6, 30], "voxel_offset": [0, 0, 0], "size": [12286, 11262, 2046]},
{"encoding": "gzip", "chunk_sizes": [[100, 100, 5]], "key": "12_12_30", "resolution": [12, 12, 30], "voxel_offset": [103, 103, 7], "size": [12286, 11262, 2046]}
]}
"""
open( joinpath(tempDir, "info"), "w" ) do f
Expand Down

0 comments on commit a6344eb

Please sign in to comment.