~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/encode.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-03-20 16:44:01 UTC
  • mfrom: (2.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20130320164401-ptf6m0ttg4zw72az
Tags: 1.1.7+2.0.0-1
Pillow 2.0.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "Python.h"
26
 
 
27
 
#if PY_VERSION_HEX < 0x01060000
28
 
#define PyObject_New PyObject_NEW
29
 
#define PyObject_Del PyMem_DEL
30
 
#endif
31
 
 
32
 
#include "Imaging.h"
33
 
#include "Gif.h"
34
 
 
35
 
#ifdef HAVE_UNISTD_H
36
 
#include <unistd.h> /* write */
37
 
#endif
38
 
 
39
 
/* -------------------------------------------------------------------- */
40
 
/* Common                                                               */
41
 
/* -------------------------------------------------------------------- */
42
 
 
43
 
typedef struct {
44
 
    PyObject_HEAD
45
 
    int (*encode)(Imaging im, ImagingCodecState state,
46
 
                  UINT8* buffer, int bytes);
47
 
    struct ImagingCodecStateInstance state;
48
 
    Imaging im;
49
 
    PyObject* lock;
50
 
} ImagingEncoderObject;
51
 
 
52
 
staticforward PyTypeObject ImagingEncoderType;
53
 
 
54
 
static ImagingEncoderObject*
55
 
PyImaging_EncoderNew(int contextsize)
56
 
{
57
 
    ImagingEncoderObject *encoder;
58
 
    void *context;
59
 
 
60
 
    ImagingEncoderType.ob_type = &PyType_Type;
61
 
 
62
 
    encoder = PyObject_New(ImagingEncoderObject, &ImagingEncoderType);
63
 
    if (encoder == NULL)
64
 
        return NULL;
65
 
 
66
 
    /* Clear the encoder state */
67
 
    memset(&encoder->state, 0, sizeof(encoder->state));
68
 
 
69
 
    /* Allocate encoder context */
70
 
    if (contextsize > 0) {
71
 
        context = (void*) calloc(1, contextsize);
72
 
        if (!context) {
73
 
            Py_DECREF(encoder);
74
 
            (void) PyErr_NoMemory();
75
 
            return NULL;
76
 
        }
77
 
    } else
78
 
        context = 0;
79
 
 
80
 
    /* Initialize encoder context */
81
 
    encoder->state.context = context;
82
 
 
83
 
    /* Target image */
84
 
    encoder->lock = NULL;
85
 
    encoder->im = NULL;
86
 
 
87
 
    return encoder;
88
 
}
89
 
 
90
 
static void
91
 
_dealloc(ImagingEncoderObject* encoder)
92
 
{
93
 
    free(encoder->state.buffer);
94
 
    free(encoder->state.context);
95
 
    Py_XDECREF(encoder->lock);
96
 
    PyObject_Del(encoder);
97
 
}
98
 
 
99
 
static PyObject* 
100
 
_encode(ImagingEncoderObject* encoder, PyObject* args)
101
 
{
102
 
    PyObject* buf;
103
 
    PyObject* result;
104
 
    int status;
105
 
 
106
 
    /* Encode to a Python string (allocated by this method) */
107
 
 
108
 
    int bufsize = 16384;
109
 
 
110
 
    if (!PyArg_ParseTuple(args, "|i", &bufsize))
111
 
        return NULL;
112
 
 
113
 
    buf = PyString_FromStringAndSize(NULL, bufsize);
114
 
    if (!buf)
115
 
        return NULL;
116
 
 
117
 
    status = encoder->encode(encoder->im, &encoder->state,
118
 
                             (UINT8*) PyString_AsString(buf), bufsize);
119
 
 
120
 
    /* adjust string length to avoid slicing in encoder */
121
 
    if (_PyString_Resize(&buf, (status > 0) ? status : 0) < 0)
122
 
        return NULL;
123
 
 
124
 
    result = Py_BuildValue("iiO", status, encoder->state.errcode, buf);
125
 
 
126
 
    Py_DECREF(buf); /* must release buffer!!! */
127
 
 
128
 
    return result;
129
 
}
130
 
 
131
 
static PyObject* 
132
 
_encode_to_file(ImagingEncoderObject* encoder, PyObject* args)
133
 
{
134
 
    UINT8* buf;
135
 
    int status;
136
 
    ImagingSectionCookie cookie;
137
 
 
138
 
    /* Encode to a file handle */
139
 
 
140
 
    int fh;
141
 
    int bufsize = 16384;
142
 
 
143
 
    if (!PyArg_ParseTuple(args, "i|i", &fh, &bufsize))
144
 
        return NULL;
145
 
 
146
 
    /* Allocate an encoder buffer */
147
 
    buf = (UINT8*) malloc(bufsize);
148
 
    if (!buf)
149
 
        return PyErr_NoMemory();
150
 
 
151
 
    ImagingSectionEnter(&cookie);
152
 
 
153
 
    do {
154
 
 
155
 
        /* This replaces the inner loop in the ImageFile _save
156
 
           function. */
157
 
 
158
 
        status = encoder->encode(encoder->im, &encoder->state, buf, bufsize);
159
 
 
160
 
        if (status > 0)
161
 
            if (write(fh, buf, status) < 0) {
162
 
                ImagingSectionLeave(&cookie);
163
 
                free(buf);
164
 
                return PyErr_SetFromErrno(PyExc_IOError);
165
 
            }
166
 
 
167
 
    } while (encoder->state.errcode == 0);
168
 
 
169
 
    ImagingSectionLeave(&cookie);
170
 
 
171
 
    free(buf);
172
 
 
173
 
    return Py_BuildValue("i", encoder->state.errcode);
174
 
}
175
 
 
176
 
extern Imaging PyImaging_AsImaging(PyObject *op);
177
 
 
178
 
static PyObject*
179
 
_setimage(ImagingEncoderObject* encoder, PyObject* args)
180
 
{
181
 
    PyObject* op;
182
 
    Imaging im;
183
 
    ImagingCodecState state;
184
 
    int x0, y0, x1, y1;
185
 
 
186
 
    /* Define where image data should be stored */
187
 
 
188
 
    x0 = y0 = x1 = y1 = 0;
189
 
 
190
 
    /* FIXME: should publish the ImagingType descriptor */
191
 
    if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1))
