fixed -d (--scale) parameter
[swftools.git] / lib / python / image.c
1 #include <Python.h>
2 //#include "/usr/include/python2.3/Imaging.h"
3 #include <Imaging.h>
4 #include "pyutils.h"
5 #undef HAVE_STAT
6 #include "../rfxswf.h"
7
8 /* redefine the ImagingObject struct defined in _imagingmodule.c */
9 /* there should be a better way to do this... */
10 typedef struct {
11     PyObject_HEAD
12     Imaging image;
13 } ImagingObject;
14
15 int image_getWidth(PyObject*_image) {
16     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
17         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
18         return 0;
19     }
20     ImagingObject*image = (ImagingObject*)_image;
21     return image->image->xsize;
22 }
23
24 int image_getHeight(PyObject*_image) {
25     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
26         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
27         return 0;
28     }
29     ImagingObject*image = (ImagingObject*)_image;
30     return image->image->ysize;
31 }
32
33 int image_getBPP(PyObject*_image) {
34     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
35         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
36         return 0;
37     }
38     ImagingObject*image = (ImagingObject*)_image;
39     if(!strcmp(image->image->mode, "1") ||
40        !strcmp(image->image->mode, "L") ||
41        !strcmp(image->image->mode, "P")) {
42         return 8;
43     }
44     if(!strcmp(image->image->mode, "I") ||
45        !strcmp(image->image->mode, "F")) {
46         return 32;
47     }
48     if(!strcmp(image->image->mode, "RGB") ||
49        !strcmp(image->image->mode, "RGBA") ||
50        !strcmp(image->image->mode, "CMYK") ||
51        !strcmp(image->image->mode, "YCbCr")) {
52         return 32;
53     }
54     PyErr_SetString(PyExc_Exception, setError("Unknown image format (%s).", image->image->mode));
55     return 0;
56 }
57
58 RGBA* image_toRGBA(PyObject*_image) 
59 {
60     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
61         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
62         return 0;
63     }
64     ImagingObject*image = (ImagingObject*)_image;
65     printf("mode: %s\n", image->image->mode);
66     printf("depth: %d\n", image->image->depth);
67     printf("bands: %d\n", image->image->bands);
68     printf("xsize: %d\n", image->image->xsize);
69     printf("ysize: %d\n", image->image->ysize);
70     int bpp = image_getBPP(_image);
71     if(!bpp)
72         return 0;
73         
74     RGBA*rgba = (RGBA*)malloc(image->image->xsize * image->image->ysize * sizeof(RGBA));
75
76     if(!strcmp(image->image->mode, "RGBA")) {
77         int y,ymax=image->image->ysize;
78         int width = image->image->xsize;
79         RGBA*dest = rgba;
80         for(y=0;y<ymax;y++) {
81             U8* src = (U8*)(image->image->image32[y]);
82             int x;
83             for(x=0;x<width;x++) {
84                 dest[x].r = src[x*4+0];
85                 dest[x].g = src[x*4+1];
86                 dest[x].b = src[x*4+2];
87                 dest[x].a = src[x*4+3];
88             }
89             dest+=width;
90         }
91         return rgba;    
92     }
93         
94     PyErr_SetString(PyExc_Exception, setError("Unsupported image format: %s (try .convert(\"RGBA\")", image->image->mode));
95     return 0;
96 }