3
A Python C extension module for decoding PackBits and LZW encoded TIFF data.
5
Refer to the tifffile.py module for documentation and tests.
7
Tested on Python 2.7 and 3.2, 32-bit and 64-bit.
10
Christoph Gohlke <http://www.lfd.uci.edu/~gohlke/>,
11
Laboratory for Fluorescence Dynamics. University of California, Irvine.
13
Copyright (c) 2008-2011, The Regents of the University of California
14
Produced by the Laboratory for Fluorescence Dynamics.
17
Redistribution and use in source and binary forms, with or without
18
modification, are permitted provided that the following conditions are met:
20
* Redistributions of source code must retain the above copyright
21
notice, this list of conditions and the following disclaimer.
22
* Redistributions in binary form must reproduce the above copyright
23
notice, this list of conditions and the following disclaimer in the
24
documentation and/or other materials provided with the distribution.
25
* Neither the name of the copyright holders nor the names of any
26
contributors may be used to endorse or promote products derived
27
from this software without specific prior written permission.
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39
POSSIBILITY OF SUCH DAMAGE.
42
/*****************************************************************************/
45
"""A Python script to build the _tifffile extension module.
47
Usage:: ``python setup.py build_ext --inplace``
51
from distutils.core import setup, Extension
54
setup(name='_tifffile', ext_modules=[Extension('_tifffile', ['tifffile.c'],
55
include_dirs=[numpy.get_include()], extra_compile_args=[])],)
58
******************************************************************************/
60
#define _VERSION_ "2011.07.07"
62
#define WIN32_LEAN_AND_MEAN
66
#include "numpy/arrayobject.h"
68
#define SWAP2BYTES(x) ((((x)>>8)&0xff) | (((x)&0xff)<<8))
69
#define SWAP4BYTES(x) ((((x)>>24)&0xff) | (((x)&0xff)<<24) | \
70
(((x)>>8)&0xff00) | (((x)&0xff00)<<8))
73
unsigned int ref; /* reference count */
74
unsigned int len; /* length of string */
75
char *str; /* pointer to bytes */
78
/*****************************************************************************/
81
/*****************************************************************************/
82
/* Python functions */
85
Decode TIFF PackBits encoded string.
87
char py_decodepackbits_doc[] = "Return TIFF PackBits decoded string.";
90
py_decodepackbits(PyObject *obj, PyObject *args)
96
char *encoded_end = NULL;
97
char *encoded_pos = NULL;
98
unsigned int encoded_len;
99
unsigned int decoded_len;
100
PyObject *byteobj = NULL;
101
PyObject *result = NULL;
103
if (!PyArg_ParseTuple(args, "O", &byteobj))
106
if (!PyBytes_Check(byteobj)) {
107
PyErr_Format(PyExc_TypeError, "expected byte string as input");
112
encoded = PyBytes_AS_STRING(byteobj);
113
encoded_len = (unsigned int)PyBytes_GET_SIZE(byteobj);
115
/* release GIL: byte/string objects are immutable */
116
Py_BEGIN_ALLOW_THREADS
118
/* determine size of decoded string */
119
encoded_pos = encoded;
120
encoded_end = encoded + encoded_len;
122
while (encoded_pos < encoded_end) {
123
n = (int)*encoded_pos++;
126
if (encoded_pos+n > encoded_end)
127
n = (int)(encoded_end - encoded_pos);
130
} else if (n > -128) {
137
result = PyBytes_FromStringAndSize(0, decoded_len);
138
if (result == NULL) {
139
PyErr_Format(PyExc_MemoryError, "failed to allocate decoded string");
142
decoded = PyBytes_AS_STRING(result);
144
Py_BEGIN_ALLOW_THREADS
147
encoded_end = encoded + encoded_len;
148
while (encoded < encoded_end) {
152
if (encoded+n > encoded_end)
153
n = (int)(encoded_end - encoded);
154
/* memmove(decoded, encoded, n); decoded += n; encoded += n; */
156
*decoded++ = *encoded++;
157
} else if (n > -128) {
160
/* memset(decoded, e, n); decoded += n; */
177
Decode TIFF LZW encoded string.
179
char py_decodelzw_doc[] = "Return TIFF LZW decoded string.";
182
py_decodelzw(PyObject *obj, PyObject *args)
184
PyThreadState *_save = NULL;
185
PyObject *byteobj = NULL;
186
PyObject *result = NULL;
188
unsigned int encoded_len = 0;
189
unsigned int decoded_len = 0;
190
unsigned int result_len = 0;
191
unsigned int table_len = 0;
193
unsigned int code, c, oldcode, mask, bitw, shr, bitcount;
194
char *encoded = NULL;
195
char *result_ptr = NULL;
198
struct BYTE_STRING *decoded = NULL;
199
struct BYTE_STRING *table[4096];
200
struct BYTE_STRING *decoded_ptr, *newentry, *newresult, *t;
201
int little_endian = 0;
203
if (!PyArg_ParseTuple(args, "O", &byteobj))
206
if (!PyBytes_Check(byteobj)) {
207
PyErr_Format(PyExc_TypeError, "expected byte string as input");
212
encoded = PyBytes_AS_STRING(byteobj);
213
encoded_len = (unsigned int)PyBytes_GET_SIZE(byteobj);
215
/* release GIL: byte/string objects are immutable */
216
_save = PyEval_SaveThread();
218
if ((*encoded != -128) || ((*(encoded+1) & 128))) {
219
PyEval_RestoreThread(_save);
220
PyErr_Format(PyExc_ValueError,
221
"strip must begin with CLEAR code");
224
little_endian = (*(unsigned short *)encoded) & 128;
226
/* allocate buffer for codes and pointers */
228
len = (encoded_len + encoded_len/9) * sizeof(decoded);
229
decoded = PyMem_Malloc(len * sizeof(void *));
230
if (decoded == NULL) {
231
PyEval_RestoreThread(_save);
232
PyErr_Format(PyExc_MemoryError, "failed to allocate decoded");
235
memset((void *)decoded, 0, len * sizeof(void *));
236
decoded_ptr = decoded;
238
/* cache strings of length 2 */
239
cptr = table2 = PyMem_Malloc(256*256*2 * sizeof(char));
240
if (table2 == NULL) {
241
PyEval_RestoreThread(_save);
242
PyErr_Format(PyExc_MemoryError, "failed to allocate table2");
245
for (i = 0; i < 256; i++) {
246
for (j = 0; j < 256; j++) {
252
memset(table, 0, sizeof(table));
262
while ((unsigned int)((bitcount + bitw) >> 3) <= encoded_len) {
264
code = *((unsigned int *)((void *)(encoded + (bitcount / 8))));
266
code = SWAP4BYTES(code);
267
code <<= bitcount % 8;
272
if (code == 257) /* end of information */
275
if (code == 256) { /* clearcode */
276
/* initialize table and switch to 9 bit */
277
while (table_len > 258) {
278
t = table[--table_len];
291
code = *((unsigned int *)((void *)(encoded + (bitcount / 8))));
293
code = SWAP4BYTES(code);
294
code <<= bitcount % 8;
299
if (code == 257) /* end of information */
302
/* decoded.append(table[code]) */
305
*((int *)decoded_ptr++) = code;
307
newresult = table[code];
309
result_len += newresult->len;
310
*(struct BYTE_STRING **)decoded_ptr++ = newresult;
313
if (code < table_len) {
314
/* code is in table */
315
/* newresult = table[code]; */
316
/* newentry = table[oldcode] + table[code][0] */
317
/* decoded.append(newresult); table.append(newentry) */
320
*((unsigned int *)decoded_ptr++) = code;
323
newresult = table[code];
325
c = (unsigned int) *newresult->str;
326
*(struct BYTE_STRING **)decoded_ptr++ = newresult;
327
result_len += newresult->len;
329
newentry = PyMem_Malloc(sizeof(struct BYTE_STRING));
333
newentry->str = table2 + (oldcode << 9) +
334
((unsigned char)c << 1);
336
len = table[oldcode]->len;
337
newentry->len = len + 1;
338
newentry->str = PyMem_Malloc(newentry->len);
339
if (newentry->str == NULL)
341
memmove(newentry->str, table[oldcode]->str, len);
342
newentry->str[len] = c;
344
table[table_len++] = newentry;
346
/* code is not in table */
347
/* newentry = newresult = table[oldcode] + table[oldcode][0] */
348
/* decoded.append(newresult); table.append(newentry) */
349
newresult = PyMem_Malloc(sizeof(struct BYTE_STRING));
350
newentry = newresult;
354
newentry->str = table2 + 514*oldcode;
356
len = table[oldcode]->len;
357
newentry->len = len + 1;
358
newentry->str = PyMem_Malloc(newentry->len);
359
if (newentry->str == NULL)
361
memmove(newentry->str, table[oldcode]->str, len);
362
newentry->str[len] = *table[oldcode]->str;
364
table[table_len++] = newentry;
365
*(struct BYTE_STRING **)decoded_ptr++ = newresult;
366
result_len += newresult->len;
370
/* increase bit-width if necessary */
389
PyEval_RestoreThread(_save);
392
PyErr_Format(PyExc_TypeError, "unexpected end of stream");
396
/* result = ''.join(decoded) */
397
decoded_len = (unsigned int)(decoded_ptr - decoded);
398
decoded_ptr = decoded;
399
result = PyBytes_FromStringAndSize(0, result_len);
400
if (result == NULL) {
401
PyErr_Format(PyExc_MemoryError, "failed to allocate decoded string");
404
result_ptr = PyBytes_AS_STRING(result);
406
_save = PyEval_SaveThread();
408
while (decoded_len--) {
409
code = *((unsigned int *)decoded_ptr);
411
*result_ptr++ = (char)code;
413
t = *((struct BYTE_STRING **)decoded_ptr);
414
memmove(result_ptr, t->str, t->len);
415
result_ptr += t->len;
426
while (table_len-- > 258) {
427
t = table[table_len];
434
PyEval_RestoreThread(_save);
442
if (decoded != NULL) {
443
while (decoded_len--) {
444
code = *((unsigned int *) decoded_ptr);
446
t = *((struct BYTE_STRING **) decoded_ptr);
456
while (table_len-- > 258) {
457
t = table[table_len];
469
/*****************************************************************************/
470
/* Create Python module */
473
"A Python C extension module for decoding PackBits and LZW encoded "
475
"Refer to the tifffile.py module for documentation and tests.\n\n"
476
"Authors:\n Christoph Gohlke <http://www.lfd.uci.edu/~gohlke/>\n"
477
" Laboratory for Fluorescence Dynamics, University of California, Irvine."
480
static PyMethodDef module_methods[] = {
481
{"decodelzw", (PyCFunction)py_decodelzw, METH_VARARGS,
483
{"decodepackbits", (PyCFunction)py_decodepackbits, METH_VARARGS,
484
py_decodepackbits_doc},
485
{NULL, NULL, 0, NULL} /* Sentinel */
488
#if PY_MAJOR_VERSION >= 3
490
struct module_state {
494
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
496
static int module_traverse(PyObject *m, visitproc visit, void *arg) {
497
Py_VISIT(GETSTATE(m)->error);
501
static int module_clear(PyObject *m) {
502
Py_CLEAR(GETSTATE(m)->error);
506
static struct PyModuleDef moduledef = {
507
PyModuleDef_HEAD_INIT,
510
sizeof(struct module_state),
518
#define INITERROR return NULL
521
PyInit__tifffile(void)
525
#define INITERROR return
534
char *doc = (char *)PyMem_Malloc(sizeof(module_doc) + sizeof(_VERSION_));
535
sprintf(doc, module_doc, _VERSION_);
537
#if PY_MAJOR_VERSION >= 3
538
moduledef.m_doc = doc;
539
module = PyModule_Create(&moduledef);
541
module = Py_InitModule3("_tifffile", module_methods, doc);
549
if (_import_array() < 0) {
555
#if PY_MAJOR_VERSION < 3
556
PyObject *s = PyString_FromString(_VERSION_);
558
PyObject *s = PyUnicode_FromString(_VERSION_);
560
PyObject *dict = PyModule_GetDict(module);
561
PyDict_SetItemString(dict, "__version__", s);
565
#if PY_MAJOR_VERSION >= 3