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