decode.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * The Python Imaging Library.
  3. *
  4. * standard decoder interfaces for the Imaging library
  5. *
  6. * history:
  7. * 1996-03-28 fl Moved from _imagingmodule.c
  8. * 1996-04-15 fl Support subregions in setimage
  9. * 1996-04-19 fl Allocate decoder buffer (where appropriate)
  10. * 1996-05-02 fl Added jpeg decoder
  11. * 1996-05-12 fl Compile cleanly as C++
  12. * 1996-05-16 fl Added hex decoder
  13. * 1996-05-26 fl Added jpeg configuration parameters
  14. * 1996-12-14 fl Added zip decoder
  15. * 1996-12-30 fl Plugged potential memory leak for tiled images
  16. * 1997-01-03 fl Added fli and msp decoders
  17. * 1997-01-04 fl Added sun_rle and tga_rle decoders
  18. * 1997-05-31 fl Added bitfield decoder
  19. * 1998-09-11 fl Added orientation and pixelsize fields to tga_rle decoder
  20. * 1998-12-29 fl Added mode/rawmode argument to decoders
  21. * 1998-12-30 fl Added mode argument to *all* decoders
  22. * 2002-06-09 fl Added stride argument to pcx decoder
  23. *
  24. * Copyright (c) 1997-2002 by Secret Labs AB.
  25. * Copyright (c) 1995-2002 by Fredrik Lundh.
  26. *
  27. * See the README file for information on usage and redistribution.
  28. */
  29. /* FIXME: make these pluggable! */
  30. #define PY_SSIZE_T_CLEAN
  31. #include "Python.h"
  32. #include "libImaging/Imaging.h"
  33. #include "libImaging/Bit.h"
  34. #include "libImaging/Bcn.h"
  35. #include "libImaging/Gif.h"
  36. #include "libImaging/Raw.h"
  37. #include "libImaging/Sgi.h"
  38. /* -------------------------------------------------------------------- */
  39. /* Common */
  40. /* -------------------------------------------------------------------- */
  41. typedef struct {
  42. PyObject_HEAD int (*decode)(
  43. Imaging im, ImagingCodecState state, UINT8 *buffer, Py_ssize_t bytes);
  44. int (*cleanup)(ImagingCodecState state);
  45. struct ImagingCodecStateInstance state;
  46. Imaging im;
  47. PyObject *lock;
  48. int pulls_fd;
  49. } ImagingDecoderObject;
  50. static PyTypeObject ImagingDecoderType;
  51. static ImagingDecoderObject *
  52. PyImaging_DecoderNew(int contextsize) {
  53. ImagingDecoderObject *decoder;
  54. void *context;
  55. if (PyType_Ready(&ImagingDecoderType) < 0) {
  56. return NULL;
  57. }
  58. decoder = PyObject_New(ImagingDecoderObject, &ImagingDecoderType);
  59. if (decoder == NULL) {
  60. return NULL;
  61. }
  62. /* Clear the decoder state */
  63. memset(&decoder->state, 0, sizeof(decoder->state));
  64. /* Allocate decoder context */
  65. if (contextsize > 0) {
  66. context = (void *)calloc(1, contextsize);
  67. if (!context) {
  68. Py_DECREF(decoder);
  69. (void)ImagingError_MemoryError();
  70. return NULL;
  71. }
  72. } else {
  73. context = 0;
  74. }
  75. /* Initialize decoder context */
  76. decoder->state.context = context;
  77. /* Target image */
  78. decoder->lock = NULL;
  79. decoder->im = NULL;
  80. /* Initialize the cleanup function pointer */
  81. decoder->cleanup = NULL;
  82. /* set if the decoder needs to pull data from the fd, instead of
  83. having it pushed */
  84. decoder->pulls_fd = 0;
  85. return decoder;
  86. }
  87. static void
  88. _dealloc(ImagingDecoderObject *decoder) {
  89. if (decoder->cleanup) {
  90. decoder->cleanup(&decoder->state);
  91. }
  92. free(decoder->state.buffer);
  93. free(decoder->state.context);
  94. Py_XDECREF(decoder->lock);
  95. Py_XDECREF(decoder->state.fd);
  96. PyObject_Del(decoder);
  97. }
  98. static PyObject *
  99. _decode(ImagingDecoderObject *decoder, PyObject *args) {
  100. Py_buffer buffer;
  101. int status;
  102. ImagingSectionCookie cookie;
  103. if (!PyArg_ParseTuple(args, "y*", &buffer)) {
  104. return NULL;
  105. }
  106. if (!decoder->pulls_fd) {
  107. ImagingSectionEnter(&cookie);
  108. }
  109. status = decoder->decode(decoder->im, &decoder->state, buffer.buf, buffer.len);
  110. if (!decoder->pulls_fd) {
  111. ImagingSectionLeave(&cookie);
  112. }
  113. PyBuffer_Release(&buffer);
  114. return Py_BuildValue("ii", status, decoder->state.errcode);
  115. }
  116. static PyObject *
  117. _decode_cleanup(ImagingDecoderObject *decoder, PyObject *args) {
  118. int status = 0;
  119. if (decoder->cleanup) {
  120. status = decoder->cleanup(&decoder->state);
  121. }
  122. return Py_BuildValue("i", status);
  123. }
  124. extern Imaging
  125. PyImaging_AsImaging(PyObject *op);
  126. static PyObject *
  127. _setimage(ImagingDecoderObject *decoder, PyObject *args) {
  128. PyObject *op;
  129. Imaging im;
  130. ImagingCodecState state;
  131. int x0, y0, x1, y1;
  132. x0 = y0 = x1 = y1 = 0;
  133. /* FIXME: should publish the ImagingType descriptor */
  134. if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1)) {
  135. return NULL;
  136. }
  137. im = PyImaging_AsImaging(op);
  138. if (!im) {
  139. return NULL;
  140. }
  141. decoder->im = im;
  142. state = &decoder->state;
  143. /* Setup decoding tile extent */
  144. if (x0 == 0 && x1 == 0) {
  145. state->xsize = im->xsize;
  146. state->ysize = im->ysize;
  147. } else {
  148. state->xoff = x0;
  149. state->yoff = y0;
  150. state->xsize = x1 - x0;
  151. state->ysize = y1 - y0;
  152. }
  153. if (state->xsize <= 0 || state->xsize + state->xoff > (int)im->xsize ||
  154. state->ysize <= 0 || state->ysize + state->yoff > (int)im->ysize) {
  155. PyErr_SetString(PyExc_ValueError, "tile cannot extend outside image");
  156. return NULL;
  157. }
  158. /* Allocate memory buffer (if bits field is set) */
  159. if (state->bits > 0) {
  160. if (!state->bytes) {
  161. if (state->xsize > ((INT_MAX / state->bits) - 7)) {
  162. return ImagingError_MemoryError();
  163. }
  164. state->bytes = (state->bits * state->xsize + 7) / 8;
  165. }
  166. /* malloc check ok, overflow checked above */
  167. state->buffer = (UINT8 *)calloc(1, state->bytes);
  168. if (!state->buffer) {
  169. return ImagingError_MemoryError();
  170. }
  171. }
  172. /* Keep a reference to the image object, to make sure it doesn't
  173. go away before we do */
  174. Py_INCREF(op);
  175. Py_XDECREF(decoder->lock);
  176. decoder->lock = op;
  177. Py_INCREF(Py_None);
  178. return Py_None;
  179. }
  180. static PyObject *
  181. _setfd(ImagingDecoderObject *decoder, PyObject *args) {
  182. PyObject *fd;
  183. ImagingCodecState state;
  184. if (!PyArg_ParseTuple(args, "O", &fd)) {
  185. return NULL;
  186. }
  187. state = &decoder->state;
  188. Py_XINCREF(fd);
  189. state->fd = fd;
  190. Py_INCREF(Py_None);
  191. return Py_None;
  192. }
  193. static PyObject *
  194. _get_pulls_fd(ImagingDecoderObject *decoder, void *closure) {
  195. return PyBool_FromLong(decoder->pulls_fd);
  196. }
  197. static struct PyMethodDef methods[] = {
  198. {"decode", (PyCFunction)_decode, METH_VARARGS},
  199. {"cleanup", (PyCFunction)_decode_cleanup, METH_VARARGS},
  200. {"setimage", (PyCFunction)_setimage, METH_VARARGS},
  201. {"setfd", (PyCFunction)_setfd, METH_VARARGS},
  202. {NULL, NULL} /* sentinel */
  203. };
  204. static struct PyGetSetDef getseters[] = {
  205. {"pulls_fd",
  206. (getter)_get_pulls_fd,
  207. NULL,
  208. "True if this decoder expects to pull from self.fd itself.",
  209. NULL},
  210. {NULL, NULL, NULL, NULL, NULL} /* sentinel */
  211. };
  212. static PyTypeObject ImagingDecoderType = {
  213. PyVarObject_HEAD_INIT(NULL, 0) "ImagingDecoder", /*tp_name*/
  214. sizeof(ImagingDecoderObject), /*tp_basicsize*/
  215. 0, /*tp_itemsize*/
  216. /* methods */
  217. (destructor)_dealloc, /*tp_dealloc*/
  218. 0, /*tp_vectorcall_offset*/
  219. 0, /*tp_getattr*/
  220. 0, /*tp_setattr*/
  221. 0, /*tp_as_async*/
  222. 0, /*tp_repr*/
  223. 0, /*tp_as_number*/
  224. 0, /*tp_as_sequence*/
  225. 0, /*tp_as_mapping*/
  226. 0, /*tp_hash*/
  227. 0, /*tp_call*/
  228. 0, /*tp_str*/
  229. 0, /*tp_getattro*/
  230. 0, /*tp_setattro*/
  231. 0, /*tp_as_buffer*/
  232. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  233. 0, /*tp_doc*/
  234. 0, /*tp_traverse*/
  235. 0, /*tp_clear*/
  236. 0, /*tp_richcompare*/
  237. 0, /*tp_weaklistoffset*/
  238. 0, /*tp_iter*/
  239. 0, /*tp_iternext*/
  240. methods, /*tp_methods*/
  241. 0, /*tp_members*/
  242. getseters, /*tp_getset*/
  243. };
  244. /* -------------------------------------------------------------------- */
  245. int
  246. get_unpacker(ImagingDecoderObject *decoder, const char *mode, const char *rawmode) {
  247. int bits;
  248. ImagingShuffler unpack;
  249. unpack = ImagingFindUnpacker(mode, rawmode, &bits);
  250. if (!unpack) {
  251. Py_DECREF(decoder);
  252. PyErr_SetString(PyExc_ValueError, "unknown raw mode for given image mode");
  253. return -1;
  254. }
  255. decoder->state.shuffle = unpack;
  256. decoder->state.bits = bits;
  257. return 0;
  258. }
  259. /* -------------------------------------------------------------------- */
  260. /* BIT (packed fields) */
  261. /* -------------------------------------------------------------------- */
  262. PyObject *
  263. PyImaging_BitDecoderNew(PyObject *self, PyObject *args) {
  264. ImagingDecoderObject *decoder;
  265. char *mode;
  266. int bits = 8;
  267. int pad = 8;
  268. int fill = 0;
  269. int sign = 0;
  270. int ystep = 1;
  271. if (!PyArg_ParseTuple(args, "s|iiiii", &mode, &bits, &pad, &fill, &sign, &ystep)) {
  272. return NULL;
  273. }
  274. if (strcmp(mode, "F") != 0) {
  275. PyErr_SetString(PyExc_ValueError, "bad image mode");
  276. return NULL;
  277. }
  278. decoder = PyImaging_DecoderNew(sizeof(BITSTATE));
  279. if (decoder == NULL) {
  280. return NULL;
  281. }
  282. decoder->decode = ImagingBitDecode;
  283. decoder->state.ystep = ystep;
  284. ((BITSTATE *)decoder->state.context)->bits = bits;
  285. ((BITSTATE *)decoder->state.context)->pad = pad;
  286. ((BITSTATE *)decoder->state.context)->fill = fill;
  287. ((BITSTATE *)decoder->state.context)->sign = sign;
  288. return (PyObject *)decoder;
  289. }
  290. /* -------------------------------------------------------------------- */
  291. /* BCn: GPU block-compressed texture formats */
  292. /* -------------------------------------------------------------------- */
  293. PyObject *
  294. PyImaging_BcnDecoderNew(PyObject *self, PyObject *args) {
  295. ImagingDecoderObject *decoder;
  296. char *mode;
  297. char *actual;
  298. int n = 0;
  299. char *pixel_format = "";
  300. if (!PyArg_ParseTuple(args, "si|s", &mode, &n, &pixel_format)) {
  301. return NULL;
  302. }
  303. switch (n) {
  304. case 1: /* BC1: 565 color, 1-bit alpha */
  305. case 2: /* BC2: 565 color, 4-bit alpha */
  306. case 3: /* BC3: 565 color, 2-endpoint 8-bit interpolated alpha */
  307. case 7: /* BC7: 4-channel 8-bit via everything */
  308. actual = "RGBA";
  309. break;
  310. case 4: /* BC4: 1-channel 8-bit via 1 BC3 alpha block */
  311. actual = "L";
  312. break;
  313. case 5: /* BC5: 2-channel 8-bit via 2 BC3 alpha blocks */
  314. case 6: /* BC6: 3-channel 16-bit float */
  315. actual = "RGB";
  316. break;
  317. default:
  318. PyErr_SetString(PyExc_ValueError, "block compression type unknown");
  319. return NULL;
  320. }
  321. if (strcmp(mode, actual) != 0) {
  322. PyErr_SetString(PyExc_ValueError, "bad image mode");
  323. return NULL;
  324. }
  325. decoder = PyImaging_DecoderNew(sizeof(char *));
  326. if (decoder == NULL) {
  327. return NULL;
  328. }
  329. decoder->decode = ImagingBcnDecode;
  330. decoder->state.state = n;
  331. ((BCNSTATE *)decoder->state.context)->pixel_format = pixel_format;
  332. return (PyObject *)decoder;
  333. }
  334. /* -------------------------------------------------------------------- */
  335. /* FLI */
  336. /* -------------------------------------------------------------------- */
  337. PyObject *
  338. PyImaging_FliDecoderNew(PyObject *self, PyObject *args) {
  339. ImagingDecoderObject *decoder;
  340. decoder = PyImaging_DecoderNew(0);
  341. if (decoder == NULL) {
  342. return NULL;
  343. }
  344. decoder->decode = ImagingFliDecode;
  345. return (PyObject *)decoder;
  346. }
  347. /* -------------------------------------------------------------------- */
  348. /* GIF */
  349. /* -------------------------------------------------------------------- */
  350. PyObject *
  351. PyImaging_GifDecoderNew(PyObject *self, PyObject *args) {
  352. ImagingDecoderObject *decoder;
  353. char *mode;
  354. int bits = 8;
  355. int interlace = 0;
  356. int transparency = -1;
  357. if (!PyArg_ParseTuple(args, "s|iii", &mode, &bits, &interlace, &transparency)) {
  358. return NULL;
  359. }
  360. if (strcmp(mode, "L") != 0 && strcmp(mode, "P") != 0) {
  361. PyErr_SetString(PyExc_ValueError, "bad image mode");
  362. return NULL;
  363. }
  364. decoder = PyImaging_DecoderNew(sizeof(GIFDECODERSTATE));
  365. if (decoder == NULL) {
  366. return NULL;
  367. }
  368. decoder->decode = ImagingGifDecode;
  369. ((GIFDECODERSTATE *)decoder->state.context)->bits = bits;
  370. ((GIFDECODERSTATE *)decoder->state.context)->interlace = interlace;
  371. ((GIFDECODERSTATE *)decoder->state.context)->transparency = transparency;
  372. return (PyObject *)decoder;
  373. }
  374. /* -------------------------------------------------------------------- */
  375. /* HEX */
  376. /* -------------------------------------------------------------------- */
  377. PyObject *
  378. PyImaging_HexDecoderNew(PyObject *self, PyObject *args) {
  379. ImagingDecoderObject *decoder;
  380. char *mode;
  381. char *rawmode;
  382. if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) {
  383. return NULL;
  384. }
  385. decoder = PyImaging_DecoderNew(0);
  386. if (decoder == NULL) {
  387. return NULL;
  388. }
  389. if (get_unpacker(decoder, mode, rawmode) < 0) {
  390. return NULL;
  391. }
  392. decoder->decode = ImagingHexDecode;
  393. return (PyObject *)decoder;
  394. }
  395. /* -------------------------------------------------------------------- */
  396. /* LibTiff */
  397. /* -------------------------------------------------------------------- */
  398. #ifdef HAVE_LIBTIFF
  399. #include "libImaging/TiffDecode.h"
  400. #include <string.h>
  401. PyObject *
  402. PyImaging_LibTiffDecoderNew(PyObject *self, PyObject *args) {
  403. ImagingDecoderObject *decoder;
  404. char *mode;
  405. char *rawmode;
  406. char *compname;
  407. int fp;
  408. uint32_t ifdoffset;
  409. if (!PyArg_ParseTuple(args, "sssiI", &mode, &rawmode, &compname, &fp, &ifdoffset)) {
  410. return NULL;
  411. }
  412. TRACE(("new tiff decoder %s\n", compname));
  413. decoder = PyImaging_DecoderNew(sizeof(TIFFSTATE));
  414. if (decoder == NULL) {
  415. return NULL;
  416. }
  417. if (get_unpacker(decoder, mode, rawmode) < 0) {
  418. return NULL;
  419. }
  420. if (!ImagingLibTiffInit(&decoder->state, fp, ifdoffset)) {
  421. Py_DECREF(decoder);
  422. PyErr_SetString(PyExc_RuntimeError, "tiff codec initialization failed");
  423. return NULL;
  424. }
  425. decoder->decode = ImagingLibTiffDecode;
  426. return (PyObject *)decoder;
  427. }
  428. #endif
  429. /* -------------------------------------------------------------------- */
  430. /* PackBits */
  431. /* -------------------------------------------------------------------- */
  432. PyObject *
  433. PyImaging_PackbitsDecoderNew(PyObject *self, PyObject *args) {
  434. ImagingDecoderObject *decoder;
  435. char *mode;
  436. char *rawmode;
  437. if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) {
  438. return NULL;
  439. }
  440. decoder = PyImaging_DecoderNew(0);
  441. if (decoder == NULL) {
  442. return NULL;
  443. }
  444. if (get_unpacker(decoder, mode, rawmode) < 0) {
  445. return NULL;
  446. }
  447. decoder->decode = ImagingPackbitsDecode;
  448. return (PyObject *)decoder;
  449. }
  450. /* -------------------------------------------------------------------- */
  451. /* PCD */
  452. /* -------------------------------------------------------------------- */
  453. PyObject *
  454. PyImaging_PcdDecoderNew(PyObject *self, PyObject *args) {
  455. ImagingDecoderObject *decoder;
  456. decoder = PyImaging_DecoderNew(0);
  457. if (decoder == NULL) {
  458. return NULL;
  459. }
  460. /* Unpack from PhotoYCC to RGB */
  461. if (get_unpacker(decoder, "RGB", "YCC;P") < 0) {
  462. return NULL;
  463. }
  464. decoder->decode = ImagingPcdDecode;
  465. return (PyObject *)decoder;
  466. }
  467. /* -------------------------------------------------------------------- */
  468. /* PCX */
  469. /* -------------------------------------------------------------------- */
  470. PyObject *
  471. PyImaging_PcxDecoderNew(PyObject *self, PyObject *args) {
  472. ImagingDecoderObject *decoder;
  473. char *mode;
  474. char *rawmode;
  475. int stride;
  476. if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride)) {
  477. return NULL;
  478. }
  479. decoder = PyImaging_DecoderNew(0);
  480. if (decoder == NULL) {
  481. return NULL;
  482. }
  483. if (get_unpacker(decoder, mode, rawmode) < 0) {
  484. return NULL;
  485. }
  486. decoder->state.bytes = stride;
  487. decoder->decode = ImagingPcxDecode;
  488. return (PyObject *)decoder;
  489. }
  490. /* -------------------------------------------------------------------- */
  491. /* RAW */
  492. /* -------------------------------------------------------------------- */
  493. PyObject *
  494. PyImaging_RawDecoderNew(PyObject *self, PyObject *args) {
  495. ImagingDecoderObject *decoder;
  496. char *mode;
  497. char *rawmode;
  498. int stride = 0;
  499. int ystep = 1;
  500. if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep)) {
  501. return NULL;
  502. }
  503. decoder = PyImaging_DecoderNew(sizeof(RAWSTATE));
  504. if (decoder == NULL) {
  505. return NULL;
  506. }
  507. if (get_unpacker(decoder, mode, rawmode) < 0) {
  508. return NULL;
  509. }
  510. decoder->decode = ImagingRawDecode;
  511. decoder->state.ystep = ystep;
  512. ((RAWSTATE *)decoder->state.context)->stride = stride;
  513. return (PyObject *)decoder;
  514. }
  515. /* -------------------------------------------------------------------- */
  516. /* SGI RLE */
  517. /* -------------------------------------------------------------------- */
  518. PyObject *
  519. PyImaging_SgiRleDecoderNew(PyObject *self, PyObject *args) {
  520. ImagingDecoderObject *decoder;
  521. char *mode;
  522. char *rawmode;
  523. int ystep = 1;
  524. int bpc = 1;
  525. if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &bpc)) {
  526. return NULL;
  527. }
  528. decoder = PyImaging_DecoderNew(sizeof(SGISTATE));
  529. if (decoder == NULL) {
  530. return NULL;
  531. }
  532. if (get_unpacker(decoder, mode, rawmode) < 0) {
  533. return NULL;
  534. }
  535. decoder->pulls_fd = 1;
  536. decoder->decode = ImagingSgiRleDecode;
  537. decoder->state.ystep = ystep;
  538. ((SGISTATE *)decoder->state.context)->bpc = bpc;
  539. return (PyObject *)decoder;
  540. }
  541. /* -------------------------------------------------------------------- */
  542. /* SUN RLE */
  543. /* -------------------------------------------------------------------- */
  544. PyObject *
  545. PyImaging_SunRleDecoderNew(PyObject *self, PyObject *args) {
  546. ImagingDecoderObject *decoder;
  547. char *mode;
  548. char *rawmode;
  549. if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) {
  550. return NULL;
  551. }
  552. decoder = PyImaging_DecoderNew(0);
  553. if (decoder == NULL) {
  554. return NULL;
  555. }
  556. if (get_unpacker(decoder, mode, rawmode) < 0) {
  557. return NULL;
  558. }
  559. decoder->decode = ImagingSunRleDecode;
  560. return (PyObject *)decoder;
  561. }
  562. /* -------------------------------------------------------------------- */
  563. /* TGA RLE */
  564. /* -------------------------------------------------------------------- */
  565. PyObject *
  566. PyImaging_TgaRleDecoderNew(PyObject *self, PyObject *args) {
  567. ImagingDecoderObject *decoder;
  568. char *mode;
  569. char *rawmode;
  570. int ystep = 1;
  571. int depth = 8;
  572. if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth)) {
  573. return NULL;
  574. }
  575. decoder = PyImaging_DecoderNew(0);
  576. if (decoder == NULL) {
  577. return NULL;
  578. }
  579. if (get_unpacker(decoder, mode, rawmode) < 0) {
  580. return NULL;
  581. }
  582. decoder->decode = ImagingTgaRleDecode;
  583. decoder->state.ystep = ystep;
  584. decoder->state.count = depth / 8;
  585. return (PyObject *)decoder;
  586. }
  587. /* -------------------------------------------------------------------- */
  588. /* XBM */
  589. /* -------------------------------------------------------------------- */
  590. PyObject *
  591. PyImaging_XbmDecoderNew(PyObject *self, PyObject *args) {
  592. ImagingDecoderObject *decoder;
  593. decoder = PyImaging_DecoderNew(0);
  594. if (decoder == NULL) {
  595. return NULL;
  596. }
  597. if (get_unpacker(decoder, "1", "1;R") < 0) {
  598. return NULL;
  599. }
  600. decoder->decode = ImagingXbmDecode;
  601. return (PyObject *)decoder;
  602. }
  603. /* -------------------------------------------------------------------- */
  604. /* ZIP */
  605. /* -------------------------------------------------------------------- */
  606. #ifdef HAVE_LIBZ
  607. #include "libImaging/ZipCodecs.h"
  608. PyObject *
  609. PyImaging_ZipDecoderNew(PyObject *self, PyObject *args) {
  610. ImagingDecoderObject *decoder;
  611. char *mode;
  612. char *rawmode;
  613. int interlaced = 0;
  614. if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced)) {
  615. return NULL;
  616. }
  617. decoder = PyImaging_DecoderNew(sizeof(ZIPSTATE));
  618. if (decoder == NULL) {
  619. return NULL;
  620. }
  621. if (get_unpacker(decoder, mode, rawmode) < 0) {
  622. return NULL;
  623. }
  624. decoder->decode = ImagingZipDecode;
  625. decoder->cleanup = ImagingZipDecodeCleanup;
  626. ((ZIPSTATE *)decoder->state.context)->interlaced = interlaced;
  627. return (PyObject *)decoder;
  628. }
  629. #endif
  630. /* -------------------------------------------------------------------- */
  631. /* JPEG */
  632. /* -------------------------------------------------------------------- */
  633. #ifdef HAVE_LIBJPEG
  634. /* We better define this decoder last in this file, so the following
  635. undef's won't mess things up for the Imaging library proper. */
  636. #undef HAVE_PROTOTYPES
  637. #undef HAVE_STDDEF_H
  638. #undef HAVE_STDLIB_H
  639. #undef UINT8
  640. #undef UINT16
  641. #undef UINT32
  642. #undef INT8
  643. #undef INT16
  644. #undef INT32
  645. #include "libImaging/Jpeg.h"
  646. PyObject *
  647. PyImaging_JpegDecoderNew(PyObject *self, PyObject *args) {
  648. ImagingDecoderObject *decoder;
  649. char *mode;
  650. char *rawmode; /* what we want from the decoder */
  651. char *jpegmode; /* what's in the file */
  652. int scale = 1;
  653. int draft = 0;
  654. if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, &scale, &draft)) {
  655. return NULL;
  656. }
  657. if (!jpegmode) {
  658. jpegmode = "";
  659. }
  660. decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE));
  661. if (decoder == NULL) {
  662. return NULL;
  663. }
  664. // libjpeg-turbo supports different output formats.
  665. // We are choosing Pillow's native format (3 color bytes + 1 padding)
  666. // to avoid extra conversion in Unpack.c.
  667. if (ImagingJpegUseJCSExtensions() && strcmp(rawmode, "RGB") == 0) {
  668. rawmode = "RGBX";
  669. }
  670. if (get_unpacker(decoder, mode, rawmode) < 0) {
  671. return NULL;
  672. }
  673. decoder->decode = ImagingJpegDecode;
  674. decoder->cleanup = ImagingJpegDecodeCleanup;
  675. strncpy(((JPEGSTATE *)decoder->state.context)->rawmode, rawmode, 8);
  676. strncpy(((JPEGSTATE *)decoder->state.context)->jpegmode, jpegmode, 8);
  677. ((JPEGSTATE *)decoder->state.context)->scale = scale;
  678. ((JPEGSTATE *)decoder->state.context)->draft = draft;
  679. return (PyObject *)decoder;
  680. }
  681. #endif
  682. /* -------------------------------------------------------------------- */
  683. /* JPEG 2000 */
  684. /* -------------------------------------------------------------------- */
  685. #ifdef HAVE_OPENJPEG
  686. #include "libImaging/Jpeg2K.h"
  687. PyObject *
  688. PyImaging_Jpeg2KDecoderNew(PyObject *self, PyObject *args) {
  689. ImagingDecoderObject *decoder;
  690. JPEG2KDECODESTATE *context;
  691. char *mode;
  692. char *format;
  693. OPJ_CODEC_FORMAT codec_format;
  694. int reduce = 0;
  695. int layers = 0;
  696. int fd = -1;
  697. PY_LONG_LONG length = -1;
  698. if (!PyArg_ParseTuple(
  699. args, "ss|iiiL", &mode, &format, &reduce, &layers, &fd, &length)) {
  700. return NULL;
  701. }
  702. if (strcmp(format, "j2k") == 0) {
  703. codec_format = OPJ_CODEC_J2K;
  704. } else if (strcmp(format, "jpt") == 0) {
  705. codec_format = OPJ_CODEC_JPT;
  706. } else if (strcmp(format, "jp2") == 0) {
  707. codec_format = OPJ_CODEC_JP2;
  708. } else {
  709. return NULL;
  710. }
  711. decoder = PyImaging_DecoderNew(sizeof(JPEG2KDECODESTATE));
  712. if (decoder == NULL) {
  713. return NULL;
  714. }
  715. decoder->pulls_fd = 1;
  716. decoder->decode = ImagingJpeg2KDecode;
  717. decoder->cleanup = ImagingJpeg2KDecodeCleanup;
  718. context = (JPEG2KDECODESTATE *)decoder->state.context;
  719. context->fd = fd;
  720. context->length = (off_t)length;
  721. context->format = codec_format;
  722. context->reduce = reduce;
  723. context->layers = layers;
  724. return (PyObject *)decoder;
  725. }
  726. #endif /* HAVE_OPENJPEG */