1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
# -*- coding: utf-8 -*-
#cython: embedsignature=True, language_level=3
## This is for optimisation
#cython: boundscheck=False, wraparound=False, cdivision=True, initializedcheck=False,
## This is for developping:
##cython: profile=True, warn.undeclared=True, warn.unused=True, warn.unused_result=False, warn.unused_arg=True
#
# Project: Fable Input/Output
# https://github.com/silx-kit/fabio
#
# Copyright (C) 2020-2020 European Synchrotron Radiation Facility, Grenoble, France
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Compression and decompression extension for Esperanto format
"""
__author__ = "Jérôme Kieffer"
__date__ = "13/11/2020"
__contact__ = "Jerome.kieffer@esrf.fr"
__license__ = "MIT"
from io import BytesIO
from struct import pack
import numpy
from libc.stdint cimport int32_t, int64_t, uint64_t, uint32_t, uint8_t, uint16_t, int16_t
ctypedef fused any_int:
int32_t
int64_t
cpdef int32_t fieldsize(any_int nbvalue):
"Direct translation of Fortran"
return _fieldsize(nbvalue)
cdef uint8_t _fieldsize(any_int nbvalue) nogil:
"Direct translation of Fortran"
cdef uint8_t getfieldsize
if(nbvalue < -63):
getfieldsize = 8
elif(nbvalue < -31):
getfieldsize = 7
elif(nbvalue < -15):
getfieldsize = 6
elif(nbvalue < -7):
getfieldsize = 5
elif(nbvalue < -3):
getfieldsize = 4
elif(nbvalue < -1):
getfieldsize = 3
elif(nbvalue < 0):
getfieldsize = 2
elif(nbvalue < 2):
getfieldsize = 1
elif(nbvalue < 3):
getfieldsize = 2
elif(nbvalue < 5):
getfieldsize = 3
elif(nbvalue < 9):
getfieldsize = 4
elif(nbvalue < 17):
getfieldsize = 5
elif(nbvalue < 33):
getfieldsize = 6
elif(nbvalue < 65):
getfieldsize = 7
else:
getfieldsize = 8
return getfieldsize
cpdef int32_t get_fieldsize(any_int[::1] array):
"""determine the fieldsize to store the given values
:param array numpy.array
:returns int
"""
cdef:
any_int maxi, mini, value
int32_t size, idx,
maxi = mini = 0
size = array.shape[0]
for idx in range(size):
value = array[idx]
maxi = max(maxi, value)
mini = min(mini, value)
return max(fieldsize(maxi), fieldsize(mini))
cdef uint8_t _get_fieldsize(int32_t[::1] array) nogil:
"""determine the fieldsize to store the given values
:param array numpy.array
:returns int
"""
cdef:
int32_t maxi, mini, value
int32_t size, idx,
maxi = mini = 0
size = array.shape[0]
for idx in range(size):
value = array[idx]
maxi = max(maxi, value)
mini = min(mini, value)
return max(_fieldsize(maxi), _fieldsize(mini))
cpdef write_escaped(int32_t value, buffer):
"""write an value to the buffer and escape when overflowing one byte
:param value: int
:param buffer: io.BytesIO
"""
if -127 <= value < 127:
buffer.write(pack("B", value + 127))
elif -32767 < value < 32767:
buffer.write(b'\xfe' + pack("<h", value))
else:
buffer.write(b'\xff' + pack("<i", value))
cdef uint16_t _write_escaped(int32_t value, uint8_t[::1] buffer, uint16_t position) nogil:
"""write an value to the buffer at the given position and escape when overflowing one byte
:param value: int
:param buffer: compressed line
:param position: where to start writing in the buffer
:return: now position in the vector
"""
cdef:
int16_t value_16
int32_t value_32
int64_t one=1
if -127 <= value < 127:
buffer[position] = <uint8_t> (value + 127)
return position+1
elif -32767 < value < 32767:
buffer[position] = 254
value_16 = value
buffer[position+1] = <uint8_t>(value_16 & ((one<<8)-1))
buffer[position+2] = <uint8_t>((value_16 & ((one<<16)-1))>>8)
return position+3
else:
buffer[position] = 255
buffer[position+1] = <uint8_t>((value & ((one<<8)-1)))
buffer[position+2] = <uint8_t>((value & ((one<<16)-1))>>8)
buffer[position+3] = <uint8_t>((value & ((one<<24)-1))>>16)
buffer[position+4] = <uint8_t>((value & ((one<<32)-1))>>24)
return position+5
cpdef bytes compress_field(int32_t[::1] ifield, int32_t fieldsize, overflow_table):
"""compress a field with given size
:param ifield: numpy.ndarray
:param fieldsize: int
:param overflow_table: io.BytesIO
:returns: bytes
"""
cdef:
uint64_t compressed_field, i, val, conv_
int32_t elem
if fieldsize == 8:
# we have to deal offsets but not bitshifts
tmp = bytearray(8)
for i in range(8):
elem = ifield[i]
if -127 <= elem < 127:
tmp[i] = elem + 127
elif -32767 <= elem < 32767:
tmp[i] = 254
overflow_table.write(pack("<h", elem))
else:
tmp[i] = 255
overflow_table.write(pack("<i", elem))
return bytes(tmp)
elif fieldsize > 0:
# we have to deal with bit-shifts but not offsets
conv_ = (1<<(fieldsize - 1)) - 1
compressed_field = 0
for i in range(8):
val = ifield[i] + conv_
compressed_field |= val << (i * fieldsize)
try:
res = pack("<Q", compressed_field)
except:
print("Exception in struct.pack: %s %s %s %s", fieldsize, type(fieldsize), ifield, compressed_field)
raise
return res[:fieldsize]
else:
raise AssertionError("fieldsize is between 0 and 8")
cdef uint16_t _compress_field(int32_t[::1] ifield, int32_t fieldsize, uint8_t[::1] buffer, uint16_t position, uint16_t overflow_position) nogil:
"""compress a field with given size
:param ifield: numpy.ndarray with 8 data to be compressed
:param fieldsize: int size of the field, number of bits per item
:param buffer: the output buffer with compressed data
:param position: Where to write the bitfield in the buffer
:param overflow_position: Where to write the overflow table in the buffer
:returns: position of th endd of the overflow values
"""
cdef:
uint64_t compressed_field, i, val, conv_, one
int32_t value
int16_t value_16
if fieldsize == 8:
# we have to deal offsets but not bitshifts
one = 1
for i in range(8):
value = ifield[i]
if -127 <= value < 127:
buffer[position+i] = value + 127
elif -32767 <= value < 32767:
buffer[position+i] = 254
value_16 = value
buffer[overflow_position] = <uint8_t>(value_16 & ((one<<8)-1))
buffer[overflow_position+1] = <uint8_t>((value_16 & ((one<<16)-1))>>8)
overflow_position += 2
else:
buffer[position+i] = 255
buffer[overflow_position] = <uint8_t>((value & ((one<<8)-1)))
buffer[overflow_position+1] = <uint8_t>((value & ((one<<16)-1))>>8)
buffer[overflow_position+2] = <uint8_t>((value & ((one<<24)-1))>>16)
buffer[overflow_position+3] = <uint8_t>((value & ((one<<32)-1))>>24)
overflow_position += 4
return overflow_position
elif fieldsize > 0:
# we have to deal with bit-shifts but not offsets
conv_ = (1<<(fieldsize - 1)) - 1
compressed_field = 0
for i in range(8):
val = ifield[i] + conv_
compressed_field |= val << (i * fieldsize)
for i in range(fieldsize):
buffer[position+i] = <uint8_t>((compressed_field>>(i*8)) & 255)
return overflow_position
else:
raise AssertionError("fieldsize is between 0 and 8")
def compress_row(int32_t[::1] data, buffer):
"""compress a single row
:arg data numpy.array
:arg buffer io.BytesIO
"""
cdef:
int32_t size, first_pixel, n_fields, n_restpx, len_a, len_b, cur, prev, i
int32_t[::1] fielda, fieldb, pixel_diff
size = data.shape[0]
first_pixel = data[0]
#data[1:] - data[:size-1]
pixel_diff = numpy.empty(size-1, dtype=numpy.int32)
prev = first_pixel
for i in range(1, size):
cur = data[i]
pixel_diff[i-1] = cur - prev
prev = cur
write_escaped(first_pixel, buffer)
n_fields = (size-1) // 16
n_restpx = (size-1) % 16
for i in range(n_fields):
fielda = pixel_diff[:8]
fieldb = pixel_diff[8:16]
len_a = get_fieldsize(fielda)
len_b = get_fieldsize(fieldb)
len_byte = (len_b << 4) | len_a
buffer.write(pack("B", len_byte))
of_buff = BytesIO()
compressed_fielda = compress_field(fielda, len_a, of_buff)
compressed_fieldb = compress_field(fieldb, len_b, of_buff)
buffer.write(compressed_fielda + compressed_fieldb + of_buff.getvalue())
pixel_diff = pixel_diff[16:]
for i in range(n_restpx):
write_escaped(pixel_diff[i], buffer)
cdef uint16_t _compress_row(int32_t [::1] data, uint8_t[::1] buffer, int32_t[::1] pixel_diff) nogil:
"""compress a single row
:param data: numpy.array one line of the input frame
:param buffer: buffer where to store compressed data
:return: size of the compressed line
"""
cdef:
int32_t size, first_pixel, n_fields, n_restpx, len_a, len_b, cur, prev, i
int32_t[::1] fielda, fieldb
uint16_t position, overflow_position
size = data.shape[0]
first_pixel = data[0]
#data[1:] - data[:size-1]
#pixel_diff = numpy.empty(size-1, dtype=numpy.int32)
prev = first_pixel
for i in range(1, size):
cur = data[i]
pixel_diff[i-1] = cur - prev
prev = cur
position = _write_escaped(first_pixel, buffer, 0)
n_fields = (size-1) // 16
n_restpx = (size-1) % 16
for i in range(n_fields):
fielda = pixel_diff[:8]
fieldb = pixel_diff[8:16]
len_a = _get_fieldsize(fielda)
len_b = _get_fieldsize(fieldb)
buffer[position] = (len_b << 4) | len_a
position += 1
overflow_position = _compress_field(fielda, len_a, buffer, position, position+len_a+len_b)
position = _compress_field(fieldb, len_b, buffer, position+len_a, overflow_position)
pixel_diff = pixel_diff[16:]
for i in range(n_restpx):
position = _write_escaped(pixel_diff[i], buffer, position)
return position
def compress(int32_t [:,::1] frame):
"""compress a frame using the agi_bitfield algorithm
Gil-free implementation of the compression algorithm
:param frame: numpy.ndarray
:returns bytes
"""
cdef:
Py_ssize_t shape, index
uint8_t[::1] buffer # Contains the compressed lines
uint32_t[::1] cumsum
int32_t [::1] delta # buffer space used by compre_row
uint32_t current
uint16_t size
uint64_t one=1
shape = frame.shape[0]
assert frame.shape[1] == shape, "Input shape is expected to be square !"
buffer = numpy.empty(8*shape*shape, numpy.uint8) #Should be able to accomodate 4096² data
cumsum = numpy.empty(shape, numpy.uint32)
delta = numpy.empty(shape-1, numpy.int32)
with nogil:
current = 0
for index in range(shape):
cumsum[index] = current
size = _compress_row(frame[index], buffer[4+current:], delta)
current += size
buffer[0] = current & ((one<<8)-1)
buffer[1] = (current & ((one<<16)-1))>>8
buffer[2] = (current & ((one<<24)-1))>>16
buffer[3] = (current & ((one<<32)-1))>>24
if numpy.little_endian:
return (numpy.asarray(buffer[:current+4]).tobytes()+
numpy.asarray(cumsum).tobytes())
else:
return (numpy.asarray(buffer[:current+4]).tobytes()+
numpy.asarray(cumsum).byteswap.tobytes())
|