192
 
        return NULL;
193
 
    im = PyImaging_AsImaging(op);
194
 
    if (!im)
195
 
        return NULL;
196
 
 
197
 
    encoder->im = im;
198
 
 
199
 
    state = &encoder->state;
200
 
 
201
 
    if (x0 == 0 && x1 == 0) {
202
 
        state->xsize = im->xsize;
203
 
        state->ysize = im->ysize;
204
 
    } else {
205
 
        state->xoff = x0;
206
 
        state->yoff = y0;
207
 
        state->xsize = x1 - x0;
208
 
        state->ysize = y1 - y0;
209
 
    }
210
 
 
211
 
    if (state->xsize <= 0 ||
212
 
        state->xsize + state->xoff > im->xsize ||
213
 
        state->ysize <= 0 ||
214
 
        state->ysize + state->yoff > im->ysize) {
215
 
        PyErr_SetString(PyExc_SystemError, "tile cannot extend outside image");
216
 
        return NULL;
217
 
    }
218
 
 
219
 
    /* Allocate memory buffer (if bits field is set) */
220
 
    if (state->bits > 0) {
221
 
        state->bytes = (state->bits * state->xsize+7)/8;
222
 
        state->buffer = (UINT8*) malloc(state->bytes);
223
 
        if (!state->buffer)
224
 
            return PyErr_NoMemory();
225
 
    }
226
 
 
227
 
    /* Keep a reference to the image object, to make sure it doesn't
228
 
       go away before we do */
229
 
    Py_INCREF(op);
230
 
    Py_XDECREF(encoder->lock);
231
 
    encoder->lock = op;
232
 
 
233
 
    Py_INCREF(Py_None);
234
 
    return Py_None;
235
 
}
236
 
 
237
 
static struct PyMethodDef methods[] = {
238
 
