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