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
|
# coding: utf-8
#
# Project: X-ray image reader
# https://github.com/silx-kit/fabio
#
# Copyright (C) 2015 European Synchrotron Radiation Facility, Grenoble, France
#
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# 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.
"""
Cif Binary Files images are 2D images written by the Pilatus detector and others.
They use a modified (simplified) byte-offset algorithm. This file contains the
decompression function from a string to an int64 numpy array.
"""
__author__ = "Jerome Kieffer"
__contact__ = "jerome.kieffer@esrf.eu"
__license__ = "MIT"
__copyright__ = "2010-2016, European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "14/12/2020"
import numpy
import cython
from libc.stdint cimport int8_t, uint8_t, \
uint16_t, int16_t,\
int32_t, uint32_t,\
int64_t, uint64_t
@cython.boundscheck(False)
@cython.wraparound(False)
def comp_cbf32(data not None):
"""Compress a dataset using the byte-offset described for Pilatus
:param data: array of integers
:return: numpy array of chars
"""
cdef:
int32_t[::1] ary = numpy.ascontiguousarray(data.ravel(), dtype=numpy.int32)
int size = ary.size, i = 0, j = 0
int8_t[::1] output = numpy.zeros(size * 7, dtype=numpy.int8)
int32_t last, current, delta, absdelta
last = 0
for i in range(size):
current = ary[i]
delta = current - last
absdelta = delta if delta > 0 else -delta
if absdelta >= 1 << 15:
output[j] = -128
output[j + 1] = 0
output[j + 2] = -128
output[j + 3] = (delta & 255)
output[j + 4] = (delta >> 8) & 255
output[j + 5] = (delta >> 16) & 255
output[j + 6] = (delta >> 24)
j += 7
elif absdelta >= 1 << 7:
output[j] = -128
output[j + 1] = delta & 255
output[j + 2] = (delta >> 8) & 255
j += 3
else:
output[j] = delta
j += 1
last = current
return numpy.asarray(output)[:j]
@cython.boundscheck(False)
@cython.wraparound(False)
def comp_cbf(data not None):
"""Compress a dataset using the byte-offset described for any int64
:param data: array of integers
:return: numpy array of chars
"""
cdef:
int64_t[::1] ary = numpy.ascontiguousarray(data.ravel(), dtype=numpy.int64)
int size = ary.size, i = 0, j = 0
int8_t[::1] output = numpy.zeros(size * 15, dtype=numpy.int8)
int64_t last, current, delta, absdelta
last = 0
for i in range(size):
current = ary[i]
delta = current - last
absdelta = delta if delta > 0 else -delta
if absdelta >= 1 << 31:
output[j] = -128
output[j + 1] = 0
output[j + 2] = -128
output[j + 3] = 0
output[j + 4] = 0
output[j + 5] = 0
output[j + 6] = -128
output[j + 7] = (delta & 255)
output[j + 8] = (delta >> 8) & 255
output[j + 9] = (delta >> 16) & 255
output[j + 10] = (delta >> 24) & 255
output[j + 11] = (delta >> 32) & 255
output[j + 12] = (delta >> 40) & 255
output[j + 13] = (delta >> 48) & 255
output[j + 14] = (delta >> 56) & 255
j += 15
elif absdelta >= 1 << 15:
output[j] = -128
output[j + 1] = 0
output[j + 2] = -128
output[j + 3] = (delta & 255)
output[j + 4] = (delta >> 8) & 255
output[j + 5] = (delta >> 16) & 255
output[j + 6] = (delta >> 24)
j += 7
elif absdelta >= 1 << 7:
output[j] = -128
output[j + 1] = delta & 255
output[j + 2] = (delta >> 8) & 255
j += 3
else:
output[j] = delta
j += 1
last = current
return numpy.asarray(output)[:j]
@cython.boundscheck(False)
@cython.wraparound(False)
def dec_cbf(bytes stream not None, size=None):
"""
Analyze a stream of char with any length of exception (2,4, or 8 bytes integers)
:param stream: bytes (string) representing the compressed data
:param size: the size of the output array (of longInts)
:return: int64 ndArrays
"""
cdef:
int i = 0
int j = 0
uint8_t tmp8 = 0
int64_t last = 0
int64_t current = 0
int64_t tmp64 = 0
int64_t tmp64a = 0
int64_t tmp64b = 0
int64_t tmp64c = 0
int64_t tmp64d = 0
int64_t tmp64e = 0
int64_t tmp64f = 0
int64_t tmp64g = 0
uint8_t key8 = 0x80
uint8_t key0 = 0x00
int csize
int lenStream = < int > len(stream)
uint8_t[::1] cstream = bytearray(stream)
int64_t[::1] data_out
if size is None:
csize = lenStream
else:
csize = < int > size
data_out = numpy.empty(csize, dtype=numpy.int64)
with nogil:
while (i < lenStream) and (j < csize):
if (cstream[i] == key8):
if ((cstream[i + 1] == key0) and (cstream[i + 2] == key8)):
if (cstream[i + 3] == key0) and (cstream[i + 4] == key0) and (cstream[i + 5] == key0) and (cstream[i + 6] == key8):
# Retrieve the interesting Bytes of data
tmp64g = cstream[i + 7]
tmp64f = cstream[i + 8]
tmp64e = cstream[i + 9]
tmp64d = cstream[i + 10]
tmp64c = cstream[i + 11]
tmp64b = cstream[i + 12]
tmp64a = cstream[i + 13]
tmp64 = <int8_t> cstream[i + 14]
# Assemble data into a 64 bits integer
current = (tmp64 << 56) | (tmp64a << 48) | (tmp64b << 40) | (tmp64c << 32) | (tmp64d << 24) | (tmp64e << 16) | (tmp64f << 8) | (tmp64g)
i += 15
else:
# Retrieve the interesting Bytes of data
tmp64c = cstream[i + 3]
tmp64b = cstream[i + 4]
tmp64a = cstream[i + 5]
tmp64 = <int8_t> cstream[i + 6]
# Assemble data into a 64 bits integer
current = (tmp64 << 24) | (tmp64a << 16) | (tmp64b << 8) | (tmp64c)
i += 7
else:
tmp64a = cstream[i + 1]
tmp64 = <int8_t> cstream[i + 2]
current = (tmp64 << 8) | (tmp64a)
i += 3
else:
current = (<int8_t> cstream[i])
i += 1
last += current
data_out[j] = last
j += 1
return data_out[:j]
@cython.boundscheck(False)
def dec_cbf32(bytes stream not None, size=None):
"""
Analyze a stream of char with any length of exception (2 or 4 bytes integers)
Optimized for int32 decompression
:param stream: bytes (string) representing the compressed data
:param size: the size of the output array (of longInts)
:return: int64 ndArrays
"""
cdef:
int i = 0
int j = 0
uint8_t tmp8 = 0
int32_t last = 0
int32_t current = 0
int32_t tmp64 = 0
int32_t tmp64a = 0
int32_t tmp64b = 0
int32_t tmp64c = 0
uint8_t key8 = 0x80
uint8_t key0 = 0x00
int csize
int lenStream = < int > len(stream)
uint8_t[:] cstream = bytearray(stream)
int32_t[::1] data_out
if size is None:
csize = lenStream
else:
csize = < int > size
data_out = numpy.empty(csize, dtype=numpy.int32)
with nogil:
while (i < lenStream) and (j < csize):
if (cstream[i] == key8):
if ((cstream[i + 1] == key0) and (cstream[i + 2] == key8)):
# Retrieve the interesting Bytes of data
tmp64c = cstream[i + 3]
tmp64b = cstream[i + 4]
tmp64a = cstream[i + 5]
tmp64 = <int8_t> cstream[i + 6]
# Assemble data into a 32 bits integer
current = (tmp64 << 24) | (tmp64a << 16) | (tmp64b << 8) | (tmp64c)
i += 7
else:
tmp64a = cstream[i + 1]
tmp64 = <int8_t> cstream[i + 2]
current = (tmp64 << 8) | (tmp64a)
i += 3
else:
current = (<int8_t> cstream[i])
i += 1
last += current
data_out[j] = last
j += 1
return numpy.asarray(data_out[:j])
@cython.boundscheck(False)
def dec_TY5(bytes stream not None, size=None):
"""
Analyze a stream of char with a TY5 compression scheme and exception (2 or 4 bytes integers)
TODO: known broken, FIXME
:param stream: bytes (string) representing the compressed data
:param size: the size of the output array (of longInts)
:return: int32 ndArrays
"""
cdef:
int i = 0
int j = 0
int32_t last = 0
int32_t current = 0
uint8_t key8 = 0xfe # 127+127
int32_t tmp32a = 0
int32_t tmp32b = 0
int csize
int lenStream = len(stream)
uint8_t[::1] cstream = bytearray(stream)
int32_t[::1] data_out
if size is None:
csize = lenStream
else:
csize = < int > size
data_out = numpy.zeros(csize, dtype=numpy.int32)
if True:
while (i < lenStream) and (j < csize):
if (cstream[i] == key8):
tmp32a = cstream[i + 1] - 127
tmp32b = <int16_t>( <int8_t> cstream[i + 2] << 8 )
current = (tmp32b) | (tmp32a)
i += 3
else:
current = <int32_t>(<uint8_t> cstream[i]) - 127
i += 1
last += current
data_out[j] = last
j += 1
return numpy.asarray(data_out[:j])
|