|
1 | 1 | # defold-webp |
2 | | -Defold native extension for WebP images |
| 2 | + |
| 3 | +A Defold extension for using WebP images. Uses [libwebp-1.6.0](https://github.com/webmproject/libwebp). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +This asset can be added as a [library dependency](https://defold.com/manuals/libraries/#setting-up-library-dependencies) in your project: |
| 8 | + |
| 9 | +* Use a specific version for development and release to avoid breaking changes: https://github.com/HalfstarDev/defold-webp/releases |
| 10 | +* Use the latest version only while evaluating and testing the asset: https://github.com/HalfstarDev/defold-webp/archive/master.zip |
| 11 | + |
| 12 | +For Android, the minimum SDK version is 21. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +#### webp.decode(image [, options]) |
| 17 | + |
| 18 | +Decode a WebP image into an image buffer. Use optional Lua table with [WebPDecoderOptions](https://developers.google.com/speed/webp/docs/api#advanced_decoding_api) as keys. Returns buffer, width, and height. |
| 19 | + |
| 20 | +``` |
| 21 | +local buf, w, h = webp.decode(sys.load_resource("/assets/image_1.webp")) |
| 22 | +resource.set_texture(go.get("#sprite", "texture0"), { |
| 23 | + width = w, |
| 24 | + height = h, |
| 25 | + type = resource.TEXTURE_TYPE_2D, |
| 26 | + format = resource.TEXTURE_FORMAT_RGBA, |
| 27 | + num_mip_maps = 1 |
| 28 | +}, buf) |
| 29 | +``` |
| 30 | + |
| 31 | +#### webp.encode(buffer, width, height [, options]) |
| 32 | + |
| 33 | +Encode a image buffer into a WebP image. Use optional Lua table with [WebPConfig](https://developers.google.com/speed/webp/docs/api#advanced_encoding_api) fields as keys. Returns string with WebP image. |
| 34 | + |
| 35 | +``` |
| 36 | +local buf, w, h = webp.decode(image) |
| 37 | +local webp_image = webp.encode(buf, w, h, {quality = 50}) |
| 38 | +local f = io.open("out.webp", "wb") |
| 39 | +if f then |
| 40 | + f:write(webp_image) |
| 41 | + f:close() |
| 42 | +end |
| 43 | +``` |
| 44 | + |
| 45 | +#### webp.get_info(image) |
| 46 | + |
| 47 | +Get information about a WebP image. Returns table with following fields: width, height, has_alpha, has_animation, lossy. |
| 48 | +``` |
| 49 | +local info = webp.get_info(image) |
| 50 | +print(info.width, info.height) |
| 51 | +``` |
0 commit comments