1 // Copyright 2011 Google Inc.
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 // -----------------------------------------------------------------------------
8 // libwebp swig interface definition
10 // Author: James Zern (jzern@google.com)
14 $ mkdir -p java/com/google/webp
16 -package com.google.webp \
17 -outdir java/com/google/webp \
18 -o libwebp_java_wrap.c libwebp.i
23 -o libwebp_python_wrap.c libwebp.i
27 %module(package="com.google.webp") libwebp
30 #endif /* SWIGPYTHON */
32 %include "constraints.i"
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 */
40 // map uint8_t* such that a byte[] is used
42 #include "webp/types.h"
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* }
52 %apply (char* STRING, size_t LENGTH) { (const uint8_t* data, size_t data_size) }
53 %typemap(out) uint8_t* {
54 $result = PyString_FromStringAndSize(
56 ($1 == NULL) ? 0 : ReturnedBufferSize("$symname", arg3, arg4));
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:
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");
68 if (PyObject_GetBuffer($input, &rgb_buffer, PyBUF_SIMPLE)) {
69 SWIG_exception_fail(SWIG_RuntimeError,
70 "in method '$symname', unable to get buffer view");
72 $1 = ($1_ltype)rgb_buffer.buf;
75 %typemap(freearg) const uint8_t* rgb {
76 PyBuffer_Release(&rgb_buffer$argnum);
79 %define DECODE_AUTODOC(func)
80 %feature("autodoc", #func "(uint8_t data) -> (rgb, width, height)") func;
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 */
92 //------------------------------------------------------------------------------
95 %apply int* OUTPUT { int* width, int* height }
97 // free the buffer returned by these functions after copying into
99 %newobject WebPDecodeRGB;
100 %newobject WebPDecodeRGBA;
101 %newobject WebPDecodeARGB;
102 %newobject WebPDecodeBGR;
103 %newobject WebPDecodeBGRA;
104 %typemap(newfree) uint8_t* "free($1);"
106 int WebPGetDecoderVersion(void);
107 int WebPGetInfo(const uint8_t* data, size_t data_size,
108 int* width, int* height);
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);
121 //------------------------------------------------------------------------------
124 int WebPGetEncoderVersion(void);
126 //------------------------------------------------------------------------------
127 // Wrapper code additions
130 #include "webp/decode.h"
131 #include "webp/encode.h"
136 #define FillMeInAsSizeCannotBeDeterminedAutomatically \
137 (result ? (jint)ReturnedBufferSize(__FUNCTION__, arg3, arg4) : 0)
139 #endif /* SWIGJAVA */
141 #if defined(SWIGJAVA) || defined(SWIGPYTHON)
143 static size_t ReturnedBufferSize(
144 const char* function, int* width, int* height) {
145 static const struct sizemap {
146 const char* function;
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 },
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 },
181 const struct sizemap* p;
184 for (p = size_map; p->function; ++p) {
185 if (!strcmp(function, p->function)) {
186 size = *width * *height * p->size_multiplier;
194 #endif /* SWIGJAVA || SWIGPYTHON */
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,
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;
216 return image_size ? output : NULL;
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;
229 return image_size ? output : NULL;
233 //------------------------------------------------------------------------------
234 // libwebp/encode wrapper functions
236 %apply int* INPUT { int* unused1, int* unused2 }
237 %apply int* OUTPUT { int* output_size }
239 // free the buffer returned by these functions after copying into
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;
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 */
263 // This autodoc will serve as a catch-all for wrap_*.
264 %feature("autodoc", "private, do not call directly.");
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); \
281 LOSSY_WRAPPER(WebPEncodeRGB)
282 LOSSY_WRAPPER(WebPEncodeBGR)
283 LOSSY_WRAPPER(WebPEncodeRGBA)
284 LOSSY_WRAPPER(WebPEncodeBGRA)
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); \
296 LOSSLESS_WRAPPER(WebPEncodeLosslessRGB)
297 LOSSLESS_WRAPPER(WebPEncodeLosslessBGR)
298 LOSSLESS_WRAPPER(WebPEncodeLosslessRGBA)
299 LOSSLESS_WRAPPER(WebPEncodeLosslessBGRA)
301 #undef LOSSLESS_WRAPPER
305 //------------------------------------------------------------------------------
310 /* Work around broken gcj jni.h */
319 %pragma(java) modulecode=%{
320 private static final int UNUSED = 1;
321 private static int outputSize[] = { 0 };
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) {
330 rgb, UNUSED, UNUSED, outputSize, width, height, stride, quality_factor);
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) {
340 rgb, UNUSED, UNUSED, outputSize, width, height, stride);
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 */
360 %define CALL_ENCODE_LOSSY_WRAPPER(func)
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"""
365 rgb, _UNUSED, _UNUSED, width, height, stride, quality_factor)
366 if len(webp[0]) == 0:
372 %define CALL_ENCODE_LOSSLESS_WRAPPER(func)
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:
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 */