| File: | encode.c |
| Warning: | line 131, column 11 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | |||
| 2 | * The Python Imaging Library. | |||
| 3 | * | |||
| 4 | * standard encoder interfaces for the Imaging library | |||
| 5 | * | |||
| 6 | * History: | |||
| 7 | * 1996-04-19 fl Based on decoders.c | |||
| 8 | * 1996-05-12 fl Compile cleanly as C++ | |||
| 9 | * 1996-12-30 fl Plugged potential memory leak for tiled images | |||
| 10 | * 1997-01-03 fl Added GIF encoder | |||
| 11 | * 1997-01-05 fl Plugged encoder buffer leaks | |||
| 12 | * 1997-01-11 fl Added encode_to_file method | |||
| 13 | * 1998-03-09 fl Added mode/rawmode argument to encoders | |||
| 14 | * 1998-07-09 fl Added interlace argument to GIF encoder | |||
| 15 | * 1999-02-07 fl Added PCX encoder | |||
| 16 | * | |||
| 17 | * Copyright (c) 1997-2001 by Secret Labs AB | |||
| 18 | * Copyright (c) 1996-1997 by Fredrik Lundh | |||
| 19 | * | |||
| 20 | * See the README file for information on usage and redistribution. | |||
| 21 | */ | |||
| 22 | ||||
| 23 | /* FIXME: make these pluggable! */ | |||
| 24 | ||||
| 25 | #define PY_SSIZE_T_CLEAN | |||
| 26 | #include "Python.h" | |||
| 27 | ||||
| 28 | #include "libImaging/Imaging.h" | |||
| 29 | #include "libImaging/Gif.h" | |||
| 30 | ||||
| 31 | #ifdef HAVE_UNISTD_H1 | |||
| 32 | #include <unistd.h> /* write */ | |||
| 33 | #endif | |||
| 34 | ||||
| 35 | /* -------------------------------------------------------------------- */ | |||
| 36 | /* Common */ | |||
| 37 | /* -------------------------------------------------------------------- */ | |||
| 38 | ||||
| 39 | typedef struct { | |||
| 40 | PyObject_HEADPyObject ob_base; int (*encode)( | |||
| 41 | Imaging im, ImagingCodecState state, UINT8 *buffer, int bytes); | |||
| 42 | int (*cleanup)(ImagingCodecState state); | |||
| 43 | struct ImagingCodecStateInstance state; | |||
| 44 | Imaging im; | |||
| 45 | PyObject *lock; | |||
| 46 | int pushes_fd; | |||
| 47 | } ImagingEncoderObject; | |||
| 48 | ||||
| 49 | static PyTypeObject ImagingEncoderType; | |||
| 50 | ||||
| 51 | static ImagingEncoderObject * | |||
| 52 | PyImaging_EncoderNew(int contextsize) { | |||
| 53 | ImagingEncoderObject *encoder; | |||
| 54 | void *context; | |||
| 55 | ||||
| 56 | if (PyType_Ready(&ImagingEncoderType) < 0) { | |||
| 57 | return NULL((void*)0); | |||
| 58 | } | |||
| 59 | ||||
| 60 | encoder = PyObject_New(ImagingEncoderObject, &ImagingEncoderType)( (ImagingEncoderObject *) _PyObject_New(&ImagingEncoderType ) ); | |||
| 61 | if (encoder == NULL((void*)0)) { | |||
| 62 | return NULL((void*)0); | |||
| 63 | } | |||
| 64 | ||||
| 65 | /* Clear the encoder state */ | |||
| 66 | memset(&encoder->state, 0, sizeof(encoder->state)); | |||
| 67 | ||||
| 68 | /* Allocate encoder context */ | |||
| 69 | if (contextsize > 0) { | |||
| 70 | context = (void *)calloc(1, contextsize); | |||
| 71 | if (!context) { | |||
| 72 | Py_DECREF(encoder)_Py_DECREF(((PyObject*)(encoder))); | |||
| 73 | (void)ImagingError_MemoryError(); | |||
| 74 | return NULL((void*)0); | |||
| 75 | } | |||
| 76 | } else { | |||
| 77 | context = 0; | |||
| 78 | } | |||
| 79 | ||||
| 80 | /* Initialize encoder context */ | |||
| 81 | encoder->state.context = context; | |||
| 82 | ||||
| 83 | /* Most encoders don't need this */ | |||
| 84 | encoder->cleanup = NULL((void*)0); | |||
| 85 | ||||
| 86 | /* Target image */ | |||
| 87 | encoder->lock = NULL((void*)0); | |||
| 88 | encoder->im = NULL((void*)0); | |||
| 89 | encoder->pushes_fd = 0; | |||
| 90 | ||||
| 91 | return encoder; | |||
| 92 | } | |||
| 93 | ||||
| 94 | static void | |||
| 95 | _dealloc(ImagingEncoderObject *encoder) { | |||
| 96 | if (encoder->cleanup) { | |||
| 97 | encoder->cleanup(&encoder->state); | |||
| 98 | } | |||
| 99 | free(encoder->state.buffer); | |||
| 100 | free(encoder->state.context); | |||
| 101 | Py_XDECREF(encoder->lock)_Py_XDECREF(((PyObject*)(encoder->lock))); | |||
| 102 | Py_XDECREF(encoder->state.fd)_Py_XDECREF(((PyObject*)(encoder->state.fd))); | |||
| 103 | PyObject_DelPyObject_Free(encoder); | |||
| 104 | } | |||
| 105 | ||||
| 106 | static PyObject * | |||
| 107 | _encode_cleanup(ImagingEncoderObject *encoder, PyObject *args) { | |||
| 108 | int status = 0; | |||
| 109 | ||||
| 110 | if (encoder->cleanup) { | |||
| 111 | status = encoder->cleanup(&encoder->state); | |||
| 112 | } | |||
| 113 | ||||
| 114 | return Py_BuildValue_Py_BuildValue_SizeT("i", status); | |||
| 115 | } | |||
| 116 | ||||
| 117 | static PyObject * | |||
| 118 | _encode(ImagingEncoderObject *encoder, PyObject *args) { | |||
| 119 | PyObject *buf; | |||
| 120 | PyObject *result; | |||
| 121 | int status; | |||
| 122 | ||||
| 123 | /* Encode to a Python string (allocated by this method) */ | |||
| 124 | ||||
| 125 | Py_ssize_t bufsize = 16384; | |||
| 126 | ||||
| 127 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|n", &bufsize)) { | |||
| ||||
| 128 | return NULL((void*)0); | |||
| 129 | } | |||
| 130 | ||||
| 131 | buf = PyBytes_FromStringAndSize(NULL((void*)0), bufsize); | |||
| ||||
| 132 | if (!buf) { | |||
| 133 | return NULL((void*)0); | |||
| 134 | } | |||
| 135 | ||||
| 136 | status = encoder->encode( | |||
| 137 | encoder->im, &encoder->state, (UINT8 *)PyBytes_AsString(buf), bufsize); | |||
| 138 | ||||
| 139 | /* adjust string length to avoid slicing in encoder */ | |||
| 140 | if (_PyBytes_Resize(&buf, (status > 0) ? status : 0) < 0) { | |||
| 141 | return NULL((void*)0); | |||
| 142 | } | |||
| 143 | ||||
| 144 | result = Py_BuildValue_Py_BuildValue_SizeT("iiO", status, encoder->state.errcode, buf); | |||
| 145 | ||||
| 146 | Py_DECREF(buf)_Py_DECREF(((PyObject*)(buf))); /* must release buffer!!! */ | |||
| 147 | ||||
| 148 | return result; | |||
| 149 | } | |||
| 150 | ||||
| 151 | static PyObject * | |||
| 152 | _encode_to_pyfd(ImagingEncoderObject *encoder, PyObject *args) { | |||
| 153 | PyObject *result; | |||
| 154 | int status; | |||
| 155 | ||||
| 156 | if (!encoder->pushes_fd) { | |||
| 157 | // UNDONE, appropriate errcode??? | |||
| 158 | result = Py_BuildValue_Py_BuildValue_SizeT("ii", 0, IMAGING_CODEC_CONFIG-8); | |||
| 159 | ; | |||
| 160 | return result; | |||
| 161 | } | |||
| 162 | ||||
| 163 | status = encoder->encode(encoder->im, &encoder->state, (UINT8 *)NULL((void*)0), 0); | |||
| 164 | ||||
| 165 | result = Py_BuildValue_Py_BuildValue_SizeT("ii", status, encoder->state.errcode); | |||
| 166 | ||||
| 167 | return result; | |||
| 168 | } | |||
| 169 | ||||
| 170 | static PyObject * | |||
| 171 | _encode_to_file(ImagingEncoderObject *encoder, PyObject *args) { | |||
| 172 | UINT8 *buf; | |||
| 173 | int status; | |||
| 174 | ImagingSectionCookie cookie; | |||
| 175 | ||||
| 176 | /* Encode to a file handle */ | |||
| 177 | ||||
| 178 | Py_ssize_t fh; | |||
| 179 | Py_ssize_t bufsize = 16384; | |||
| 180 | ||||
| 181 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "n|n", &fh, &bufsize)) { | |||
| 182 | return NULL((void*)0); | |||
| 183 | } | |||
| 184 | ||||
| 185 | /* Allocate an encoder buffer */ | |||
| 186 | /* malloc check ok, either constant int, or checked by PyArg_ParseTuple */ | |||
| 187 | buf = (UINT8 *)malloc(bufsize); | |||
| 188 | if (!buf) { | |||
| 189 | return ImagingError_MemoryError(); | |||
| 190 | } | |||
| 191 | ||||
| 192 | ImagingSectionEnter(&cookie); | |||
| 193 | ||||
| 194 | do { | |||
| 195 | /* This replaces the inner loop in the ImageFile _save | |||
| 196 | function. */ | |||
| 197 | ||||
| 198 | status = encoder->encode(encoder->im, &encoder->state, buf, bufsize); | |||
| 199 | ||||
| 200 | if (status > 0) { | |||
| 201 | if (write(fh, buf, status) < 0) { | |||
| 202 | ImagingSectionLeave(&cookie); | |||
| 203 | free(buf); | |||
| 204 | return PyErr_SetFromErrno(PyExc_OSError); | |||
| 205 | } | |||
| 206 | } | |||
| 207 | ||||
| 208 | } while (encoder->state.errcode == 0); | |||
| 209 | ||||
| 210 | ImagingSectionLeave(&cookie); | |||
| 211 | ||||
| 212 | free(buf); | |||
| 213 | ||||
| 214 | return Py_BuildValue_Py_BuildValue_SizeT("i", encoder->state.errcode); | |||
| 215 | } | |||
| 216 | ||||
| 217 | extern Imaging | |||
| 218 | PyImaging_AsImaging(PyObject *op); | |||
| 219 | ||||
| 220 | static PyObject * | |||
| 221 | _setimage(ImagingEncoderObject *encoder, PyObject *args) { | |||
| 222 | PyObject *op; | |||
| 223 | Imaging im; | |||
| 224 | ImagingCodecState state; | |||
| 225 | Py_ssize_t x0, y0, x1, y1; | |||
| 226 | ||||
| 227 | /* Define where image data should be stored */ | |||
| 228 | ||||
| 229 | x0 = y0 = x1 = y1 = 0; | |||
| 230 | ||||
| 231 | /* FIXME: should publish the ImagingType descriptor */ | |||
| 232 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|(nnnn)", &op, &x0, &y0, &x1, &y1)) { | |||
| 233 | return NULL((void*)0); | |||
| 234 | } | |||
| 235 | im = PyImaging_AsImaging(op); | |||
| 236 | if (!im) { | |||
| 237 | return NULL((void*)0); | |||
| 238 | } | |||
| 239 | ||||
| 240 | encoder->im = im; | |||
| 241 | ||||
| 242 | state = &encoder->state; | |||
| 243 | ||||
| 244 | if (x0 == 0 && x1 == 0) { | |||
| 245 | state->xsize = im->xsize; | |||
| 246 | state->ysize = im->ysize; | |||
| 247 | } else { | |||
| 248 | state->xoff = x0; | |||
| 249 | state->yoff = y0; | |||
| 250 | state->xsize = x1 - x0; | |||
| 251 | state->ysize = y1 - y0; | |||
| 252 | } | |||
| 253 | ||||
| 254 | if (state->xsize <= 0 || state->xsize + state->xoff > im->xsize || | |||
| 255 | state->ysize <= 0 || state->ysize + state->yoff > im->ysize) { | |||
| 256 | PyErr_SetString(PyExc_SystemError, "tile cannot extend outside image"); | |||
| 257 | return NULL((void*)0); | |||
| 258 | } | |||
| 259 | ||||
| 260 | /* Allocate memory buffer (if bits field is set) */ | |||
| 261 | if (state->bits > 0) { | |||
| 262 | if (state->xsize > ((INT_MAX2147483647 / state->bits) - 7)) { | |||
| 263 | return ImagingError_MemoryError(); | |||
| 264 | } | |||
| 265 | state->bytes = (state->bits * state->xsize + 7) / 8; | |||
| 266 | /* malloc check ok, overflow checked above */ | |||
| 267 | state->buffer = (UINT8 *)calloc(1, state->bytes); | |||
| 268 | if (!state->buffer) { | |||
| 269 | return ImagingError_MemoryError(); | |||
| 270 | } | |||
| 271 | } | |||
| 272 | ||||
| 273 | /* Keep a reference to the image object, to make sure it doesn't | |||
| 274 | go away before we do */ | |||
| 275 | Py_INCREF(op)_Py_INCREF(((PyObject*)(op))); | |||
| 276 | Py_XDECREF(encoder->lock)_Py_XDECREF(((PyObject*)(encoder->lock))); | |||
| 277 | encoder->lock = op; | |||
| 278 | ||||
| 279 | Py_INCREF(Py_None)_Py_INCREF(((PyObject*)((&_Py_NoneStruct)))); | |||
| 280 | return Py_None(&_Py_NoneStruct); | |||
| 281 | } | |||
| 282 | ||||
| 283 | static PyObject * | |||
| 284 | _setfd(ImagingEncoderObject *encoder, PyObject *args) { | |||
| 285 | PyObject *fd; | |||
| 286 | ImagingCodecState state; | |||
| 287 | ||||
| 288 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O", &fd)) { | |||
| 289 | return NULL((void*)0); | |||
| 290 | } | |||
| 291 | ||||
| 292 | state = &encoder->state; | |||
| 293 | ||||
| 294 | Py_XINCREF(fd)_Py_XINCREF(((PyObject*)(fd))); | |||
| 295 | state->fd = fd; | |||
| 296 | ||||
| 297 | Py_INCREF(Py_None)_Py_INCREF(((PyObject*)((&_Py_NoneStruct)))); | |||
| 298 | return Py_None(&_Py_NoneStruct); | |||
| 299 | } | |||
| 300 | ||||
| 301 | static PyObject * | |||
| 302 | _get_pushes_fd(ImagingEncoderObject *encoder) { | |||
| 303 | return PyBool_FromLong(encoder->pushes_fd); | |||
| 304 | } | |||
| 305 | ||||
| 306 | static struct PyMethodDef methods[] = { | |||
| 307 | {"encode", (PyCFunction)_encode, METH_VARARGS0x0001}, | |||
| 308 | {"cleanup", (PyCFunction)_encode_cleanup, METH_VARARGS0x0001}, | |||
| 309 | {"encode_to_file", (PyCFunction)_encode_to_file, METH_VARARGS0x0001}, | |||
| 310 | {"encode_to_pyfd", (PyCFunction)_encode_to_pyfd, METH_VARARGS0x0001}, | |||
| 311 | {"setimage", (PyCFunction)_setimage, METH_VARARGS0x0001}, | |||
| 312 | {"setfd", (PyCFunction)_setfd, METH_VARARGS0x0001}, | |||
| 313 | {NULL((void*)0), NULL((void*)0)} /* sentinel */ | |||
| 314 | }; | |||
| 315 | ||||
| 316 | static struct PyGetSetDef getseters[] = { | |||
| 317 | {"pushes_fd", | |||
| 318 | (getter)_get_pushes_fd, | |||
| 319 | NULL((void*)0), | |||
| 320 | "True if this decoder expects to push directly to self.fd", | |||
| 321 | NULL((void*)0)}, | |||
| 322 | {NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0)} /* sentinel */ | |||
| 323 | }; | |||
| 324 | ||||
| 325 | static PyTypeObject ImagingEncoderType = { | |||
| 326 | PyVarObject_HEAD_INIT(NULL, 0){ { 1, ((void*)0) }, 0 }, "ImagingEncoder", /*tp_name*/ | |||
| 327 | sizeof(ImagingEncoderObject), /*tp_size*/ | |||
| 328 | 0, /*tp_itemsize*/ | |||
| 329 | /* methods */ | |||
| 330 | (destructor)_dealloc, /*tp_dealloc*/ | |||
| 331 | 0, /*tp_print*/ | |||
| 332 | 0, /*tp_getattr*/ | |||
| 333 | 0, /*tp_setattr*/ | |||
| 334 | 0, /*tp_compare*/ | |||
| 335 | 0, /*tp_repr*/ | |||
| 336 | 0, /*tp_as_number */ | |||
| 337 | 0, /*tp_as_sequence */ | |||
| 338 | 0, /*tp_as_mapping */ | |||
| 339 | 0, /*tp_hash*/ | |||
| 340 | 0, /*tp_call*/ | |||
| 341 | 0, /*tp_str*/ | |||
| 342 | 0, /*tp_getattro*/ | |||
| 343 | 0, /*tp_setattro*/ | |||
| 344 | 0, /*tp_as_buffer*/ | |||
| 345 | Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), /*tp_flags*/ | |||
| 346 | 0, /*tp_doc*/ | |||
| 347 | 0, /*tp_traverse*/ | |||
| 348 | 0, /*tp_clear*/ | |||
| 349 | 0, /*tp_richcompare*/ | |||
| 350 | 0, /*tp_weaklistoffset*/ | |||
| 351 | 0, /*tp_iter*/ | |||
| 352 | 0, /*tp_iternext*/ | |||
| 353 | methods, /*tp_methods*/ | |||
| 354 | 0, /*tp_members*/ | |||
| 355 | getseters, /*tp_getset*/ | |||
| 356 | }; | |||
| 357 | ||||
| 358 | /* -------------------------------------------------------------------- */ | |||
| 359 | ||||
| 360 | int | |||
| 361 | get_packer(ImagingEncoderObject *encoder, const char *mode, const char *rawmode) { | |||
| 362 | int bits; | |||
| 363 | ImagingShuffler pack; | |||
| 364 | ||||
| 365 | pack = ImagingFindPacker(mode, rawmode, &bits); | |||
| 366 | if (!pack) { | |||
| 367 | Py_DECREF(encoder)_Py_DECREF(((PyObject*)(encoder))); | |||
| 368 | PyErr_Format(PyExc_ValueError, "No packer found from %s to %s", mode, rawmode); | |||
| 369 | return -1; | |||
| 370 | } | |||
| 371 | ||||
| 372 | encoder->state.shuffle = pack; | |||
| 373 | encoder->state.bits = bits; | |||
| 374 | ||||
| 375 | return 0; | |||
| 376 | } | |||
| 377 | ||||
| 378 | /* -------------------------------------------------------------------- */ | |||
| 379 | /* EPS */ | |||
| 380 | /* -------------------------------------------------------------------- */ | |||
| 381 | ||||
| 382 | PyObject * | |||
| 383 | PyImaging_EpsEncoderNew(PyObject *self, PyObject *args) { | |||
| 384 | ImagingEncoderObject *encoder; | |||
| 385 | ||||
| 386 | encoder = PyImaging_EncoderNew(0); | |||
| 387 | if (encoder == NULL((void*)0)) { | |||
| 388 | return NULL((void*)0); | |||
| 389 | } | |||
| 390 | ||||
| 391 | encoder->encode = ImagingEpsEncode; | |||
| 392 | ||||
| 393 | return (PyObject *)encoder; | |||
| 394 | } | |||
| 395 | ||||
| 396 | /* -------------------------------------------------------------------- */ | |||
| 397 | /* GIF */ | |||
| 398 | /* -------------------------------------------------------------------- */ | |||
| 399 | ||||
| 400 | PyObject * | |||
| 401 | PyImaging_GifEncoderNew(PyObject *self, PyObject *args) { | |||
| 402 | ImagingEncoderObject *encoder; | |||
| 403 | ||||
| 404 | char *mode; | |||
| 405 | char *rawmode; | |||
| 406 | Py_ssize_t bits = 8; | |||
| 407 | Py_ssize_t interlace = 0; | |||
| 408 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ss|nn", &mode, &rawmode, &bits, &interlace)) { | |||
| 409 | return NULL((void*)0); | |||
| 410 | } | |||
| 411 | ||||
| 412 | encoder = PyImaging_EncoderNew(sizeof(GIFENCODERSTATE)); | |||
| 413 | if (encoder == NULL((void*)0)) { | |||
| 414 | return NULL((void*)0); | |||
| 415 | } | |||
| 416 | ||||
| 417 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 418 | return NULL((void*)0); | |||
| 419 | } | |||
| 420 | ||||
| 421 | encoder->encode = ImagingGifEncode; | |||
| 422 | ||||
| 423 | ((GIFENCODERSTATE *)encoder->state.context)->bits = bits; | |||
| 424 | ((GIFENCODERSTATE *)encoder->state.context)->interlace = interlace; | |||
| 425 | ||||
| 426 | return (PyObject *)encoder; | |||
| 427 | } | |||
| 428 | ||||
| 429 | /* -------------------------------------------------------------------- */ | |||
| 430 | /* PCX */ | |||
| 431 | /* -------------------------------------------------------------------- */ | |||
| 432 | ||||
| 433 | PyObject * | |||
| 434 | PyImaging_PcxEncoderNew(PyObject *self, PyObject *args) { | |||
| 435 | ImagingEncoderObject *encoder; | |||
| 436 | ||||
| 437 | char *mode; | |||
| 438 | char *rawmode; | |||
| 439 | Py_ssize_t bits = 8; | |||
| 440 | ||||
| 441 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ss|n", &mode, &rawmode, &bits)) { | |||
| 442 | return NULL((void*)0); | |||
| 443 | } | |||
| 444 | ||||
| 445 | encoder = PyImaging_EncoderNew(0); | |||
| 446 | if (encoder == NULL((void*)0)) { | |||
| 447 | return NULL((void*)0); | |||
| 448 | } | |||
| 449 | ||||
| 450 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 451 | return NULL((void*)0); | |||
| 452 | } | |||
| 453 | ||||
| 454 | encoder->encode = ImagingPcxEncode; | |||
| 455 | ||||
| 456 | return (PyObject *)encoder; | |||
| 457 | } | |||
| 458 | ||||
| 459 | /* -------------------------------------------------------------------- */ | |||
| 460 | /* RAW */ | |||
| 461 | /* -------------------------------------------------------------------- */ | |||
| 462 | ||||
| 463 | PyObject * | |||
| 464 | PyImaging_RawEncoderNew(PyObject *self, PyObject *args) { | |||
| 465 | ImagingEncoderObject *encoder; | |||
| 466 | ||||
| 467 | char *mode; | |||
| 468 | char *rawmode; | |||
| 469 | Py_ssize_t stride = 0; | |||
| 470 | Py_ssize_t ystep = 1; | |||
| 471 | ||||
| 472 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ss|nn", &mode, &rawmode, &stride, &ystep)) { | |||
| 473 | return NULL((void*)0); | |||
| 474 | } | |||
| 475 | ||||
| 476 | encoder = PyImaging_EncoderNew(0); | |||
| 477 | if (encoder == NULL((void*)0)) { | |||
| 478 | return NULL((void*)0); | |||
| 479 | } | |||
| 480 | ||||
| 481 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 482 | return NULL((void*)0); | |||
| 483 | } | |||
| 484 | ||||
| 485 | encoder->encode = ImagingRawEncode; | |||
| 486 | ||||
| 487 | encoder->state.ystep = ystep; | |||
| 488 | encoder->state.count = stride; | |||
| 489 | ||||
| 490 | return (PyObject *)encoder; | |||
| 491 | } | |||
| 492 | ||||
| 493 | /* -------------------------------------------------------------------- */ | |||
| 494 | /* TGA */ | |||
| 495 | /* -------------------------------------------------------------------- */ | |||
| 496 | ||||
| 497 | PyObject * | |||
| 498 | PyImaging_TgaRleEncoderNew(PyObject *self, PyObject *args) { | |||
| 499 | ImagingEncoderObject *encoder; | |||
| 500 | ||||
| 501 | char *mode; | |||
| 502 | char *rawmode; | |||
| 503 | Py_ssize_t ystep = 1; | |||
| 504 | ||||
| 505 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ss|n", &mode, &rawmode, &ystep)) { | |||
| 506 | return NULL((void*)0); | |||
| 507 | } | |||
| 508 | ||||
| 509 | encoder = PyImaging_EncoderNew(0); | |||
| 510 | if (encoder == NULL((void*)0)) { | |||
| 511 | return NULL((void*)0); | |||
| 512 | } | |||
| 513 | ||||
| 514 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 515 | return NULL((void*)0); | |||
| 516 | } | |||
| 517 | ||||
| 518 | encoder->encode = ImagingTgaRleEncode; | |||
| 519 | ||||
| 520 | encoder->state.ystep = ystep; | |||
| 521 | ||||
| 522 | return (PyObject *)encoder; | |||
| 523 | } | |||
| 524 | ||||
| 525 | /* -------------------------------------------------------------------- */ | |||
| 526 | /* XBM */ | |||
| 527 | /* -------------------------------------------------------------------- */ | |||
| 528 | ||||
| 529 | PyObject * | |||
| 530 | PyImaging_XbmEncoderNew(PyObject *self, PyObject *args) { | |||
| 531 | ImagingEncoderObject *encoder; | |||
| 532 | ||||
| 533 | encoder = PyImaging_EncoderNew(0); | |||
| 534 | if (encoder == NULL((void*)0)) { | |||
| 535 | return NULL((void*)0); | |||
| 536 | } | |||
| 537 | ||||
| 538 | if (get_packer(encoder, "1", "1;R") < 0) { | |||
| 539 | return NULL((void*)0); | |||
| 540 | } | |||
| 541 | ||||
| 542 | encoder->encode = ImagingXbmEncode; | |||
| 543 | ||||
| 544 | return (PyObject *)encoder; | |||
| 545 | } | |||
| 546 | ||||
| 547 | /* -------------------------------------------------------------------- */ | |||
| 548 | /* ZIP */ | |||
| 549 | /* -------------------------------------------------------------------- */ | |||
| 550 | ||||
| 551 | #ifdef HAVE_LIBZ1 | |||
| 552 | ||||
| 553 | #include "libImaging/ZipCodecs.h" | |||
| 554 | ||||
| 555 | PyObject * | |||
| 556 | PyImaging_ZipEncoderNew(PyObject *self, PyObject *args) { | |||
| 557 | ImagingEncoderObject *encoder; | |||
| 558 | ||||
| 559 | char *mode; | |||
| 560 | char *rawmode; | |||
| 561 | Py_ssize_t optimize = 0; | |||
| 562 | Py_ssize_t compress_level = -1; | |||
| 563 | Py_ssize_t compress_type = -1; | |||
| 564 | char *dictionary = NULL((void*)0); | |||
| 565 | Py_ssize_t dictionary_size = 0; | |||
| 566 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT( | |||
| 567 | args, | |||
| 568 | "ss|nnny#", | |||
| 569 | &mode, | |||
| 570 | &rawmode, | |||
| 571 | &optimize, | |||
| 572 | &compress_level, | |||
| 573 | &compress_type, | |||
| 574 | &dictionary, | |||
| 575 | &dictionary_size)) { | |||
| 576 | return NULL((void*)0); | |||
| 577 | } | |||
| 578 | ||||
| 579 | /* Copy to avoid referencing Python's memory */ | |||
| 580 | if (dictionary && dictionary_size > 0) { | |||
| 581 | /* malloc check ok, size comes from PyArg_ParseTuple */ | |||
| 582 | char *p = malloc(dictionary_size); | |||
| 583 | if (!p) { | |||
| 584 | return ImagingError_MemoryError(); | |||
| 585 | } | |||
| 586 | memcpy(p, dictionary, dictionary_size); | |||
| 587 | dictionary = p; | |||
| 588 | } else { | |||
| 589 | dictionary = NULL((void*)0); | |||
| 590 | } | |||
| 591 | ||||
| 592 | encoder = PyImaging_EncoderNew(sizeof(ZIPSTATE)); | |||
| 593 | if (encoder == NULL((void*)0)) { | |||
| 594 | free(dictionary); | |||
| 595 | return NULL((void*)0); | |||
| 596 | } | |||
| 597 | ||||
| 598 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 599 | free(dictionary); | |||
| 600 | return NULL((void*)0); | |||
| 601 | } | |||
| 602 | ||||
| 603 | encoder->encode = ImagingZipEncode; | |||
| 604 | encoder->cleanup = ImagingZipEncodeCleanup; | |||
| 605 | ||||
| 606 | if (rawmode[0] == 'P') { | |||
| 607 | /* disable filtering */ | |||
| 608 | ((ZIPSTATE *)encoder->state.context)->mode = ZIP_PNG_PALETTE1; | |||
| 609 | } | |||
| 610 | ||||
| 611 | ((ZIPSTATE *)encoder->state.context)->optimize = optimize; | |||
| 612 | ((ZIPSTATE *)encoder->state.context)->compress_level = compress_level; | |||
| 613 | ((ZIPSTATE *)encoder->state.context)->compress_type = compress_type; | |||
| 614 | ((ZIPSTATE *)encoder->state.context)->dictionary = dictionary; | |||
| 615 | ((ZIPSTATE *)encoder->state.context)->dictionary_size = dictionary_size; | |||
| 616 | ||||
| 617 | return (PyObject *)encoder; | |||
| 618 | } | |||
| 619 | #endif | |||
| 620 | ||||
| 621 | /* -------------------------------------------------------------------- */ | |||
| 622 | /* LibTiff */ | |||
| 623 | /* -------------------------------------------------------------------- */ | |||
| 624 | ||||
| 625 | #ifdef HAVE_LIBTIFF1 | |||
| 626 | ||||
| 627 | #include "libImaging/TiffDecode.h" | |||
| 628 | ||||
| 629 | #include <string.h> | |||
| 630 | ||||
| 631 | PyObject * | |||
| 632 | PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { | |||
| 633 | ImagingEncoderObject *encoder; | |||
| 634 | ||||
| 635 | char *mode; | |||
| 636 | char *rawmode; | |||
| 637 | char *compname; | |||
| 638 | char *filename; | |||
| 639 | Py_ssize_t fp; | |||
| 640 | ||||
| 641 | PyObject *tags, *types; | |||
| 642 | PyObject *key, *value; | |||
| 643 | Py_ssize_t pos = 0; | |||
| 644 | int key_int, status, is_core_tag, is_var_length, num_core_tags, i; | |||
| 645 | TIFFDataType type = TIFF_NOTYPE; | |||
| 646 | // This list also exists in TiffTags.py | |||
| 647 | const int core_tags[] = {256, 257, 258, 259, 262, 263, 266, 269, 274, | |||
| 648 | 277, 278, 280, 281, 340, 341, 282, 283, 284, | |||
| 649 | 286, 287, 296, 297, 320, 321, 338, 32995, 32998, | |||
| 650 | 32996, 339, 32997, 330, 531, 530, 65537, 301, 532}; | |||
| 651 | ||||
| 652 | Py_ssize_t tags_size; | |||
| 653 | PyObject *item; | |||
| 654 | ||||
| 655 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT( | |||
| 656 | args, | |||
| 657 | "sssnsOO", | |||
| 658 | &mode, | |||
| 659 | &rawmode, | |||
| 660 | &compname, | |||
| 661 | &fp, | |||
| 662 | &filename, | |||
| 663 | &tags, | |||
| 664 | &types)) { | |||
| 665 | return NULL((void*)0); | |||
| 666 | } | |||
| 667 | ||||
| 668 | if (!PyList_Check(tags)((((((PyObject*)(tags))->ob_type))->tp_flags & ((1UL << 25))) != 0)) { | |||
| 669 | PyErr_SetString(PyExc_ValueError, "Invalid tags list"); | |||
| 670 | return NULL((void*)0); | |||
| 671 | } else { | |||
| 672 | tags_size = PyList_Size(tags); | |||
| 673 | TRACE(("tags size: %d\n", (int)tags_size)); | |||
| 674 | for (pos = 0; pos < tags_size; pos++) { | |||
| 675 | item = PyList_GetItem(tags, pos); | |||
| 676 | if (!PyTuple_Check(item)((((((PyObject*)(item))->ob_type))->tp_flags & ((1UL << 26))) != 0) || PyTuple_Size(item) != 2) { | |||
| 677 | PyErr_SetString(PyExc_ValueError, "Invalid tags list"); | |||
| 678 | return NULL((void*)0); | |||
| 679 | } | |||
| 680 | } | |||
| 681 | pos = 0; | |||
| 682 | } | |||
| 683 | if (!PyDict_Check(types)((((((PyObject*)(types))->ob_type))->tp_flags & ((1UL << 29))) != 0)) { | |||
| 684 | PyErr_SetString(PyExc_ValueError, "Invalid types dictionary"); | |||
| 685 | return NULL((void*)0); | |||
| 686 | } | |||
| 687 | ||||
| 688 | TRACE(("new tiff encoder %s fp: %d, filename: %s \n", compname, fp, filename)); | |||
| 689 | ||||
| 690 | encoder = PyImaging_EncoderNew(sizeof(TIFFSTATE)); | |||
| 691 | if (encoder == NULL((void*)0)) { | |||
| 692 | return NULL((void*)0); | |||
| 693 | } | |||
| 694 | ||||
| 695 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 696 | return NULL((void*)0); | |||
| 697 | } | |||
| 698 | ||||
| 699 | if (!ImagingLibTiffEncodeInit(&encoder->state, filename, fp)) { | |||
| 700 | Py_DECREF(encoder)_Py_DECREF(((PyObject*)(encoder))); | |||
| 701 | PyErr_SetString(PyExc_RuntimeError, "tiff codec initialization failed"); | |||
| 702 | return NULL((void*)0); | |||
| 703 | } | |||
| 704 | ||||
| 705 | num_core_tags = sizeof(core_tags) / sizeof(int); | |||
| 706 | for (pos = 0; pos < tags_size; pos++) { | |||
| 707 | item = PyList_GetItem(tags, pos); | |||
| 708 | // We already checked that tags is a 2-tuple list. | |||
| 709 | key = PyTuple_GetItem(item, 0); | |||
| 710 | key_int = (int)PyLong_AsLong(key); | |||
| 711 | value = PyTuple_GetItem(item, 1); | |||
| 712 | status = 0; | |||
| 713 | is_core_tag = 0; | |||
| 714 | is_var_length = 0; | |||
| 715 | type = TIFF_NOTYPE; | |||
| 716 | ||||
| 717 | for (i = 0; i < num_core_tags; i++) { | |||
| 718 | if (core_tags[i] == key_int) { | |||
| 719 | is_core_tag = 1; | |||
| 720 | break; | |||
| 721 | } | |||
| 722 | } | |||
| 723 | ||||
| 724 | if (!is_core_tag) { | |||
| 725 | PyObject *tag_type = PyDict_GetItem(types, key); | |||
| 726 | if (tag_type) { | |||
| 727 | int type_int = PyLong_AsLong(tag_type); | |||
| 728 | if (type_int >= TIFF_BYTE && type_int <= TIFF_DOUBLE) { | |||
| 729 | type = (TIFFDataType)type_int; | |||
| 730 | } | |||
| 731 | } | |||
| 732 | } | |||
| 733 | ||||
| 734 | if (type == TIFF_NOTYPE) { | |||
| 735 | // Autodetect type. Types should not be changed for backwards | |||
| 736 | // compatibility. | |||
| 737 | if (PyLong_Check(value)((((((PyObject*)(value))->ob_type))->tp_flags & ((1UL << 24))) != 0)) { | |||
| 738 | type = TIFF_LONG; | |||
| 739 | } else if (PyFloat_Check(value)((((PyObject*)(value))->ob_type) == (&PyFloat_Type) || PyType_IsSubtype((((PyObject*)(value))->ob_type), (&PyFloat_Type )))) { | |||
| 740 | type = TIFF_DOUBLE; | |||
| 741 | } else if (PyBytes_Check(value)((((((PyObject*)(value))->ob_type))->tp_flags & ((1UL << 27))) != 0)) { | |||
| 742 | type = TIFF_ASCII; | |||
| 743 | } | |||
| 744 | } | |||
| 745 | ||||
| 746 | if (PyTuple_Check(value)((((((PyObject*)(value))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
| 747 | Py_ssize_t len; | |||
| 748 | len = PyTuple_Size(value); | |||
| 749 | ||||
| 750 | is_var_length = 1; | |||
| 751 | ||||
| 752 | if (!len) { | |||
| 753 | continue; | |||
| 754 | } | |||
| 755 | ||||
| 756 | if (type == TIFF_NOTYPE) { | |||
| 757 | // Autodetect type based on first item. Types should not be | |||
| 758 | // changed for backwards compatibility. | |||
| 759 | if (PyLong_Check(PyTuple_GetItem(value, 0))((((((PyObject*)(PyTuple_GetItem(value, 0)))->ob_type))-> tp_flags & ((1UL << 24))) != 0)) { | |||
| 760 | type = TIFF_LONG; | |||
| 761 | } else if (PyFloat_Check(PyTuple_GetItem(value, 0))((((PyObject*)(PyTuple_GetItem(value, 0)))->ob_type) == (& PyFloat_Type) || PyType_IsSubtype((((PyObject*)(PyTuple_GetItem (value, 0)))->ob_type), (&PyFloat_Type)))) { | |||
| 762 | type = TIFF_FLOAT; | |||
| 763 | } | |||
| 764 | } | |||
| 765 | } | |||
| 766 | ||||
| 767 | if (!is_core_tag) { | |||
| 768 | // Register field for non core tags. | |||
| 769 | if (type == TIFF_BYTE) { | |||
| 770 | is_var_length = 1; | |||
| 771 | } | |||
| 772 | if (ImagingLibTiffMergeFieldInfo( | |||
| 773 | &encoder->state, type, key_int, is_var_length)) { | |||
| 774 | continue; | |||
| 775 | } | |||
| 776 | } | |||
| 777 | ||||
| 778 | if (type == TIFF_BYTE || type == TIFF_UNDEFINED) { | |||
| 779 | status = ImagingLibTiffSetField( | |||
| 780 | &encoder->state, | |||
| 781 | (ttag_t)key_int, | |||
| 782 | PyBytes_Size(value), | |||
| 783 | PyBytes_AsString(value)); | |||
| 784 | } else if (is_var_length) { | |||
| 785 | Py_ssize_t len, i; | |||
| 786 | TRACE(("Setting from Tuple: %d \n", key_int)); | |||
| 787 | len = PyTuple_Size(value); | |||
| 788 | ||||
| 789 | if (key_int == TIFFTAG_COLORMAP320) { | |||
| 790 | int stride = 256; | |||
| 791 | if (len != 768) { | |||
| 792 | PyErr_SetString( | |||
| 793 | PyExc_ValueError, "Requiring 768 items for Colormap"); | |||
| 794 | return NULL((void*)0); | |||
| 795 | } | |||
| 796 | UINT16 *av; | |||
| 797 | /* malloc check ok, calloc checks for overflow */ | |||
| 798 | av = calloc(len, sizeof(UINT16)); | |||
| 799 | if (av) { | |||
| 800 | for (i = 0; i < len; i++) { | |||
| 801 | av[i] = (UINT16)PyLong_AsLong(PyTuple_GetItem(value, i)); | |||
| 802 | } | |||
| 803 | status = ImagingLibTiffSetField( | |||
| 804 | &encoder->state, | |||
| 805 | (ttag_t)key_int, | |||
| 806 | av, | |||
| 807 | av + stride, | |||
| 808 | av + stride * 2); | |||
| 809 | free(av); | |||
| 810 | } | |||
| 811 | } else if (key_int == TIFFTAG_YCBCRSUBSAMPLING530) { | |||
| 812 | status = ImagingLibTiffSetField( | |||
| 813 | &encoder->state, | |||
| 814 | (ttag_t)key_int, | |||
| 815 | (UINT16)PyLong_AsLong(PyTuple_GetItem(value, 0)), | |||
| 816 | (UINT16)PyLong_AsLong(PyTuple_GetItem(value, 1))); | |||
| 817 | } else if (type == TIFF_SHORT) { | |||
| 818 | UINT16 *av; | |||
| 819 | /* malloc check ok, calloc checks for overflow */ | |||
| 820 | av = calloc(len, sizeof(UINT16)); | |||
| 821 | if (av) { | |||
| 822 | for (i = 0; i < len; i++) { | |||
| 823 | av[i] = (UINT16)PyLong_AsLong(PyTuple_GetItem(value, i)); | |||
| 824 | } | |||
| 825 | status = ImagingLibTiffSetField( | |||
| 826 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 827 | free(av); | |||
| 828 | } | |||
| 829 | } else if (type == TIFF_LONG) { | |||
| 830 | UINT32 *av; | |||
| 831 | /* malloc check ok, calloc checks for overflow */ | |||
| 832 | av = calloc(len, sizeof(UINT32)); | |||
| 833 | if (av) { | |||
| 834 | for (i = 0; i < len; i++) { | |||
| 835 | av[i] = (UINT32)PyLong_AsLong(PyTuple_GetItem(value, i)); | |||
| 836 | } | |||
| 837 | status = ImagingLibTiffSetField( | |||
| 838 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 839 | free(av); | |||
| 840 | } | |||
| 841 | } else if (type == TIFF_SBYTE) { | |||
| 842 | INT8 *av; | |||
| 843 | /* malloc check ok, calloc checks for overflow */ | |||
| 844 | av = calloc(len, sizeof(INT8)); | |||
| 845 | if (av) { | |||
| 846 | for (i = 0; i < len; i++) { | |||
| 847 | av[i] = (INT8)PyLong_AsLong(PyTuple_GetItem(value, i)); | |||
| 848 | } | |||
| 849 | status = ImagingLibTiffSetField( | |||
| 850 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 851 | free(av); | |||
| 852 | } | |||
| 853 | } else if (type == TIFF_SSHORT) { | |||
| 854 | INT16 *av; | |||
| 855 | /* malloc check ok, calloc checks for overflow */ | |||
| 856 | av = calloc(len, sizeof(INT16)); | |||
| 857 | if (av) { | |||
| 858 | for (i = 0; i < len; i++) { | |||
| 859 | av[i] = (INT16)PyLong_AsLong(PyTuple_GetItem(value, i)); | |||
| 860 | } | |||
| 861 | status = ImagingLibTiffSetField( | |||
| 862 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 863 | free(av); | |||
| 864 | } | |||
| 865 | } else if (type == TIFF_SLONG) { | |||
| 866 | INT32 *av; | |||
| 867 | /* malloc check ok, calloc checks for overflow */ | |||
| 868 | av = calloc(len, sizeof(INT32)); | |||
| 869 | if (av) { | |||
| 870 | for (i = 0; i < len; i++) { | |||
| 871 | av[i] = (INT32)PyLong_AsLong(PyTuple_GetItem(value, i)); | |||
| 872 | } | |||
| 873 | status = ImagingLibTiffSetField( | |||
| 874 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 875 | free(av); | |||
| 876 | } | |||
| 877 | } else if (type == TIFF_FLOAT) { | |||
| 878 | FLOAT32float *av; | |||
| 879 | /* malloc check ok, calloc checks for overflow */ | |||
| 880 | av = calloc(len, sizeof(FLOAT32float)); | |||
| 881 | if (av) { | |||
| 882 | for (i = 0; i < len; i++) { | |||
| 883 | av[i] = (FLOAT32float)PyFloat_AsDouble(PyTuple_GetItem(value, i)); | |||
| 884 | } | |||
| 885 | status = ImagingLibTiffSetField( | |||
| 886 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 887 | free(av); | |||
| 888 | } | |||
| 889 | } else if (type == TIFF_DOUBLE) { | |||
| 890 | FLOAT64double *av; | |||
| 891 | /* malloc check ok, calloc checks for overflow */ | |||
| 892 | av = calloc(len, sizeof(FLOAT64double)); | |||
| 893 | if (av) { | |||
| 894 | for (i = 0; i < len; i++) { | |||
| 895 | av[i] = PyFloat_AsDouble(PyTuple_GetItem(value, i)); | |||
| 896 | } | |||
| 897 | status = ImagingLibTiffSetField( | |||
| 898 | &encoder->state, (ttag_t)key_int, len, av); | |||
| 899 | free(av); | |||
| 900 | } | |||
| 901 | } | |||
| 902 | } else { | |||
| 903 | if (type == TIFF_SHORT) { | |||
| 904 | status = ImagingLibTiffSetField( | |||
| 905 | &encoder->state, (ttag_t)key_int, (UINT16)PyLong_AsLong(value)); | |||
| 906 | } else if (type == TIFF_LONG) { | |||
| 907 | status = ImagingLibTiffSetField( | |||
| 908 | &encoder->state, (ttag_t)key_int, (UINT32)PyLong_AsLong(value)); | |||
| 909 | } else if (type == TIFF_SSHORT) { | |||
| 910 | status = ImagingLibTiffSetField( | |||
| 911 | &encoder->state, (ttag_t)key_int, (INT16)PyLong_AsLong(value)); | |||
| 912 | } else if (type == TIFF_SLONG) { | |||
| 913 | status = ImagingLibTiffSetField( | |||
| 914 | &encoder->state, (ttag_t)key_int, (INT32)PyLong_AsLong(value)); | |||
| 915 | } else if (type == TIFF_FLOAT) { | |||
| 916 | status = ImagingLibTiffSetField( | |||
| 917 | &encoder->state, (ttag_t)key_int, (FLOAT32float)PyFloat_AsDouble(value)); | |||
| 918 | } else if (type == TIFF_DOUBLE) { | |||
| 919 | status = ImagingLibTiffSetField( | |||
| 920 | &encoder->state, (ttag_t)key_int, (FLOAT64double)PyFloat_AsDouble(value)); | |||
| 921 | } else if (type == TIFF_SBYTE) { | |||
| 922 | status = ImagingLibTiffSetField( | |||
| 923 | &encoder->state, (ttag_t)key_int, (INT8)PyLong_AsLong(value)); | |||
| 924 | } else if (type == TIFF_ASCII) { | |||
| 925 | status = ImagingLibTiffSetField( | |||
| 926 | &encoder->state, (ttag_t)key_int, PyBytes_AsString(value)); | |||
| 927 | } else if (type == TIFF_RATIONAL) { | |||
| 928 | status = ImagingLibTiffSetField( | |||
| 929 | &encoder->state, (ttag_t)key_int, (FLOAT64double)PyFloat_AsDouble(value)); | |||
| 930 | } else { | |||
| 931 | TRACE( | |||
| 932 | ("Unhandled type for key %d : %s \n", | |||
| 933 | key_int, | |||
| 934 | PyBytes_AsString(PyObject_Str(value)))); | |||
| 935 | } | |||
| 936 | } | |||
| 937 | if (!status) { | |||
| 938 | TRACE(("Error setting Field\n")); | |||
| 939 | Py_DECREF(encoder)_Py_DECREF(((PyObject*)(encoder))); | |||
| 940 | PyErr_SetString(PyExc_RuntimeError, "Error setting from dictionary"); | |||
| 941 | return NULL((void*)0); | |||
| 942 | } | |||
| 943 | } | |||
| 944 | ||||
| 945 | encoder->encode = ImagingLibTiffEncode; | |||
| 946 | ||||
| 947 | return (PyObject *)encoder; | |||
| 948 | } | |||
| 949 | ||||
| 950 | #endif | |||
| 951 | ||||
| 952 | /* -------------------------------------------------------------------- */ | |||
| 953 | /* JPEG */ | |||
| 954 | /* -------------------------------------------------------------------- */ | |||
| 955 | ||||
| 956 | #ifdef HAVE_LIBJPEG1 | |||
| 957 | ||||
| 958 | /* We better define this encoder last in this file, so the following | |||
| 959 | undef's won't mess things up for the Imaging library proper. */ | |||
| 960 | ||||
| 961 | #undef HAVE_PROTOTYPES | |||
| 962 | #undef HAVE_STDDEF_H | |||
| 963 | #undef HAVE_STDLIB_H | |||
| 964 | #undef UINT8 | |||
| 965 | #undef UINT16 | |||
| 966 | #undef UINT32 | |||
| 967 | #undef INT8 | |||
| 968 | #undef INT16 | |||
| 969 | #undef INT32 | |||
| 970 | ||||
| 971 | #include "libImaging/Jpeg.h" | |||
| 972 | ||||
| 973 | static unsigned int * | |||
| 974 | get_qtables_arrays(PyObject *qtables, int *qtablesLen) { | |||
| 975 | PyObject *tables; | |||
| 976 | PyObject *table; | |||
| 977 | PyObject *table_data; | |||
| 978 | int i, j, num_tables; | |||
| 979 | unsigned int *qarrays; | |||
| 980 | ||||
| 981 | if ((qtables == NULL((void*)0)) || (qtables == Py_None(&_Py_NoneStruct))) { | |||
| 982 | return NULL((void*)0); | |||
| 983 | } | |||
| 984 | ||||
| 985 | if (!PySequence_Check(qtables)) { | |||
| 986 | PyErr_SetString(PyExc_ValueError, "Invalid quantization tables"); | |||
| 987 | return NULL((void*)0); | |||
| 988 | } | |||
| 989 | ||||
| 990 | tables = PySequence_Fast(qtables, "expected a sequence"); | |||
| 991 | num_tables = PySequence_Size(qtables); | |||
| 992 | if (num_tables < 1 || num_tables > NUM_QUANT_TBLS4) { | |||
| 993 | PyErr_SetString( | |||
| 994 | PyExc_ValueError, | |||
| 995 | "Not a valid number of quantization tables. Should be between 1 and 4."); | |||
| 996 | Py_DECREF(tables)_Py_DECREF(((PyObject*)(tables))); | |||
| 997 | return NULL((void*)0); | |||
| 998 | } | |||
| 999 | /* malloc check ok, num_tables <4, DCTSIZE2 == 64 from jpeglib.h */ | |||
| 1000 | qarrays = (unsigned int *)malloc(num_tables * DCTSIZE264 * sizeof(unsigned int)); | |||
| 1001 | if (!qarrays) { | |||
| 1002 | Py_DECREF(tables)_Py_DECREF(((PyObject*)(tables))); | |||
| 1003 | return ImagingError_MemoryError(); | |||
| 1004 | } | |||
| 1005 | for (i = 0; i < num_tables; i++) { | |||
| 1006 | table = PySequence_Fast_GET_ITEM(tables, i)(((((((PyObject*)(tables))->ob_type))->tp_flags & ( (1UL << 25))) != 0) ? (((PyListObject *)(tables))->ob_item [i]) : ((((void) (0)), (PyTupleObject *)(tables))->ob_item [i])); | |||
| 1007 | if (!PySequence_Check(table)) { | |||
| 1008 | PyErr_SetString(PyExc_ValueError, "Invalid quantization tables"); | |||
| 1009 | goto JPEG_QTABLES_ERR; | |||
| 1010 | } | |||
| 1011 | if (PySequence_Size(table) != DCTSIZE264) { | |||
| 1012 | PyErr_SetString(PyExc_ValueError, "Invalid quantization table size"); | |||
| 1013 | goto JPEG_QTABLES_ERR; | |||
| 1014 | } | |||
| 1015 | table_data = PySequence_Fast(table, "expected a sequence"); | |||
| 1016 | for (j = 0; j < DCTSIZE264; j++) { | |||
| 1017 | qarrays[i * DCTSIZE264 + j] = | |||
| 1018 | PyLong_AS_LONG(PySequence_Fast_GET_ITEM(table_data, j))PyLong_AsLong((((((((PyObject*)(table_data))->ob_type))-> tp_flags & ((1UL << 25))) != 0) ? (((PyListObject * )(table_data))->ob_item[j]) : ((((void) (0)), (PyTupleObject *)(table_data))->ob_item[j]))); | |||
| 1019 | } | |||
| 1020 | Py_DECREF(table_data)_Py_DECREF(((PyObject*)(table_data))); | |||
| 1021 | } | |||
| 1022 | ||||
| 1023 | *qtablesLen = num_tables; | |||
| 1024 | ||||
| 1025 | JPEG_QTABLES_ERR: | |||
| 1026 | Py_DECREF(tables)_Py_DECREF(((PyObject*)(tables))); // Run on both error and not error | |||
| 1027 | if (PyErr_Occurred()) { | |||
| 1028 | free(qarrays); | |||
| 1029 | qarrays = NULL((void*)0); | |||
| 1030 | return NULL((void*)0); | |||
| 1031 | } | |||
| 1032 | ||||
| 1033 | return qarrays; | |||
| 1034 | } | |||
| 1035 | ||||
| 1036 | PyObject * | |||
| 1037 | PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) { | |||
| 1038 | ImagingEncoderObject *encoder; | |||
| 1039 | ||||
| 1040 | char *mode; | |||
| 1041 | char *rawmode; | |||
| 1042 | Py_ssize_t quality = 0; | |||
| 1043 | Py_ssize_t progressive = 0; | |||
| 1044 | Py_ssize_t smooth = 0; | |||
| 1045 | Py_ssize_t optimize = 0; | |||
| 1046 | Py_ssize_t streamtype = 0; /* 0=interchange, 1=tables only, 2=image only */ | |||
| 1047 | Py_ssize_t xdpi = 0, ydpi = 0; | |||
| 1048 | Py_ssize_t subsampling = -1; /* -1=default, 0=none, 1=medium, 2=high */ | |||
| 1049 | PyObject *qtables = NULL((void*)0); | |||
| 1050 | unsigned int *qarrays = NULL((void*)0); | |||
| 1051 | int qtablesLen = 0; | |||
| 1052 | char *extra = NULL((void*)0); | |||
| 1053 | Py_ssize_t extra_size; | |||
| 1054 | char *rawExif = NULL((void*)0); | |||
| 1055 | Py_ssize_t rawExifLen = 0; | |||
| 1056 | ||||
| 1057 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT( | |||
| 1058 | args, | |||
| 1059 | "ss|nnnnnnnnOy#y#", | |||
| 1060 | &mode, | |||
| 1061 | &rawmode, | |||
| 1062 | &quality, | |||
| 1063 | &progressive, | |||
| 1064 | &smooth, | |||
| 1065 | &optimize, | |||
| 1066 | &streamtype, | |||
| 1067 | &xdpi, | |||
| 1068 | &ydpi, | |||
| 1069 | &subsampling, | |||
| 1070 | &qtables, | |||
| 1071 | &extra, | |||
| 1072 | &extra_size, | |||
| 1073 | &rawExif, | |||
| 1074 | &rawExifLen)) { | |||
| 1075 | return NULL((void*)0); | |||
| 1076 | } | |||
| 1077 | ||||
| 1078 | encoder = PyImaging_EncoderNew(sizeof(JPEGENCODERSTATE)); | |||
| 1079 | if (encoder == NULL((void*)0)) { | |||
| 1080 | return NULL((void*)0); | |||
| 1081 | } | |||
| 1082 | ||||
| 1083 | // libjpeg-turbo supports different output formats. | |||
| 1084 | // We are choosing Pillow's native format (3 color bytes + 1 padding) | |||
| 1085 | // to avoid extra conversion in Pack.c. | |||
| 1086 | if (ImagingJpegUseJCSExtensions() && strcmp(rawmode, "RGB") == 0) { | |||
| 1087 | rawmode = "RGBX"; | |||
| 1088 | } | |||
| 1089 | ||||
| 1090 | if (get_packer(encoder, mode, rawmode) < 0) { | |||
| 1091 | return NULL((void*)0); | |||
| 1092 | } | |||
| 1093 | ||||
| 1094 | // Freed in JpegEncode, Case 5 | |||
| 1095 | qarrays = get_qtables_arrays(qtables, &qtablesLen); | |||
| 1096 | ||||
| 1097 | if (extra && extra_size > 0) { | |||
| 1098 | /* malloc check ok, length is from python parsearg */ | |||
| 1099 | char *p = malloc(extra_size); // Freed in JpegEncode, Case 5 | |||
| 1100 | if (!p) { | |||
| 1101 | return ImagingError_MemoryError(); | |||
| 1102 | } | |||
| 1103 | memcpy(p, extra, extra_size); | |||
| 1104 | extra = p; | |||
| 1105 | } else { | |||
| 1106 | extra = NULL((void*)0); | |||
| 1107 | } | |||
| 1108 | ||||
| 1109 | if (rawExif && rawExifLen > 0) { | |||
| 1110 | /* malloc check ok, length is from python parsearg */ | |||
| 1111 | char *pp = malloc(rawExifLen); // Freed in JpegEncode, Case 5 | |||
| 1112 | if (!pp) { | |||
| 1113 | if (extra) { | |||
| 1114 | free(extra); | |||
| 1115 | } | |||
| 1116 | return ImagingError_MemoryError(); | |||
| 1117 | } | |||
| 1118 | memcpy(pp, rawExif, rawExifLen); | |||
| 1119 | rawExif = pp; | |||
| 1120 | } else { | |||
| 1121 | rawExif = NULL((void*)0); | |||
| 1122 | } | |||
| 1123 | ||||
| 1124 | encoder->encode = ImagingJpegEncode; | |||
| 1125 | ||||
| 1126 | strncpy(((JPEGENCODERSTATE *)encoder->state.context)->rawmode, rawmode, 8); | |||
| 1127 | ||||
| 1128 | ((JPEGENCODERSTATE *)encoder->state.context)->quality = quality; | |||
| 1129 | ((JPEGENCODERSTATE *)encoder->state.context)->qtables = qarrays; | |||
| 1130 | ((JPEGENCODERSTATE *)encoder->state.context)->qtablesLen = qtablesLen; | |||
| 1131 | ((JPEGENCODERSTATE *)encoder->state.context)->subsampling = subsampling; | |||
| 1132 | ((JPEGENCODERSTATE *)encoder->state.context)->progressive = progressive; | |||
| 1133 | ((JPEGENCODERSTATE *)encoder->state.context)->smooth = smooth; | |||
| 1134 | ((JPEGENCODERSTATE *)encoder->state.context)->optimize = optimize; | |||
| 1135 | ((JPEGENCODERSTATE *)encoder->state.context)->streamtype = streamtype; | |||
| 1136 | ((JPEGENCODERSTATE *)encoder->state.context)->xdpi = xdpi; | |||
| 1137 | ((JPEGENCODERSTATE *)encoder->state.context)->ydpi = ydpi; | |||
| 1138 | ((JPEGENCODERSTATE *)encoder->state.context)->extra = extra; | |||
| 1139 | ((JPEGENCODERSTATE *)encoder->state.context)->extra_size = extra_size; | |||
| 1140 | ((JPEGENCODERSTATE *)encoder->state.context)->rawExif = rawExif; | |||
| 1141 | ((JPEGENCODERSTATE *)encoder->state.context)->rawExifLen = rawExifLen; | |||
| 1142 | ||||
| 1143 | return (PyObject *)encoder; | |||
| 1144 | } | |||
| 1145 | ||||
| 1146 | #endif | |||
| 1147 | ||||
| 1148 | /* -------------------------------------------------------------------- */ | |||
| 1149 | /* JPEG 2000 */ | |||
| 1150 | /* -------------------------------------------------------------------- */ | |||
| 1151 | ||||
| 1152 | #ifdef HAVE_OPENJPEG1 | |||
| 1153 | ||||
| 1154 | #include "libImaging/Jpeg2K.h" | |||
| 1155 | ||||
| 1156 | static void | |||
| 1157 | j2k_decode_coord_tuple(PyObject *tuple, int *x, int *y) { | |||
| 1158 | *x = *y = 0; | |||
| 1159 | ||||
| 1160 | if (tuple && PyTuple_Check(tuple)((((((PyObject*)(tuple))->ob_type))->tp_flags & ((1UL << 26))) != 0) && PyTuple_GET_SIZE(tuple)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(tuple))))-> ob_size) == 2) { | |||
| 1161 | *x = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 0)((((void) (0)), (PyTupleObject *)(tuple))->ob_item[0])); | |||
| 1162 | *y = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 1)((((void) (0)), (PyTupleObject *)(tuple))->ob_item[1])); | |||
| 1163 | ||||
| 1164 | if (*x < 0) { | |||
| 1165 | *x = 0; | |||
| 1166 | } | |||
| 1167 | if (*y < 0) { | |||
| 1168 | *y = 0; | |||
| 1169 | } | |||
| 1170 | } | |||
| 1171 | } | |||
| 1172 | ||||
| 1173 | PyObject * | |||
| 1174 | PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) { | |||
| 1175 | ImagingEncoderObject *encoder; | |||
| 1176 | JPEG2KENCODESTATE *context; | |||
| 1177 | ||||
| 1178 | char *mode; | |||
| 1179 | char *format; | |||
| 1180 | OPJ_CODEC_FORMAT codec_format; | |||
| 1181 | PyObject *offset = NULL((void*)0), *tile_offset = NULL((void*)0), *tile_size = NULL((void*)0); | |||
| 1182 | char *quality_mode = "rates"; | |||
| 1183 | PyObject *quality_layers = NULL((void*)0); | |||
| 1184 | Py_ssize_t num_resolutions = 0; | |||
| 1185 | PyObject *cblk_size = NULL((void*)0), *precinct_size = NULL((void*)0); | |||
| 1186 | PyObject *irreversible = NULL((void*)0); | |||
| 1187 | char *progression = "LRCP"; | |||
| 1188 | OPJ_PROG_ORDER prog_order; | |||
| 1189 | char *cinema_mode = "no"; | |||
| 1190 | OPJ_CINEMA_MODE cine_mode; | |||
| 1191 | Py_ssize_t fd = -1; | |||
| 1192 | ||||
| 1193 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT( | |||
| 1194 | args, | |||
| 1195 | "ss|OOOsOnOOOssn", | |||
| 1196 | &mode, | |||
| 1197 | &format, | |||
| 1198 | &offset, | |||
| 1199 | &tile_offset, | |||
| 1200 | &tile_size, | |||
| 1201 | &quality_mode, | |||
| 1202 | &quality_layers, | |||
| 1203 | &num_resolutions, | |||
| 1204 | &cblk_size, | |||
| 1205 | &precinct_size, | |||
| 1206 | &irreversible, | |||
| 1207 | &progression, | |||
| 1208 | &cinema_mode, | |||
| 1209 | &fd)) { | |||
| 1210 | return NULL((void*)0); | |||
| 1211 | } | |||
| 1212 | ||||
| 1213 | if (strcmp(format, "j2k") == 0) { | |||
| 1214 | codec_format = OPJ_CODEC_J2K; | |||
| 1215 | } else if (strcmp(format, "jpt") == 0) { | |||
| 1216 | codec_format = OPJ_CODEC_JPT; | |||
| 1217 | } else if (strcmp(format, "jp2") == 0) { | |||
| 1218 | codec_format = OPJ_CODEC_JP2; | |||
| 1219 | } else { | |||
| 1220 | return NULL((void*)0); | |||
| 1221 | } | |||
| 1222 | ||||
| 1223 | if (strcmp(progression, "LRCP") == 0) { | |||
| 1224 | prog_order = OPJ_LRCP; | |||
| 1225 | } else if (strcmp(progression, "RLCP") == 0) { | |||
| 1226 | prog_order = OPJ_RLCP; | |||
| 1227 | } else if (strcmp(progression, "RPCL") == 0) { | |||
| 1228 | prog_order = OPJ_RPCL; | |||
| 1229 | } else if (strcmp(progression, "PCRL") == 0) { | |||
| 1230 | prog_order = OPJ_PCRL; | |||
| 1231 | } else if (strcmp(progression, "CPRL") == 0) { | |||
| 1232 | prog_order = OPJ_CPRL; | |||
| 1233 | } else { | |||
| 1234 | return NULL((void*)0); | |||
| 1235 | } | |||
| 1236 | ||||
| 1237 | if (strcmp(cinema_mode, "no") == 0) { | |||
| 1238 | cine_mode = OPJ_OFF; | |||
| 1239 | } else if (strcmp(cinema_mode, "cinema2k-24") == 0) { | |||
| 1240 | cine_mode = OPJ_CINEMA2K_24; | |||
| 1241 | } else if (strcmp(cinema_mode, "cinema2k-48") == 0) { | |||
| 1242 | cine_mode = OPJ_CINEMA2K_48; | |||
| 1243 | } else if (strcmp(cinema_mode, "cinema4k-24") == 0) { | |||
| 1244 | cine_mode = OPJ_CINEMA4K_24; | |||
| 1245 | } else { | |||
| 1246 | return NULL((void*)0); | |||
| 1247 | } | |||
| 1248 | ||||
| 1249 | encoder = PyImaging_EncoderNew(sizeof(JPEG2KENCODESTATE)); | |||
| 1250 | if (!encoder) { | |||
| 1251 | return NULL((void*)0); | |||
| 1252 | } | |||
| 1253 | ||||
| 1254 | encoder->encode = ImagingJpeg2KEncode; | |||
| 1255 | encoder->cleanup = ImagingJpeg2KEncodeCleanup; | |||
| 1256 | encoder->pushes_fd = 1; | |||
| 1257 | ||||
| 1258 | context = (JPEG2KENCODESTATE *)encoder->state.context; | |||
| 1259 | ||||
| 1260 | context->fd = fd; | |||
| 1261 | context->format = codec_format; | |||
| 1262 | context->offset_x = context->offset_y = 0; | |||
| 1263 | ||||
| 1264 | j2k_decode_coord_tuple(offset, &context->offset_x, &context->offset_y); | |||
| 1265 | j2k_decode_coord_tuple( | |||
| 1266 | tile_offset, &context->tile_offset_x, &context->tile_offset_y); | |||
| 1267 | j2k_decode_coord_tuple(tile_size, &context->tile_size_x, &context->tile_size_y); | |||
| 1268 | ||||
| 1269 | /* Error on illegal tile offsets */ | |||
| 1270 | if (context->tile_size_x && context->tile_size_y) { | |||
| 1271 | if (context->tile_offset_x <= context->offset_x - context->tile_size_x || | |||
| 1272 | context->tile_offset_y <= context->offset_y - context->tile_size_y) { | |||
| 1273 | PyErr_SetString( | |||
| 1274 | PyExc_ValueError, | |||
| 1275 | "JPEG 2000 tile offset too small; top left tile must " | |||
| 1276 | "intersect image area"); | |||
| 1277 | Py_DECREF(encoder)_Py_DECREF(((PyObject*)(encoder))); | |||
| 1278 | return NULL((void*)0); | |||
| 1279 | } | |||
| 1280 | ||||
| 1281 | if (context->tile_offset_x > context->offset_x || | |||
| 1282 | context->tile_offset_y > context->offset_y) { | |||
| 1283 | PyErr_SetString( | |||
| 1284 | PyExc_ValueError, | |||
| 1285 | "JPEG 2000 tile offset too large to cover image area"); | |||
| 1286 | Py_DECREF(encoder)_Py_DECREF(((PyObject*)(encoder))); | |||
| 1287 | return NULL((void*)0); | |||
| 1288 | } | |||
| 1289 | } | |||
| 1290 | ||||
| 1291 | if (quality_layers && PySequence_Check(quality_layers)) { | |||
| 1292 | context->quality_is_in_db = strcmp(quality_mode, "dB") == 0; | |||
| 1293 | context->quality_layers = quality_layers; | |||
| 1294 | Py_INCREF(quality_layers)_Py_INCREF(((PyObject*)(quality_layers))); | |||
| 1295 | } | |||
| 1296 | ||||
| 1297 | context->num_resolutions = num_resolutions; | |||
| 1298 | ||||
| 1299 | j2k_decode_coord_tuple(cblk_size, &context->cblk_width, &context->cblk_height); | |||
| 1300 | j2k_decode_coord_tuple( | |||
| 1301 | precinct_size, &context->precinct_width, &context->precinct_height); | |||
| 1302 | ||||
| 1303 | context->irreversible = PyObject_IsTrue(irreversible); | |||
| 1304 | context->progression = prog_order; | |||
| 1305 | context->cinema_mode = cine_mode; | |||
| 1306 | ||||
| 1307 | return (PyObject *)encoder; | |||
| 1308 | } | |||
| 1309 | ||||
| 1310 | #endif | |||
| 1311 | ||||
| 1312 | /* | |||
| 1313 | * Local Variables: | |||
| 1314 | * c-basic-offset: 4 | |||
| 1315 | * End: | |||
| 1316 | * | |||
| 1317 | */ |
| 1 | #ifndef PyBytes_FromStringAndSize |
| 2 | struct _object; |
| 3 | typedef struct _object PyObject; |
| 4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
| 5 | PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len) { |
| 6 | return clang_analyzer_PyObject_New_Reference(); |
| 7 | } |
| 8 | #else |
| 9 | #warning "API PyBytes_FromStringAndSize is defined as a macro." |
| 10 | #endif |