~edwin-grubbs/python-imaging/trunk

« back to all changes in this revision

Viewing changes to _imaging.c

  • Committer: effbot
  • Date: 2006-03-01 19:11:48 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:18
Load Imaging-1.1.3 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * The Python Imaging Library.
3
 
 * $Id$
 
3
 * $Id: //modules/pil/_imaging.c#11 $
4
4
 *
5
5
 * the imaging library bindings
6
6
 *
45
45
 * 1999-01-10 fl   Added some experimental arrow graphics stuff
46
46
 * 1999-02-06 fl   Added draw_bitmap, font acceleration stuff
47
47
 * 2001-04-17 fl   Fixed some egcs compiler nits
 
48
 * 2001-09-17 fl   Added screen grab primitives (win32)
 
49
 * 2002-03-09 fl   Added stretch primitive
 
50
 * 2002-03-10 fl   
48
51
 *
49
 
 * Copyright (c) 1997-2001 by Secret Labs AB 
50
 
 * Copyright (c) 1995-2001 by Fredrik Lundh
 
52
 * Copyright (c) 1997-2002 by Secret Labs AB 
 
53
 * Copyright (c) 1995-2002 by Fredrik Lundh
51
54
 *
52
55
 * See the README file for information on usage and redistribution.
53
56
 */
390
393
    int r, g, b, a;
391
394
    double f;
392
395
 
 
396
    /* fill ink buffer (four bytes) with something that can
 
397
       be cast to either UINT8 or INT32 */
 
398
 
393
399
    if (im->image8) {
394
400
        /* unsigned integer, single layer */
395
401
        r = PyInt_AsLong(color);
396
402
        if (r == -1 && PyErr_Occurred())
397
403
            return NULL;
398
404
        ink[0] = CLIP(r);
 
405
       ink[1] = ink[2] = ink[3] = 0;
399
406
        return ink;
400
407
    } else {
401
408
        switch (im->type) {
571
578
{
572
579
    char* mode;
573
580
    int dither = 0;
574
 
    if (!PyArg_ParseTuple(args, "s|i", &mode, &dither))
575
 
        return NULL;
576
 
 
577
 
    return PyImagingNew(ImagingConvert(self->image, mode, NULL, dither));
 
581
    ImagingObject *paletteimage = NULL;
 
582
 
 
583
    if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage))
 
584
        return NULL;
 
585
    if (paletteimage != NULL) {
 
586
      if (!PyImaging_Check(paletteimage)) {
 
587
        PyObject_Print((PyObject *)paletteimage, stderr, 0);
 
588
        PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'");
 
589
        return NULL;
 
590
      }
 
591
      if (paletteimage->image->palette == NULL) {
 
592
        PyErr_SetString(PyExc_ValueError, "null palette");
 
593
        return NULL;
 
594
      }
 
595
    }
 
596
 
 
597
    return PyImagingNew(ImagingConvert(self->image, mode, paletteimage ? paletteimage->image->palette : NULL, dither));
578
598
}
579
599
 
580
600
static PyObject* 
1175
1195
    if (theta < 0.0)
1176
1196
        theta += 360;
1177
1197
 