    {"encode", (PyCFunction)_encode, 1},
239
 
    {"encode_to_file", (PyCFunction)_encode_to_file, 1},
240
 
    {"setimage", (PyCFunction)_setimage, 1},
241
 
    {NULL, NULL} /* sentinel */
242
 
};
243
 
 
244
 
static PyObject*  
245
 
_getattr(ImagingEncoderObject* self, char* name)
246
 
{
247
 
    return Py_FindMethod(methods, (PyObject*) self, name);
248
 
}
249
 
 
250
 
statichere PyTypeObject ImagingEncoderType = {
251
 
        PyObject_HEAD_INIT(NULL)
252
 
        0,                              /*ob_size*/
253
 
        "ImagingEncoder",               /*tp_name*/
254
 
        sizeof(ImagingEncoderObject),   /*tp_size*/
255
 
        0,                              /*tp_itemsize*/
256
 
        /* methods */
257
 
        (destructor)_dealloc,           /*tp_dealloc*/
258
 
        0,                              /*tp_print*/
259
 
        (getattrfunc)_getattr,          /*tp_getattr*/
260
 
        0,                              /*tp_setattr*/
261
 
        0,                              /*tp_compare*/
262
 
        0,                              /*tp_repr*/
263
 
        0,                              /*tp_hash*/
264
 
};
265
 
 
266
 
/* -------------------------------------------------------------------- */
267
 
 
268
 
int
269
 
get_packer(ImagingEncoderObject* encoder, const char* mode,
270
 
           const char* rawmode)
271
 
{
272
 
    int bits;
273
 
    ImagingShuffler pack;
274
 
 
275
 
    pack = ImagingFindPacker(mode, rawmode, &bits);
276
 
    if (!pack) {
277
 
        Py_DECREF(encoder);
278
 
        PyErr_SetString(PyExc_SystemError, "unknown raw mode");
279
 
        return -1;
280
 
    }
281
 
 
282
 
    encoder->state.shuffle = pack;
283
 
    encoder->state.bits = bits;
284
 
 
285
 
    return 0;
286
 
}
287
 
 
288
 
 
289
 
/* -------------------------------------------------------------------- */
290
 
/* EPS                                                                  */
291
 
/* -------------------------------------------------------------------- */
292
 
 
293
 
PyObject*
294
 
PyImaging_EpsEncoderNew(PyObject* self, PyObject* args)
295
 
{
296
 
    ImagingEncoderObject* encoder;
297
 
 
298
 
    encoder = PyImaging_EncoderNew(0);
299
 
    if (encoder == NULL)
300
 
        return NULL;
301
 
 
302
 
    encoder->encode = ImagingEpsEncode;
303
 
 
304
 
    return (PyObject*) encoder;
305
 
}
306
 
 
307
 
 
308
 
/* -------------------------------------------------------------------- */
309
 
/* GIF                                                                  */
310
 
/* -------------------------------------------------------------------- */
311
 
 
312
 
PyObject*
313
 
PyImaging_GifEncoderNew(PyObject* self, PyObject* args)
314
 
{
315
 
    ImagingEncoderObject* encoder;
316
 
 
317
 
    char *mode;
318
 
    char *rawmode;
319
 
    int bits = 8;
320
 
    int interlace = 0;
321
 
    if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &bits, &interlace))
322
 
        return NULL;
323
 
 
324
 
    encoder = PyImaging_EncoderNew(sizeof(GIFENCODERSTATE));
325
 
    if (encoder == NULL)
326
 
        return NULL;
327
 
 
328
 
    if (get_packer(encoder, mode, rawmode) < 0)
329
 
        return NULL;
330
 
 
331
 
    encoder->encode = ImagingGifEncode;
332
 
 
333
 
    ((GIFENCODERSTATE*)encoder->state.context)->bits = bits;
334
 
    ((GIFENCODERSTATE*)encoder->state.context)->interlace = interlace;
335
 
 
336
 
    return (PyObject*) encoder;
337
 
}
338
 
 
339
 
 
340
 
/* -------------------------------------------------------------------- */
341
 
/* PCX                                                                  */
342
 
