forked from python-pillow/Pillow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.c
689 lines (515 loc) · 16.1 KB
/
decode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
* 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! */
#include "Python.h"
#if PY_VERSION_HEX < 0x01060000
#define PyObject_New PyObject_NEW
#define PyObject_Del PyMem_DEL
#endif
#include "Imaging.h"
#include "Gif.h"
#include "Lzw.h"
#include "Raw.h"
#include "Bit.h"
/* -------------------------------------------------------------------- */
/* Common */
/* -------------------------------------------------------------------- */
typedef struct {
PyObject_HEAD
int (*decode)(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
struct ImagingCodecStateInstance state;
Imaging im;
PyObject* lock;
} ImagingDecoderObject;
staticforward PyTypeObject ImagingDecoderType;
static ImagingDecoderObject*
PyImaging_DecoderNew(int contextsize)
{
ImagingDecoderObject *decoder;
void *context;
ImagingDecoderType.ob_type = &PyType_Type;
decoder = PyObject_New(ImagingDecoderObject, &ImagingDecoderType);
if (decoder == NULL)
return NULL;
/* Clear the decoder state */
memset(&decoder->state, 0, sizeof(decoder->state));
/* Allocate decoder context */
if (contextsize > 0) {
context = (void*) calloc(1, contextsize);
if (!context) {
Py_DECREF(decoder);
(void) PyErr_NoMemory();
return NULL;
}
} else
context = 0;
/* Initialize decoder context */
decoder->state.context = context;
/* Target image */
decoder->lock = NULL;
decoder->im = NULL;
return decoder;
}
static void
_dealloc(ImagingDecoderObject* decoder)
{
free(decoder->state.buffer);
free(decoder->state.context);
Py_XDECREF(decoder->lock);
PyObject_Del(decoder);
}
static PyObject*
_decode(ImagingDecoderObject* decoder, PyObject* args)
{
UINT8* buffer;
int bufsize, status;
if (!PyArg_ParseTuple(args, "s#", &buffer, &bufsize))
return NULL;
status = decoder->decode(decoder->im, &decoder->state, buffer, bufsize);
return Py_BuildValue("ii", status, decoder->state.errcode);
}
extern Imaging PyImaging_AsImaging(PyObject *op);
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 */
if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1))
return NULL;
im = PyImaging_AsImaging(op);
if (!im)
return NULL;
decoder->im = im;
state = &decoder->state;
/* Setup decoding tile extent */
if (x0 == 0 && x1 == 0) {
state->xsize = im->xsize;
state->ysize = im->ysize;
} else {
state->xoff = x0;
state->yoff = y0;
state->xsize = x1 - x0;
state->ysize = y1 - y0;
}
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;
}
/* Allocate memory buffer (if bits field is set) */
if (state->bits > 0) {
if (!state->bytes)
state->bytes = (state->bits * state->xsize+7)/8;
state->buffer = (UINT8*) malloc(state->bytes);
if (!state->buffer)
return PyErr_NoMemory();
}
/* 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;
}
static struct PyMethodDef methods[] = {
{"decode", (PyCFunction)_decode, 1},
{"setimage", (PyCFunction)_setimage, 1},
{NULL, NULL} /* sentinel */
};
static PyObject*
_getattr(ImagingDecoderObject* self, char* name)
{
return Py_FindMethod(methods, (PyObject*) self, name);
}
statichere PyTypeObject ImagingDecoderType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"ImagingDecoder", /*tp_name*/
sizeof(ImagingDecoderObject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_hash*/
};
/* -------------------------------------------------------------------- */
int
get_unpacker(ImagingDecoderObject* decoder, const char* mode,
const char* rawmode)
{
int bits;
ImagingShuffler unpack;
unpack = ImagingFindUnpacker(mode, rawmode, &bits);
if (!unpack) {
Py_DECREF(decoder);
PyErr_SetString(PyExc_ValueError, "unknown raw mode");
return -1;
}
decoder->state.shuffle = unpack;
decoder->state.bits = bits;
return 0;
}
/* -------------------------------------------------------------------- */
/* BIT (packed fields) */
/* -------------------------------------------------------------------- */
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))
return NULL;
if (strcmp(mode, "F") != 0) {
PyErr_SetString(PyExc_ValueError, "bad image mode");
return NULL;
}
decoder = PyImaging_DecoderNew(sizeof(BITSTATE));
if (decoder == NULL)
return NULL;
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;
}
/* -------------------------------------------------------------------- */
/* FLI */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_FliDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
decoder->decode = ImagingFliDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* GIF */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_GifDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
int bits = 8;
int interlace = 0;
if (!PyArg_ParseTuple(args, "s|ii", &mode, &bits, &interlace))
return NULL;
if (strcmp(mode, "L") != 0 && strcmp(mode, "P") != 0) {
PyErr_SetString(PyExc_ValueError, "bad image mode");
return NULL;
}
decoder = PyImaging_DecoderNew(sizeof(GIFDECODERSTATE));
if (decoder == NULL)
return NULL;
decoder->decode = ImagingGifDecode;
((GIFDECODERSTATE*)decoder->state.context)->bits = bits;
((GIFDECODERSTATE*)decoder->state.context)->interlace = interlace;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* HEX */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_HexDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
return NULL;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingHexDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* LZW */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_TiffLzwDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
int filter = 0;
if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &filter))
return NULL;
decoder = PyImaging_DecoderNew(sizeof(LZWSTATE));
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingLzwDecode;
((LZWSTATE*)decoder->state.context)->filter = filter;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* MSP */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_MspDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, "1", "1") < 0)
return NULL;
decoder->decode = ImagingMspDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* PackBits */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_PackbitsDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
return NULL;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingPackbitsDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* PCD */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_PcdDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
/* Unpack from PhotoYCC to RGB */
if (get_unpacker(decoder, "RGB", "YCC;P") < 0)
return NULL;
decoder->decode = ImagingPcdDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* PCX */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_PcxDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
int stride;
if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride))
return NULL;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->state.bytes = stride;
decoder->decode = ImagingPcxDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* RAW */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_RawDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
int stride = 0;
int ystep = 1;
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep))
return NULL;
decoder = PyImaging_DecoderNew(sizeof(RAWSTATE));
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingRawDecode;
decoder->state.ystep = ystep;
((RAWSTATE*)decoder->state.context)->stride = stride;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* SUN RLE */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_SunRleDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
return NULL;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingSunRleDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* TGA RLE */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_TgaRleDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
int ystep = 1;
int depth = 8;
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth))
return NULL;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingTgaRleDecode;
decoder->state.ystep = ystep;
decoder->state.count = depth / 8;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* XBM */
/* -------------------------------------------------------------------- */
PyObject*
PyImaging_XbmDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
decoder = PyImaging_DecoderNew(0);
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, "1", "1;R") < 0)
return NULL;
decoder->decode = ImagingXbmDecode;
return (PyObject*) decoder;
}
/* -------------------------------------------------------------------- */
/* ZIP */
/* -------------------------------------------------------------------- */
#ifdef HAVE_LIBZ
#include "Zip.h"
PyObject*
PyImaging_ZipDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode;
int interlaced = 0;
if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced))
return NULL;
decoder = PyImaging_DecoderNew(sizeof(ZIPSTATE));
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingZipDecode;
((ZIPSTATE*)decoder->state.context)->interlaced = interlaced;
return (PyObject*) decoder;
}
#endif
/* -------------------------------------------------------------------- */
/* JPEG */
/* -------------------------------------------------------------------- */
#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. */
#undef HAVE_PROTOTYPES
#undef HAVE_STDDEF_H
#undef HAVE_STDLIB_H
#undef UINT8
#undef UINT16
#undef UINT32
#undef INT8
#undef INT16
#undef INT32
#include "Jpeg.h"
PyObject*
PyImaging_JpegDecoderNew(PyObject* self, PyObject* args)
{
ImagingDecoderObject* decoder;
char* mode;
char* rawmode; /* what we wan't from the decoder */
char* jpegmode; /* what's in the file */
int scale = 1;
int draft = 0;
if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode,
&scale, &draft))
return NULL;
if (!jpegmode)
jpegmode = "";
decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE));
if (decoder == NULL)
return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL;
decoder->decode = ImagingJpegDecode;
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