2013-07-01 02:42:19 +04:00
|
|
|
/*
|
2010-07-31 06:52:47 +04:00
|
|
|
* The Python Imaging Library.
|
|
|
|
*
|
|
|
|
* standard decoder interfaces for the Imaging library
|
|
|
|
*
|
|
|
|
* history:
|
|
|
|
* 1996-03-28 fl Moved from _imagingmodule.c
|
|
|
|
* 1996-04-15 fl Support subregions in setimage
|
|
|
|
* 1996-04-19 fl Allocate decoder buffer (where appropriate)
|
|
|
|
* 1996-05-02 fl Added jpeg decoder
|
|
|
|
* 1996-05-12 fl Compile cleanly as C++
|
|
|
|
* 1996-05-16 fl Added hex decoder
|
|
|
|
* 1996-05-26 fl Added jpeg configuration parameters
|
|
|
|
* 1996-12-14 fl Added zip decoder
|
|
|
|
* 1996-12-30 fl Plugged potential memory leak for tiled images
|
|
|
|
* 1997-01-03 fl Added fli and msp decoders
|
|
|
|
* 1997-01-04 fl Added sun_rle and tga_rle decoders
|
|
|
|
* 1997-05-31 fl Added bitfield decoder
|
|
|
|
* 1998-09-11 fl Added orientation and pixelsize fields to tga_rle decoder
|
|
|
|
* 1998-12-29 fl Added mode/rawmode argument to decoders
|
|
|
|
* 1998-12-30 fl Added mode argument to *all* decoders
|
|
|
|
* 2002-06-09 fl Added stride argument to pcx decoder
|
|
|
|
*
|
|
|
|
* Copyright (c) 1997-2002 by Secret Labs AB.
|
|
|
|
* Copyright (c) 1995-2002 by Fredrik Lundh.
|
|
|
|
*
|
|
|
|
* See the README file for information on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FIXME: make these pluggable! */
|
|
|
|
|
2019-03-26 05:50:57 +03:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
2010-07-31 06:52:47 +04:00
|
|
|
#include "Python.h"
|
|
|
|
|
2020-09-16 08:10:28 +03:00
|
|
|
#include "libImaging/Imaging.h"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2021-05-18 10:43:50 +03:00
|
|
|
#include "libImaging/Bit.h"
|
|
|
|
#include "libImaging/Bcn.h"
|
2020-09-22 06:33:05 +03:00
|
|
|
#include "libImaging/Gif.h"
|
|
|
|
#include "libImaging/Raw.h"
|
|
|
|
#include "libImaging/Sgi.h"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:15:24 +04:00
|
|
|
/* Common */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD int (*decode)(
|
|
|
|
Imaging im, ImagingCodecState state, UINT8 *buffer, Py_ssize_t bytes);
|
2013-05-16 08:16:37 +04:00
|
|
|
int (*cleanup)(ImagingCodecState state);
|
2010-07-31 06:52:47 +04:00
|
|
|
struct ImagingCodecStateInstance state;
|
|
|
|
Imaging im;
|
|
|
|
PyObject *lock;
|
2016-05-29 01:07:36 +03:00
|
|
|
int pulls_fd;
|
2010-07-31 06:52:47 +04:00
|
|
|
} ImagingDecoderObject;
|
|
|
|
|
2012-10-13 20:53:07 +04:00
|
|
|
static PyTypeObject ImagingDecoderType;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
static ImagingDecoderObject *
|
|
|
|
PyImaging_DecoderNew(int contextsize) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
void *context;
|
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (PyType_Ready(&ImagingDecoderType) < 0) {
|
2012-10-13 20:53:07 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyObject_New(ImagingDecoderObject, &ImagingDecoderType);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* Clear the decoder state */
|
|
|
|
memset(&decoder->state, 0, sizeof(decoder->state));
|
|
|
|
|
|
|
|
/* Allocate decoder context */
|
|
|
|
if (contextsize > 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
context = (void *)calloc(1, contextsize);
|
|
|
|
if (!context) {
|
|
|
|
Py_DECREF(decoder);
|
2020-12-20 05:00:10 +03:00
|
|
|
(void)ImagingError_MemoryError();
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-10 12:56:36 +03:00
|
|
|
} else {
|
2013-05-16 08:14:03 +04:00
|
|
|
context = 0;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* Initialize decoder context */
|
|
|
|
decoder->state.context = context;
|
|
|
|
|
|
|
|
/* Target image */
|
|
|
|
decoder->lock = NULL;
|
|
|
|
decoder->im = NULL;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-16 08:16:37 +04:00
|
|
|
/* Initialize the cleanup function pointer */
|
|
|
|
decoder->cleanup = NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2016-05-29 01:07:36 +03:00
|
|
|
/* set if the decoder needs to pull data from the fd, instead of
|
|
|
|
having it pushed */
|
|
|
|
decoder->pulls_fd = 0;
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_dealloc(ImagingDecoderObject *decoder) {
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder->cleanup) {
|
2014-08-26 07:34:44 +04:00
|
|
|
decoder->cleanup(&decoder->state);
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
free(decoder->state.buffer);
|
|
|
|
free(decoder->state.context);
|
|
|
|
Py_XDECREF(decoder->lock);
|
2016-05-29 01:07:36 +03:00
|
|
|
Py_XDECREF(decoder->state.fd);
|
2010-07-31 06:52:47 +04:00
|
|
|
PyObject_Del(decoder);
|
|
|
|
}
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
static PyObject *
|
2010-07-31 06:52:47 +04:00
|
|
|
_decode(ImagingDecoderObject *decoder, PyObject *args) {
|
2023-02-25 11:10:47 +03:00
|
|
|
Py_buffer buffer;
|
2019-03-26 05:50:57 +03:00
|
|
|
int status;
|
2015-05-10 08:10:05 +03:00
|
|
|
ImagingSectionCookie cookie;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2023-02-25 11:10:47 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "y*", &buffer)) {
|
2013-05-16 08:15:24 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2016-05-29 01:07:36 +03:00
|
|
|
if (!decoder->pulls_fd) {
|
|
|
|
ImagingSectionEnter(&cookie);
|
|
|
|
}
|
2015-05-10 08:10:05 +03:00
|
|
|
|
2023-02-25 11:10:47 +03:00
|
|
|
status = decoder->decode(decoder->im, &decoder->state, buffer.buf, buffer.len);
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2016-05-29 01:07:36 +03:00
|
|
|
if (!decoder->pulls_fd) {
|
|
|
|
ImagingSectionLeave(&cookie);
|
|
|
|
}
|
2015-05-10 08:10:05 +03:00
|
|
|
|
2023-02-27 01:48:41 +03:00
|
|
|
PyBuffer_Release(&buffer);
|
2010-07-31 06:52:47 +04:00
|
|
|
return Py_BuildValue("ii", status, decoder->state.errcode);
|
|
|
|
}
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
static PyObject *
|
2013-05-16 08:16:37 +04:00
|
|
|
_decode_cleanup(ImagingDecoderObject *decoder, PyObject *args) {
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
if (decoder->cleanup) {
|
|
|
|
status = decoder->cleanup(&decoder->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Py_BuildValue("i", status);
|
|
|
|
}
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
extern Imaging
|
|
|
|
PyImaging_AsImaging(PyObject *op);
|
2013-05-16 08:16:37 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
static PyObject *
|
|
|
|
_setimage(ImagingDecoderObject *decoder, PyObject *args) {
|
|
|
|
PyObject *op;
|
|
|
|
Imaging im;
|
|
|
|
ImagingCodecState state;
|
|
|
|
int x0, y0, x1, y1;
|
|
|
|
|
|
|
|
x0 = y0 = x1 = y1 = 0;
|
|
|
|
|
|
|
|
/* FIXME: should publish the ImagingType descriptor */
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
im = PyImaging_AsImaging(op);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!im) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->im = im;
|
|
|
|
|
|
|
|
state = &decoder->state;
|
|
|
|
|
|
|
|
/* Setup decoding tile extent */
|
|
|
|
if (x0 == 0 && x1 == 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
state->xsize = im->xsize;
|
|
|
|
state->ysize = im->ysize;
|
2010-07-31 06:52:47 +04:00
|
|
|
} else {
|
2013-05-16 08:14:03 +04:00
|
|
|
state->xoff = x0;
|
|
|
|
state->yoff = y0;
|
|
|
|
state->xsize = x1 - x0;
|
|
|
|
state->ysize = y1 - y0;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
2013-05-16 08:14:03 +04:00
|
|
|
if (state->xsize <= 0 || state->xsize + state->xoff > (int)im->xsize ||
|
|
|
|
state->ysize <= 0 || state->ysize + state->yoff > (int)im->ysize) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "tile cannot extend outside image");
|
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory buffer (if bits field is set) */
|
|
|
|
if (state->bits > 0) {
|
2016-03-16 14:47:18 +03:00
|
|
|
if (!state->bytes) {
|
2016-05-21 17:31:19 +03:00
|
|
|
if (state->xsize > ((INT_MAX / state->bits) - 7)) {
|
2020-12-20 05:00:10 +03:00
|
|
|
return ImagingError_MemoryError();
|
2016-03-16 14:47:18 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
state->bytes = (state->bits * state->xsize + 7) / 8;
|
2016-03-16 14:47:18 +03:00
|
|
|
}
|
2016-08-14 09:44:56 +03:00
|
|
|
/* malloc check ok, overflow checked above */
|
2021-04-09 14:41:07 +03:00
|
|
|
state->buffer = (UINT8 *)calloc(1, state->bytes);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!state->buffer) {
|
2020-12-20 05:00:10 +03:00
|
|
|
return ImagingError_MemoryError();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep a reference to the image object, to make sure it doesn't
|
|
|
|
go away before we do */
|
|
|
|
Py_INCREF(op);
|
|
|
|
Py_XDECREF(decoder->lock);
|
|
|
|
decoder->lock = op;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2016-05-29 01:07:36 +03:00
|
|
|
static PyObject *
|
|
|
|
_setfd(ImagingDecoderObject *decoder, PyObject *args) {
|
|
|
|
PyObject *fd;
|
|
|
|
ImagingCodecState state;
|
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "O", &fd)) {
|
2016-05-29 01:07:36 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2016-05-29 01:07:36 +03:00
|
|
|
|
|
|
|
state = &decoder->state;
|
|
|
|
|
|
|
|
Py_XINCREF(fd);
|
|
|
|
state->fd = fd;
|
2016-08-14 09:44:56 +03:00
|
|
|
|
2016-05-29 01:07:36 +03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2021-11-14 01:10:54 +03:00
|
|
|
_get_pulls_fd(ImagingDecoderObject *decoder, void *closure) {
|
2016-05-29 01:07:36 +03:00
|
|
|
return PyBool_FromLong(decoder->pulls_fd);
|
|
|
|
}
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
static struct PyMethodDef methods[] = {
|
2021-05-11 13:16:44 +03:00
|
|
|
{"decode", (PyCFunction)_decode, METH_VARARGS},
|
|
|
|
{"cleanup", (PyCFunction)_decode_cleanup, METH_VARARGS},
|
|
|
|
{"setimage", (PyCFunction)_setimage, METH_VARARGS},
|
|
|
|
{"setfd", (PyCFunction)_setfd, METH_VARARGS},
|
2010-07-31 06:52:47 +04:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2014-03-12 20:25:59 +04:00
|
|
|
static struct PyGetSetDef getseters[] = {
|
2016-05-29 01:07:36 +03:00
|
|
|
{"pulls_fd",
|
|
|
|
(getter)_get_pulls_fd,
|
|
|
|
NULL,
|
|
|
|
"True if this decoder expects to pull from self.fd itself.",
|
|
|
|
NULL},
|
2014-03-12 20:25:59 +04:00
|
|
|
{NULL, NULL, NULL, NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2012-10-13 20:53:07 +04:00
|
|
|
static PyTypeObject ImagingDecoderType = {
|
2013-05-16 08:14:03 +04:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0) "ImagingDecoder", /*tp_name*/
|
2023-07-02 08:52:08 +03:00
|
|
|
sizeof(ImagingDecoderObject), /*tp_basicsize*/
|
2013-05-16 08:14:03 +04:00
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)_dealloc, /*tp_dealloc*/
|
2023-07-02 08:52:08 +03:00
|
|
|
0, /*tp_vectorcall_offset*/
|
2012-10-13 20:53:07 +04:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
2023-07-02 08:52:08 +03:00
|
|
|
0, /*tp_as_async*/
|
2012-10-13 20:53:07 +04:00
|
|
|
0, /*tp_repr*/
|
2023-07-02 08:52:08 +03:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
2012-10-13 20:53:07 +04:00
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
|
|
0, /*tp_doc*/
|
|
|
|
0, /*tp_traverse*/
|
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
methods, /*tp_methods*/
|
|
|
|
0, /*tp_members*/
|
2014-03-12 20:25:59 +04:00
|
|
|
getseters, /*tp_getset*/
|
2010-07-31 06:52:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
get_unpacker(ImagingDecoderObject *decoder, const char *mode, const char *rawmode) {
|
|
|
|
int bits;
|
|
|
|
ImagingShuffler unpack;
|
|
|
|
|
|
|
|
unpack = ImagingFindUnpacker(mode, rawmode, &bits);
|
|
|
|
if (!unpack) {
|
2013-05-16 08:14:03 +04:00
|
|
|
Py_DECREF(decoder);
|
2021-04-30 12:36:30 +03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "unknown raw mode for given image mode");
|
2013-05-16 08:14:03 +04:00
|
|
|
return -1;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder->state.shuffle = unpack;
|
|
|
|
decoder->state.bits = bits;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* BIT (packed fields) */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_BitDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
int bits = 8;
|
|
|
|
int pad = 8;
|
|
|
|
int fill = 0;
|
|
|
|
int sign = 0;
|
|
|
|
int ystep = 1;
|
|
|
|
if (!PyArg_ParseTuple(args, "s|iiiii", &mode, &bits, &pad, &fill, &sign, &ystep)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (strcmp(mode, "F") != 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "bad image mode");
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(sizeof(BITSTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingBitDecode;
|
|
|
|
|
|
|
|
decoder->state.ystep = ystep;
|
|
|
|
|
|
|
|
((BITSTATE *)decoder->state.context)->bits = bits;
|
|
|
|
((BITSTATE *)decoder->state.context)->pad = pad;
|
|
|
|
((BITSTATE *)decoder->state.context)->fill = fill;
|
|
|
|
((BITSTATE *)decoder->state.context)->sign = sign;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
2016-08-11 02:16:32 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* BCn: GPU block-compressed texture formats */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_BcnDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *actual;
|
|
|
|
int n = 0;
|
2021-05-18 10:43:50 +03:00
|
|
|
char *pixel_format = "";
|
|
|
|
if (!PyArg_ParseTuple(args, "si|s", &mode, &n, &pixel_format)) {
|
2016-08-11 02:16:32 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2016-08-11 02:16:32 +03:00
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1: /* BC1: 565 color, 1-bit alpha */
|
|
|
|
case 2: /* BC2: 565 color, 4-bit alpha */
|
|
|
|
case 3: /* BC3: 565 color, 2-endpoint 8-bit interpolated alpha */
|
|
|
|
case 7: /* BC7: 4-channel 8-bit via everything */
|
|
|
|
actual = "RGBA";
|
|
|
|
break;
|
|
|
|
case 4: /* BC4: 1-channel 8-bit via 1 BC3 alpha block */
|
|
|
|
actual = "L";
|
|
|
|
break;
|
2021-05-17 11:57:23 +03:00
|
|
|
case 5: /* BC5: 2-channel 8-bit via 2 BC3 alpha blocks */
|
2016-08-11 02:16:32 +03:00
|
|
|
case 6: /* BC6: 3-channel 16-bit float */
|
2022-07-18 21:48:20 +03:00
|
|
|
actual = "RGB";
|
2016-08-11 02:16:32 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PyErr_SetString(PyExc_ValueError, "block compression type unknown");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(mode, actual) != 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "bad image mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:43:50 +03:00
|
|
|
decoder = PyImaging_DecoderNew(sizeof(char *));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2016-08-11 02:16:32 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2016-08-11 02:16:32 +03:00
|
|
|
|
|
|
|
decoder->decode = ImagingBcnDecode;
|
|
|
|
decoder->state.state = n;
|
2021-05-18 10:43:50 +03:00
|
|
|
((BCNSTATE *)decoder->state.context)->pixel_format = pixel_format;
|
2016-08-11 02:16:32 +03:00
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* FLI */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_FliDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingFliDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* GIF */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_GifDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
int bits = 8;
|
|
|
|
int interlace = 0;
|
2022-03-22 12:28:49 +03:00
|
|
|
int transparency = -1;
|
|
|
|
if (!PyArg_ParseTuple(args, "s|iii", &mode, &bits, &interlace, &transparency)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (strcmp(mode, "L") != 0 && strcmp(mode, "P") != 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "bad image mode");
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(sizeof(GIFDECODERSTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingGifDecode;
|
|
|
|
|
|
|
|
((GIFDECODERSTATE *)decoder->state.context)->bits = bits;
|
|
|
|
((GIFDECODERSTATE *)decoder->state.context)->interlace = interlace;
|
2022-03-22 12:28:49 +03:00
|
|
|
((GIFDECODERSTATE *)decoder->state.context)->transparency = transparency;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* HEX */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_HexDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingHexDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* LibTiff */
|
2013-03-09 07:51:59 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBTIFF
|
|
|
|
|
2020-09-22 06:33:05 +03:00
|
|
|
#include "libImaging/TiffDecode.h"
|
2013-03-09 07:51:59 +04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_LibTiffDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
|
|
|
char *compname;
|
2013-05-16 08:14:03 +04:00
|
|
|
int fp;
|
2021-04-30 17:51:39 +03:00
|
|
|
uint32_t ifdoffset;
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "sssiI", &mode, &rawmode, &compname, &fp, &ifdoffset)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2013-03-09 07:51:59 +04:00
|
|
|
|
|
|
|
TRACE(("new tiff decoder %s\n", compname));
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-09 07:51:59 +04:00
|
|
|
decoder = PyImaging_DecoderNew(sizeof(TIFFSTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-03-09 07:51:59 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-03-09 07:51:59 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2013-03-09 07:51:59 +04:00
|
|
|
|
2014-08-21 22:02:01 +04:00
|
|
|
if (!ImagingLibTiffInit(&decoder->state, fp, ifdoffset)) {
|
2013-03-09 07:51:59 +04:00
|
|
|
Py_DECREF(decoder);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "tiff codec initialization failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder->decode = ImagingLibTiffDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* PackBits */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_PackbitsDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingPackbitsDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* PCD */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_PcdDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* Unpack from PhotoYCC to RGB */
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, "RGB", "YCC;P") < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingPcdDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* PCX */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_PcxDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
|
|
|
int stride;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->state.bytes = stride;
|
|
|
|
|
|
|
|
decoder->decode = ImagingPcxDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* RAW */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_RawDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
|
|
|
int stride = 0;
|
|
|
|
int ystep = 1;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(sizeof(RAWSTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingRawDecode;
|
|
|
|
|
|
|
|
decoder->state.ystep = ystep;
|
|
|
|
|
|
|
|
((RAWSTATE *)decoder->state.context)->stride = stride;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
2017-07-22 10:34:06 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* SGI RLE */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_SgiRleDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
|
|
|
int ystep = 1;
|
2017-07-28 01:51:10 +03:00
|
|
|
int bpc = 1;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &bpc)) {
|
2017-07-22 10:34:06 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2017-07-22 10:34:06 +03:00
|
|
|
|
2017-07-28 01:51:10 +03:00
|
|
|
decoder = PyImaging_DecoderNew(sizeof(SGISTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2017-07-22 10:34:06 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2017-07-22 10:34:06 +03:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2017-07-22 10:34:06 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2017-07-22 10:34:06 +03:00
|
|
|
|
2017-07-28 22:58:17 +03:00
|
|
|
decoder->pulls_fd = 1;
|
2017-07-22 10:34:06 +03:00
|
|
|
decoder->decode = ImagingSgiRleDecode;
|
|
|
|
decoder->state.ystep = ystep;
|
2017-07-28 01:51:10 +03:00
|
|
|
|
|
|
|
((SGISTATE *)decoder->state.context)->bpc = bpc;
|
2017-07-22 10:34:06 +03:00
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* SUN RLE */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_SunRleDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingSunRleDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* TGA RLE */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_TgaRleDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
|
|
|
int ystep = 1;
|
|
|
|
int depth = 8;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingTgaRleDecode;
|
|
|
|
|
|
|
|
decoder->state.ystep = ystep;
|
|
|
|
decoder->state.count = depth / 8;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* XBM */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_XbmDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(0);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, "1", "1;R") < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingXbmDecode;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* ZIP */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
|
2020-09-22 06:33:05 +03:00
|
|
|
#include "libImaging/ZipCodecs.h"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_ZipDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *rawmode;
|
|
|
|
int interlaced = 0;
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(sizeof(ZIPSTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingZipDecode;
|
2017-05-23 17:26:13 +03:00
|
|
|
decoder->cleanup = ImagingZipDecodeCleanup;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
((ZIPSTATE *)decoder->state.context)->interlaced = interlaced;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-05-16 08:14:03 +04:00
|
|
|
/* JPEG */
|
2010-07-31 06:52:47 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBJPEG
|
|
|
|
|
|
|
|
/* We better define this decoder last in this file, so the following
|
|
|
|
undef's won't mess things up for the Imaging library proper. */
|
|
|
|
|
2013-05-16 08:14:03 +04:00
|
|
|
#undef HAVE_PROTOTYPES
|
|
|
|
#undef HAVE_STDDEF_H
|
|
|
|
#undef HAVE_STDLIB_H
|
|
|
|
#undef UINT8
|
|
|
|
#undef UINT16
|
|
|
|
#undef UINT32
|
|
|
|
#undef INT8
|
|
|
|
#undef INT16
|
|
|
|
#undef INT32
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-09-22 06:33:05 +03:00
|
|
|
#include "libImaging/Jpeg.h"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_JpegDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
|
|
|
|
|
|
|
char *mode;
|
2015-08-21 12:45:12 +03:00
|
|
|
char *rawmode; /* what we want from the decoder */
|
2010-07-31 06:52:47 +04:00
|
|
|
char *jpegmode; /* what's in the file */
|
|
|
|
int scale = 1;
|
|
|
|
int draft = 0;
|
2017-08-11 00:22:01 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, &scale, &draft)) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!jpegmode) {
|
2013-05-16 08:14:03 +04:00
|
|
|
jpegmode = "";
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2016-10-12 23:54:08 +03:00
|
|
|
// libjpeg-turbo supports different output formats.
|
|
|
|
// We are choosing Pillow's native format (3 color bytes + 1 padding)
|
|
|
|
// to avoid extra conversion in Unpack.c.
|
2017-08-11 04:07:54 +03:00
|
|
|
if (ImagingJpegUseJCSExtensions() && strcmp(rawmode, "RGB") == 0) {
|
2016-10-12 23:54:08 +03:00
|
|
|
rawmode = "RGBX";
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (get_unpacker(decoder, mode, rawmode) < 0) {
|
2013-05-16 08:14:03 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
decoder->decode = ImagingJpegDecode;
|
2013-05-16 08:16:37 +04:00
|
|
|
decoder->cleanup = ImagingJpegDecodeCleanup;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
strncpy(((JPEGSTATE *)decoder->state.context)->rawmode, rawmode, 8);
|
|
|
|
strncpy(((JPEGSTATE *)decoder->state.context)->jpegmode, jpegmode, 8);
|
|
|
|
|
|
|
|
((JPEGSTATE *)decoder->state.context)->scale = scale;
|
|
|
|
((JPEGSTATE *)decoder->state.context)->draft = draft;
|
|
|
|
|
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-12 20:25:59 +04:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2014-03-13 22:27:16 +04:00
|
|
|
/* JPEG 2000 */
|
2014-03-12 20:25:59 +04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENJPEG
|
|
|
|
|
2020-09-22 06:33:05 +03:00
|
|
|
#include "libImaging/Jpeg2K.h"
|
2014-03-12 20:25:59 +04:00
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImaging_Jpeg2KDecoderNew(PyObject *self, PyObject *args) {
|
|
|
|
ImagingDecoderObject *decoder;
|
2014-03-13 22:27:16 +04:00
|
|
|
JPEG2KDECODESTATE *context;
|
2014-03-12 20:25:59 +04:00
|
|
|
|
|
|
|
char *mode;
|
|
|
|
char *format;
|
|
|
|
OPJ_CODEC_FORMAT codec_format;
|
|
|
|
int reduce = 0;
|
|
|
|
int layers = 0;
|
2014-03-14 15:21:08 +04:00
|
|
|
int fd = -1;
|
2014-05-27 15:43:54 +04:00
|
|
|
PY_LONG_LONG length = -1;
|
2016-05-29 01:07:36 +03:00
|
|
|
|
2014-05-27 15:43:54 +04:00
|
|
|
if (!PyArg_ParseTuple(
|
2020-05-10 12:56:36 +03:00
|
|
|
args, "ss|iiiL", &mode, &format, &reduce, &layers, &fd, &length)) {
|
2014-03-12 20:25:59 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-03-12 20:25:59 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (strcmp(format, "j2k") == 0) {
|
2014-03-12 20:25:59 +04:00
|
|
|
codec_format = OPJ_CODEC_J2K;
|
2020-05-10 12:56:36 +03:00
|
|
|
} else if (strcmp(format, "jpt") == 0) {
|
2014-03-12 20:25:59 +04:00
|
|
|
codec_format = OPJ_CODEC_JPT;
|
2020-05-10 12:56:36 +03:00
|
|
|
} else if (strcmp(format, "jp2") == 0) {
|
2014-03-12 20:25:59 +04:00
|
|
|
codec_format = OPJ_CODEC_JP2;
|
2020-05-10 12:56:36 +03:00
|
|
|
} else {
|
2014-03-12 20:25:59 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-03-12 20:25:59 +04:00
|
|
|
|
2014-03-13 22:27:16 +04:00
|
|
|
decoder = PyImaging_DecoderNew(sizeof(JPEG2KDECODESTATE));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (decoder == NULL) {
|
2014-03-12 20:25:59 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-03-12 20:25:59 +04:00
|
|
|
|
2016-05-29 01:07:36 +03:00
|
|
|
decoder->pulls_fd = 1;
|
2014-03-12 20:25:59 +04:00
|
|
|
decoder->decode = ImagingJpeg2KDecode;
|
|
|
|
decoder->cleanup = ImagingJpeg2KDecodeCleanup;
|
|
|
|
|
2014-03-13 22:27:16 +04:00
|
|
|
context = (JPEG2KDECODESTATE *)decoder->state.context;
|
2016-08-14 09:44:56 +03:00
|
|
|
|
2014-03-14 15:21:08 +04:00
|
|
|
context->fd = fd;
|
2014-05-27 15:43:54 +04:00
|
|
|
context->length = (off_t)length;
|
2014-03-12 20:25:59 +04:00
|
|
|
context->format = codec_format;
|
|
|
|
context->reduce = reduce;
|
|
|
|
context->layers = layers;
|
2014-03-19 16:16:14 +04:00
|
|
|
|
2014-03-12 20:25:59 +04:00
|
|
|
return (PyObject *)decoder;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_OPENJPEG */
|