1178
 
    if (filter && imIn->type == IMAGING_TYPE_SPECIAL) {
 
1198
    if (filter && imIn->type != IMAGING_TYPE_SPECIAL) {
1179
1199
        /* Rotate with resampling filter */
1180
1200
        imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
1181
1201
        ImagingRotate(imOut, imIn, theta, filter);
1207
1227
}
1208
1228
 
1209
1229
static PyObject* 
 
1230
_stretch(ImagingObject* self, PyObject* args)
 
1231
{
 
1232
    Imaging imIn;
 
1233
    Imaging imTemp;
 
1234
    Imaging imOut;
 
1235
 
 
1236
    int xsize, ysize;
 
1237
    int filter = IMAGING_TRANSFORM_NEAREST;
 
1238
    if (!PyArg_ParseTuple(args, "(ii)|i", &xsize, &ysize, &filter))
 
1239
        return NULL;
 
1240
 
 
1241
    imIn = self->image;
 
1242
 
 
1243
    /* two-pass resize: minimize size of intermediate image */
 
1244
    if (imIn->xsize * ysize < xsize * imIn->ysize)
 
1245
        imTemp = ImagingNew(imIn->mode, imIn->xsize, ysize);
 
1246
    else 
 
1247
        imTemp = ImagingNew(imIn->mode, xsize, imIn->ysize);
 
1248
    if (!imTemp)
 
1249
        return NULL;
 
1250
 
 
1251
    /* first pass */
 
1252
    if (!ImagingStretch(imTemp, imIn, filter)) {
 
1253
        ImagingDelete(imTemp);
 
1254
        return NULL;
 
1255
    }
 
1256
 
 
1257
    imOut = ImagingNew(imIn->mode, xsize, ysize);
 
1258
    if (!imOut) {
 
1259
        ImagingDelete(imTemp);
 
1260
        return NULL;
 
1261
    }
 
1262
 
 
1263
    /* second pass */
 
1264
    if (!ImagingStretch(imOut, imTemp, filter)) {
 
1265
        ImagingDelete(imOut);
 
1266
        ImagingDelete(imTemp);
 
1267
        return NULL;
 
1268
    }
 
1269
 
 
1270
    ImagingDelete(imTemp);
 
1271
    
 
1272
    return PyImagingNew(imOut);
 
1273
}
 
1274
 
 
1275
static PyObject* 
1210
1276
_transform2(ImagingObject* self, PyObject* args)
1211
1277
{
1212
1278
    static const char* wrong_number = "wrong number of matrix entries";
1681
1747
    if (!PyArg_ParseTuple(args, "s", &text))
1682
1748
        return NULL;
1683
1749
 
1684
 
    im = ImagingNew("1", textwidth(self, text), self->ysize);
 
1750
    im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
1685
1751
    if (!im)
1686
1752
        return NULL;
1687
1753
 
1740
1806
static PyObject* 
1741
1807
_draw_ink(ImagingObject* self, PyObject* args)
1742
1808
{
1743
 
    char ink[4];
 
1809
    INT32 ink = 0;
1744
1810
    PyObject* color;
1745
1811
 
1746
1812
    if (!PyArg_ParseTuple(args, "O", &color))
1747
1813
        return NULL;
1748
1814
 
1749
 
    if (!getink(color, self->image, ink))
 
1815
    if (!getink(color, self->image, (char*) &ink))
1750
1816
        return NULL;
1751
1817
 
1752
 
    if (self->image->image8)
1753
 
        return Py_BuildValue("i", *(UINT8*) ink);
1754
 
    else
1755
 
        return Py_BuildValue("i", *(INT32*) ink);
 
1818
    return Py_BuildValue("i", (int) ink);
1756
1819
}
1757
1820
 
1758
1821
static PyObject* 
2201
2264
#endif
2202
2265
    {"resize", (PyCFunction)_resize, 1},
2203
2266
    {"rotate", (PyCFunction)_rotate, 1},
 
2267
    {"stretch", (PyCFunction)_stretch, 1},
2204
2268
    {"transpose", (PyCFunction)_transpose, 1},
2205
2269
    {"transform2", (PyCFunction)_transform2, 1},
2206
2270
 
2376
2440
/* Display support (in display.c) */
2377
2441
extern PyObject* PyImaging_DisplayWin32(PyObject* self, PyObject* args);
2378
2442
extern PyObject* PyImaging_DisplayModeWin32(PyObject* self, PyObject* args);
 
2443
extern PyObject* PyImaging_GrabScreenWin32(PyObject* self, PyObject* args);
2379
2444
 
2380
2445
/* Experimental path stuff (in path.c) */
2381
2446
extern PyObject* PyPath_Create(ImagingObject* self, PyObject* args);
2436
2501
#ifdef WIN32
2437
2502
    {"display", (PyCFunction)PyImaging_DisplayWin32, 1},
2438
2503
    {"display_mode", (PyCFunction)PyImaging_DisplayModeWin32, 1},
 
2504
    {"grabscreen", (PyCFunction)PyImaging_GrabScreenWin32, 1},
2439
2505
#endif
2440
2506
 
2441
2507
    /* Utilities */
2472
2538
    {NULL, NULL} /* sentinel */
2473
2539
};
2474
2540
 
2475
 
void
2476
 
#ifdef WIN32
2477
 
__declspec(dllexport)
2478
 
#endif
 
2541
DL_EXPORT(void)
2479
2542
init_imaging(void)
2480
2543
{
2481
2544
    /* Patch object type */