/* -------------------------------------------------------------------- */
343
 
 
344
 
PyObject*
345
 
PyImaging_PcxEncoderNew(PyObject* self, PyObject* args)
346
 
{
347
 
    ImagingEncoderObject* encoder;
348
 
 
349
 
    char *mode;
350
 
    char *rawmode;
351
 
    int bits = 8;
352
 
    if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &bits))
353
 
        return NULL;
354
 
 
355
 
    encoder = PyImaging_EncoderNew(0);
356
 
    if (encoder == NULL)
357
 
        return NULL;
358
 
 
359
 
    if (get_packer(encoder, mode, rawmode) < 0)
360
 
        return NULL;
361
 
 
362
 
    encoder->encode = ImagingPcxEncode;
363
 
 
364
 
    return (PyObject*) encoder;
365
 
}
366
 
 
367
 
 
368
 
/* -------------------------------------------------------------------- */
369
 
/* RAW                                                                  */
370
 
/* -------------------------------------------------------------------- */
371
 
 
372
 
PyObject*
373
 
PyImaging_RawEncoderNew(PyObject* self, PyObject* args)
374
 
{
375
 
    ImagingEncoderObject* encoder;
376
 
 
377
 
    char *mode;
378
 
    char *rawmode;
379
 
    int stride = 0;
380
 
    int ystep = 1;
381
 
 
382
 
    if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep))
383
 
        return NULL;
384
 
 
385
 
    encoder = PyImaging_EncoderNew(0);
386
 
    if (encoder == NULL)
387
 
        return NULL;
388
 
 
389
 
    if (get_packer(encoder, mode, rawmode) < 0)
390
 
        return NULL;
391
 
 
392
 
    encoder->encode = ImagingRawEncode;
393
 
 
394
 
    encoder->state.ystep = ystep;
395
 
    encoder->state.count = stride;
396
 
 
397
 
    return (PyObject*) encoder;
398
 
}
399
 
 
400
 
 
401
 
/* -------------------------------------------------------------------- */
402
 
/* XBM                                                                  */
403
 
/* -------------------------------------------------------------------- */
404
 
 
405
 
PyObject*
406
 
PyImaging_XbmEncoderNew(PyObject* self, PyObject* args)
407
 
{
408
 
    ImagingEncoderObject* encoder;
409
 
 
410
 
    encoder = PyImaging_EncoderNew(0);
411
 
    if (encoder == NULL)
412
 
        return NULL;
413
 
 
414
 
    if (get_packer(encoder, "1", "1;R") < 0)
415
 
        return NULL;
416
 
 
417
 
    encoder->encode = ImagingXbmEncode;
418
 
 
419
 
    return (PyObject*) encoder;
420
 
}
421
 
 
422
 
 
423
 
/* -------------------------------------------------------------------- */
424
 
/* ZIP                                                                  */
425
 
/* -------------------------------------------------------------------- */
426
 
 
427
 
#ifdef HAVE_LIBZ
428
 
 
429
 
#include "Zip.h"
430
 
 
431
 
PyObject*
432
 
PyImaging_ZipEncoderNew(PyObject* self, PyObject* args)
433
 
{
434
 
    ImagingEncoderObject* encoder;
435
 
 
436
 
    char* mode;
437
 
    char* rawmode;
438
 
    int optimize = 0;
439
 
    char* dictionary = NULL;
440
 
    int dictionary_size = 0;
441
 
    if (!PyArg_ParseTuple(args, "ss|is#", &mode, &rawmode, &optimize,
442
 
                          &dictionary, &dictionary_size))
443
 
        return NULL;
444
 
 
445
 
    encoder = PyImaging_EncoderNew(sizeof(ZIPSTATE));
446
 
    if (encoder == NULL)
447
 
        return NULL;
448
 
 
449
 
    if (get_packer(encoder, mode, rawmode) < 0)
450
 
        return NULL;
451
 
 
452
 
    encoder->encode = ImagingZipEncode;
453
 
 
454
 
    if (rawmode[0] == 'P')
455
 
        /* disable filtering */
456
 
        ((ZIPSTATE*)encoder->state.context)->mode = ZIP_PNG_PALETTE;
457
 
 
458
 
    ((ZIPSTATE*)encoder->state.context)->optimize = optimize;
459
 
    ((ZIPSTATE*)encoder->state.context)->dictionary = dictionary;
460
 
    ((ZIPSTATE*)encoder->state.context)->dictionary_size = dictionary_size;
461
 
 
462
 
    return (PyObject*) encoder;
463
 
}
464
 
