made imaging optional
[swftools.git] / lib / python / image.c
1 #include <Python.h>
2 #ifdef HAVE_STAT
3 #undef HAVE_STAT
4 #endif
5 //#include "/usr/include/python2.3/Imaging.h"
6 #include "../../config.h"
7 #ifdef HAVE_PYTHON_IMAGING
8 #include <Imaging.h>
9 #endif
10 #include "pyutils.h"
11 #undef HAVE_STAT
12 #include "../rfxswf.h"
13
14 /* redefine the ImagingObject struct defined in _imagingmodule.c */
15 /* there should be a better way to do this... */
16 typedef struct {
17     PyObject_HEAD
18 #ifdef HAVE_PYTHON_IMAGING
19     Imaging image;
20 #endif
21 } ImagingObject;
22
23 int image_getWidth(PyObject*_image) {
24 #ifdef HAVE_PYTHON_IMAGING
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->xsize;
31 #else
32     PyErr_SetString(PyExc_Exception, "imaging not compiled in");
33     return 0;
34 #endif
35 }
36
37 int image_getHeight(PyObject*_image) {
38 #ifdef HAVE_PYTHON_IMAGING
39     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
40         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
41         return 0;
42     }
43     ImagingObject*image = (ImagingObject*)_image;
44     return image->image->ysize;
45 #else
46     PyErr_SetString(PyExc_Exception, "imaging not compiled in");
47     return 0;
48 #endif
49 }
50
51 int image_getBPP(PyObject*_image) {
52 #ifdef HAVE_PYTHON_IMAGING
53     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
54         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
55         return 0;
56     }
57     ImagingObject*image = (ImagingObject*)_image;
58     if(!strcmp(image->image->mode, "1") ||
59        !strcmp(image->image->mode, "L") ||
60        !strcmp(image->image->mode, "P")) {
61         return 8;
62     }
63     if(!strcmp(image->image->mode, "I") ||
64        !strcmp(image->image->mode, "F")) {
65         return 32;
66     }
67     if(!strcmp(image->image->mode, "RGB") ||
68        !strcmp(image->image->mode, "RGBA") ||
69        !strcmp(image->image->mode, "CMYK") ||
70        !strcmp(image->image->mode, "YCbCr")) {
71         return 32;
72     }
73     PyErr_SetString(PyExc_Exception, setError("Unknown image format (%s).", image->image->mode));
74     return 0;
75 #else
76     PyErr_SetString(PyExc_Exception, "imaging not compiled in");
77     return 0;
78 #endif
79 }
80
81 RGBA* image_toRGBA(PyObject*_image) 
82 {
83 #ifdef HAVE_PYTHON_IMAGING
84     if(strcmp(_image->ob_type->tp_name, "ImagingCore")) {
85         PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name));
86         return 0;
87     }
88     ImagingObject*image = (ImagingObject*)_image;
89     printf("mode: %s\n", image->image->mode);
90     printf("depth: %d\n", image->image->depth);
91     printf("bands: %d\n", image->image->bands);
92     printf("xsize: %d\n", image->image->xsize);
93     printf("ysize: %d\n", image->image->ysize);
94     int bpp = image_getBPP(_image);
95     if(!bpp)
96         return 0;
97         
98     RGBA*rgba = (RGBA*)malloc(image->image->xsize * image->image->ysize * sizeof(RGBA));
99
100     if(!strcmp(image->image->mode, "RGBA")) {
101         int y,ymax=image->image->ysize;
102         int width = image->image->xsize;
103         RGBA*dest = rgba;
104         for(y=0;y<ymax;y++) {
105             U8* src = (U8*)(image->image->image32[y]);
106             int x;
107             for(x=0;x<width;x++) {
108                 dest[x].r = src[x*4+0];
109                 dest[x].g = src[x*4+1];
110                 dest[x].b = src[x*4+2];
111                 dest[x].a = src[x*4+3];
112             }
113             dest+=width;
114         }
115         return rgba;    
116     }
117         
118     PyErr_SetString(PyExc_Exception, setError("Unsupported image format: %s (try .convert(\"RGBA\")", image->image->mode));
119 #else
120     PyErr_SetString(PyExc_Exception, "imaging not compiled in");
121 #endif
122     return 0;
123 }
124
125 #ifdef HAVE_PYTHON_IMAGING
126 extern PyObject*PyImagingNew(Imaging imOut);
127 #endif
128
129 PyObject* rgba_to_image(RGBA*rgba, int width, int height)
130 {
131 #ifdef HAVE_PYTHON_IMAGING
132 #ifndef WIN32
133     Imaging img = ImagingNew("RGBA", width, height);
134     int y;
135     if(!img->image32) {
136         fprintf(stderr, "No array allocated!\n");
137         return 0;
138     }
139     for(y=0;y<height;y++) {
140         U8* dest = (U8*)(img->image32[y]);
141         RGBA* src = &rgba[width*y];
142         int x;
143         for(x=0;x<width;x++) {
144             dest[x+0] = src[x].r;
145             dest[x+1] = src[x].g;
146             dest[x+2] = src[x].b;
147             dest[x+3] = src[x].a;
148         }
149     }
150
151     return PyImagingNew(img);
152 #else
153     fprintf(stderr, "This image extraction is not yet supported on non-linux systems\n");
154     return 0;
155 #endif
156 #else
157     PyErr_SetString(PyExc_Exception, "imaging not compiled in");
158     return 0;
159 #endif
160 }