New comit of SDL2
[supertux.git] / src / SDL2 / external / libwebp-0.3.0 / swig / libwebp.i
1 // Copyright 2011 Google Inc.
2 //
3 // This code is licensed under the same terms as WebM:
4 //  Software License Agreement:  http://www.webmproject.org/license/software/
5 //  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
7 //
8 // libwebp swig interface definition
9 //
10 // Author: James Zern (jzern@google.com)
11
12 /*
13   Java bindings:
14   $ mkdir -p java/com/google/webp
15   $ swig -java \
16          -package com.google.webp \
17          -outdir java/com/google/webp \
18          -o libwebp_java_wrap.c libwebp.i
19
20   Python bindings:
21   $ swig -python \
22          -outdir . \
23          -o libwebp_python_wrap.c libwebp.i
24 */
25
26 #ifdef SWIGPYTHON
27 %module(package="com.google.webp") libwebp
28 #else
29 %module libwebp
30 #endif  /* SWIGPYTHON */
31
32 %include "constraints.i"
33 %include "typemaps.i"
34
35 #ifdef SWIGJAVA
36 %include "arrays_java.i";
37 %include "enums.swg" /*NB: requires JDK-1.5+
38                        See: http://www.swig.org/Doc1.3/Java.html#enumerations */
39
40 // map uint8_t* such that a byte[] is used
41 %{
42 #include "webp/types.h"
43 %}
44 // from arrays_java.i (signed char)
45 JAVA_ARRAYS_DECL(uint8_t, jbyte, Byte, Uint8)
46 JAVA_ARRAYS_IMPL(uint8_t, jbyte, Byte, Uint8)
47 JAVA_ARRAYS_TYPEMAPS(uint8_t, byte, jbyte, Uint8, "[B")
48 %apply uint8_t[] { uint8_t* }
49 #endif  /* SWIGJAVA */
50
51 #ifdef SWIGPYTHON
52 %apply (char* STRING, size_t LENGTH) { (const uint8_t* data, size_t data_size) }
53 %typemap(out) uint8_t* {
54   $result = PyString_FromStringAndSize(
55       (const char*)$1,
56       ($1 == NULL) ? 0 : ReturnedBufferSize("$symname", arg3, arg4));
57 }
58
59 %typemap (in) const uint8_t* rgb (Py_buffer rgb_buffer) {
60   // NB: with Python < 2.6 the old style buffer protocol may be used:
61   // Py_ssize_t unused;
62   // PyObject_AsReadBuffer($input, (const void**)(&$1), &unused);
63   if (!PyObject_CheckBuffer($input)) {
64     SWIG_exception_fail(SWIG_TypeError,
65                         "in method '$symname', argument $argnum"
66                         " does not support the buffer interface");
67   }
68   if (PyObject_GetBuffer($input, &rgb_buffer, PyBUF_SIMPLE)) {
69     SWIG_exception_fail(SWIG_RuntimeError,
70                         "in method '$symname', unable to get buffer view");
71   }
72   $1 = ($1_ltype)rgb_buffer.buf;
73 }
74
75 %typemap(freearg) const uint8_t* rgb {
76   PyBuffer_Release(&rgb_buffer$argnum);
77 }
78
79 %define DECODE_AUTODOC(func)
80 %feature("autodoc", #func "(uint8_t data) -> (rgb, width, height)") func;
81 %enddef
82
83 %feature("autodoc", "1");
84 DECODE_AUTODOC(WebPDecodeRGB);
85 DECODE_AUTODOC(WebPDecodeRGBA);
86 DECODE_AUTODOC(WebPDecodeARGB);
87 DECODE_AUTODOC(WebPDecodeBGR);
88 DECODE_AUTODOC(WebPDecodeBGRA);
89 %feature("autodoc", "WebPGetInfo(uint8_t data) -> (width, height)") WebPGetInfo;
90 #endif  /* SWIGPYTHON */
91
92 //------------------------------------------------------------------------------
93 // Decoder specific
94
95 %apply int* OUTPUT { int* width, int* height }
96
97 // free the buffer returned by these functions after copying into
98 // the native type
99 %newobject WebPDecodeRGB;
100 %newobject WebPDecodeRGBA;
101 %newobject WebPDecodeARGB;
102 %newobject WebPDecodeBGR;
103 %newobject WebPDecodeBGRA;
104 %typemap(newfree) uint8_t* "free($1);"
105
106 int WebPGetDecoderVersion(void);
107 int WebPGetInfo(const uint8_t* data, size_t data_size,
108                 int* width, int* height);
109
110 uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
111                        int* width, int* height);
112 uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
113                         int* width, int* height);
114 uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
115                         int* width, int* height);
116 uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
117                        int* width, int* height);
118 uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
119                         int* width, int* height);
120
121 //------------------------------------------------------------------------------
122 // Encoder specific
123
124 int WebPGetEncoderVersion(void);
125
126 //------------------------------------------------------------------------------
127 // Wrapper code additions
128
129 %{
130 #include "webp/decode.h"
131 #include "webp/encode.h"
132 %}
133
134 #ifdef SWIGJAVA
135 %{
136 #define FillMeInAsSizeCannotBeDeterminedAutomatically \
137     (result ? (jint)ReturnedBufferSize(__FUNCTION__, arg3, arg4) : 0)
138 %}
139 #endif  /* SWIGJAVA */
140
141 #if defined(SWIGJAVA) || defined(SWIGPYTHON)
142 %{
143 static size_t ReturnedBufferSize(
144     const char* function, int* width, int* height) {
145   static const struct sizemap {
146     const char* function;
147     int size_multiplier;
148   } size_map[] = {
149 #ifdef SWIGJAVA
150     { "Java_com_google_webp_libwebpJNI_WebPDecodeRGB",  3 },
151     { "Java_com_google_webp_libwebpJNI_WebPDecodeRGBA", 4 },
152     { "Java_com_google_webp_libwebpJNI_WebPDecodeARGB", 4 },
153     { "Java_com_google_webp_libwebpJNI_WebPDecodeBGR",  3 },
154     { "Java_com_google_webp_libwebpJNI_WebPDecodeBGRA", 4 },
155     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeRGB",  1 },
156     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeBGR",  1 },
157     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeRGBA", 1 },
158     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeBGRA", 1 },
159     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeLosslessRGB",  1 },
160     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeLosslessBGR",  1 },
161     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeLosslessRGBA", 1 },
162     { "Java_com_google_webp_libwebpJNI_wrap_1WebPEncodeLosslessBGRA", 1 },
163 #endif
164 #ifdef SWIGPYTHON
165     { "WebPDecodeRGB",  3 },
166     { "WebPDecodeRGBA", 4 },
167     { "WebPDecodeARGB", 4 },
168     { "WebPDecodeBGR",  3 },
169     { "WebPDecodeBGRA", 4 },
170     { "wrap_WebPEncodeRGB",  1 },
171     { "wrap_WebPEncodeBGR",  1 },
172     { "wrap_WebPEncodeRGBA", 1 },
173     { "wrap_WebPEncodeBGRA", 1 },
174     { "wrap_WebPEncodeLosslessRGB",  1 },
175     { "wrap_WebPEncodeLosslessBGR",  1 },
176     { "wrap_WebPEncodeLosslessRGBA", 1 },
177     { "wrap_WebPEncodeLosslessBGRA", 1 },
178 #endif
179     { NULL, 0 }
180   };
181   const struct sizemap* p;
182   size_t size = 0;
183
184   for (p = size_map; p->function; ++p) {
185     if (!strcmp(function, p->function)) {
186       size = *width * *height * p->size_multiplier;
187       break;
188     }
189   }
190
191   return size;
192 }
193 %}
194 #endif  /* SWIGJAVA || SWIGPYTHON */
195
196 %{
197 typedef size_t (*WebPEncodeFunction)(const uint8_t* rgb,
198                                      int width, int height, int stride,
199                                      float quality_factor, uint8_t** output);
200 typedef size_t (*WebPEncodeLosslessFunction)(const uint8_t* rgb,
201                                              int width, int height, int stride,
202                                              uint8_t** output);
203
204 static uint8_t* EncodeLossy(const uint8_t* rgb,
205                             int width, int height, int stride,
206                             float quality_factor,
207                             WebPEncodeFunction encfn,
208                             int* output_size, int* unused) {
209   uint8_t* output = NULL;
210   const size_t image_size =
211       encfn(rgb, width, height, stride, quality_factor, &output);
212   // the values of following two will be interpreted by ReturnedBufferSize()
213   // as 'width' and 'height' in the size calculation.
214   *output_size = image_size;
215   *unused = 1;
216   return image_size ? output : NULL;
217 }
218
219 static uint8_t* EncodeLossless(const uint8_t* rgb,
220                                int width, int height, int stride,
221                                WebPEncodeLosslessFunction encfn,
222                                int* output_size, int* unused) {
223   uint8_t* output = NULL;
224   const size_t image_size = encfn(rgb, width, height, stride, &output);
225   // the values of the following two will be interpreted by
226   // ReturnedBufferSize() as 'width' and 'height' in the size calculation.
227   *output_size = image_size;
228   *unused = 1;
229   return image_size ? output : NULL;
230 }
231 %}
232
233 //------------------------------------------------------------------------------
234 // libwebp/encode wrapper functions
235
236 %apply int* INPUT { int* unused1, int* unused2 }
237 %apply int* OUTPUT { int* output_size }
238
239 // free the buffer returned by these functions after copying into
240 // the native type
241 %newobject wrap_WebPEncodeRGB;
242 %newobject wrap_WebPEncodeBGR;
243 %newobject wrap_WebPEncodeRGBA;
244 %newobject wrap_WebPEncodeBGRA;
245 %newobject wrap_WebPEncodeLosslessRGB;
246 %newobject wrap_WebPEncodeLosslessBGR;
247 %newobject wrap_WebPEncodeLosslessRGBA;
248 %newobject wrap_WebPEncodeLosslessBGRA;
249
250 #ifdef SWIGJAVA
251 // There's no reason to call these directly
252 %javamethodmodifiers wrap_WebPEncodeRGB "private";
253 %javamethodmodifiers wrap_WebPEncodeBGR "private";
254 %javamethodmodifiers wrap_WebPEncodeRGBA "private";
255 %javamethodmodifiers wrap_WebPEncodeBGRA "private";
256 %javamethodmodifiers wrap_WebPEncodeLosslessRGB "private";
257 %javamethodmodifiers wrap_WebPEncodeLosslessBGR "private";
258 %javamethodmodifiers wrap_WebPEncodeLosslessRGBA "private";
259 %javamethodmodifiers wrap_WebPEncodeLosslessBGRA "private";
260 #endif  /* SWIGJAVA */
261
262 #ifdef SWIGPYTHON
263 // This autodoc will serve as a catch-all for wrap_*.
264 %feature("autodoc", "private, do not call directly.");
265 #endif
266
267 %inline %{
268 // Changes the return type of WebPEncode* to more closely match Decode*.
269 // This also makes it easier to wrap the output buffer in a native type rather
270 // than dealing with the return pointer.
271 // The additional parameters are to allow reuse of ReturnedBufferSize(),
272 // unused2 and output_size will be used in this case.
273 #define LOSSY_WRAPPER(FUNC)                                             \
274   static uint8_t* wrap_##FUNC(                                          \
275       const uint8_t* rgb, int* unused1, int* unused2, int* output_size, \
276       int width, int height, int stride, float quality_factor) {        \
277     return EncodeLossy(rgb, width, height, stride, quality_factor,      \
278                        FUNC, output_size, unused2);                     \
279   }                                                                     \
280
281 LOSSY_WRAPPER(WebPEncodeRGB)
282 LOSSY_WRAPPER(WebPEncodeBGR)
283 LOSSY_WRAPPER(WebPEncodeRGBA)
284 LOSSY_WRAPPER(WebPEncodeBGRA)
285
286 #undef LOSSY_WRAPPER
287
288 #define LOSSLESS_WRAPPER(FUNC)                                          \
289   static uint8_t* wrap_##FUNC(                                          \
290       const uint8_t* rgb, int* unused1, int* unused2, int* output_size, \
291       int width, int height, int stride) {                              \
292     return EncodeLossless(rgb, width, height, stride,                   \
293                           FUNC, output_size, unused2);                  \
294   }                                                                     \
295
296 LOSSLESS_WRAPPER(WebPEncodeLosslessRGB)
297 LOSSLESS_WRAPPER(WebPEncodeLosslessBGR)
298 LOSSLESS_WRAPPER(WebPEncodeLosslessRGBA)
299 LOSSLESS_WRAPPER(WebPEncodeLosslessBGRA)
300
301 #undef LOSSLESS_WRAPPER
302
303 %}
304
305 //------------------------------------------------------------------------------
306 // Language specific
307
308 #ifdef SWIGJAVA
309 %{
310 /* Work around broken gcj jni.h */
311 #ifdef __GCJ_JNI_H__
312 # undef JNIEXPORT
313 # define JNIEXPORT
314 # undef JNICALL
315 # define JNICALL
316 #endif
317 %}
318
319 %pragma(java) modulecode=%{
320   private static final int UNUSED = 1;
321   private static int outputSize[] = { 0 };
322 %}
323
324
325 %define CALL_ENCODE_LOSSY_WRAPPER(func)
326 %pragma(java) modulecode=%{
327   public static byte[] func(
328       byte[] rgb, int width, int height, int stride, float quality_factor) {
329     return wrap_##func(
330         rgb, UNUSED, UNUSED, outputSize, width, height, stride, quality_factor);
331   }
332 %}
333 %enddef
334
335 %define CALL_ENCODE_LOSSLESS_WRAPPER(func)
336 %pragma(java) modulecode=%{
337   public static byte[] func(
338       byte[] rgb, int width, int height, int stride) {
339     return wrap_##func(
340         rgb, UNUSED, UNUSED, outputSize, width, height, stride);
341   }
342 %}
343 %enddef
344
345 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeRGB)
346 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeRGBA)
347 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeBGR)
348 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeBGRA)
349 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessRGB)
350 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessRGBA)
351 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessBGR)
352 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessBGRA)
353 #endif  /* SWIGJAVA */
354
355 #ifdef SWIGPYTHON
356 %pythoncode %{
357 _UNUSED = 1
358 %}
359
360 %define CALL_ENCODE_LOSSY_WRAPPER(func)
361 %pythoncode %{
362 def func(rgb, width, height, stride, quality_factor):
363   """func(uint8_t rgb, int width, int height, int stride, float quality_factor) -> lossy_webp"""
364   webp = wrap_##func(
365       rgb, _UNUSED, _UNUSED, width, height, stride, quality_factor)
366   if len(webp[0]) == 0:
367     return None
368   return webp[0]
369 %}
370 %enddef
371
372 %define CALL_ENCODE_LOSSLESS_WRAPPER(func)
373 %pythoncode %{
374 def func(rgb, width, height, stride):
375   """func(uint8_t rgb, int width, int height, int stride) -> lossless_webp"""
376   webp = wrap_##func(rgb, _UNUSED, _UNUSED, width, height, stride)
377   if len(webp[0]) == 0:
378     return None
379   return webp[0]
380 %}
381 %enddef
382
383 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeRGB)
384 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeRGBA)
385 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeBGR)
386 CALL_ENCODE_LOSSY_WRAPPER(WebPEncodeBGRA)
387 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessRGB)
388 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessRGBA)
389 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessBGR)
390 CALL_ENCODE_LOSSLESS_WRAPPER(WebPEncodeLosslessBGRA)
391 #endif  /* SWIGPYTHON */