#endif
465
 
 
466
 
 
467
 
/* -------------------------------------------------------------------- */
468
 
/* JPEG                                                                 */
469
 
/* -------------------------------------------------------------------- */
470
 
 
471
 
#ifdef HAVE_LIBJPEG
472
 
 
473
 
/* We better define this encoder last in this file, so the following
474
 
   undef's won't mess things up for the Imaging library proper. */
475
 
 
476
 
#undef  HAVE_PROTOTYPES
477
 
#undef  HAVE_STDDEF_H
478
 
#undef  HAVE_STDLIB_H
479
 
#undef  UINT8
480
 
#undef  UINT16
481
 
#undef  UINT32
482
 
#undef  INT8
483
 
#undef  INT16
484
 
#undef  INT32
485
 
 
486
 
#include "Jpeg.h"
487
 
 
488
 
PyObject*
489
 
PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
490
 
{
491
 
    ImagingEncoderObject* encoder;
492
 
 
493
 
    char *mode;
494
 
    char *rawmode;
495
 
    int quality = 0;
496
 
    int progressive = 0;
497
 
    int smooth = 0;
498
 
    int optimize = 0;
499
 
    int streamtype = 0; /* 0=interchange, 1=tables only, 2=image only */
500
 
    int xdpi = 0, ydpi = 0;
501
 
    int subsampling = -1; /* -1=default, 0=none, 1=medium, 2=high */
502
 
    char* extra = NULL; int extra_size;
503
 
    if (!PyArg_ParseTuple(args, "ss|iiiiiiiis#", &mode, &rawmode, &quality,
504
 
                          &progressive, &smooth, &optimize, &streamtype,
505
 
                          &xdpi, &ydpi, &subsampling, &extra, &extra_size))
506
 
        return NULL;
507
 
 
508
 
    encoder = PyImaging_EncoderNew(sizeof(JPEGENCODERSTATE));
509
 
    if (encoder == NULL)
510
 
        return NULL;
511
 
 
512
 
    if (get_packer(encoder, mode, rawmode) < 0)
513
 
        return NULL;
514
 
 
515
 
    if (extra && extra_size > 0) {
516
 
        char* p = malloc(extra_size);
517
 
        if (!p)
518
 
            return PyErr_NoMemory();
519
 
        memcpy(p, extra, extra_size);
520
 
        extra = p;
521
 
    } else
522
 
        extra = NULL;
523
 
 
524
 
    encoder->encode = ImagingJpegEncode;
525
 
 
526
 
    ((JPEGENCODERSTATE*)encoder->state.context)->quality = quality;
527
 
    ((JPEGENCODERSTATE*)encoder->state.context)->subsampling = subsampling;
528
 
    ((JPEGENCODERSTATE*)encoder->state.context)->progressive = progressive;
529
 
    ((JPEGENCODERSTATE*)encoder->state.context)->smooth = smooth;
530
 
    ((JPEGENCODERSTATE*)encoder->state.context)->optimize = optimize;
531
 
    ((JPEGENCODERSTATE*)encoder->state.context)->streamtype = streamtype;
532
 
    ((JPEGENCODERSTATE*)encoder->state.context)->xdpi = xdpi;
533
 
    ((JPEGENCODERSTATE*)encoder->state.context)->ydpi = ydpi;
534
 
    ((JPEGENCODERSTATE*)encoder->state.context)->extra = extra;
535
 
    ((JPEGENCODERSTATE*)encoder->state.context)->extra_size = extra_size;
536
 
 
537
 
    return (PyObject*) encoder;
538
 
}
539
 
 
540
 
#endif