File: basisu_encoder_pybind11.cpp

package info (click to toggle)
basis-universal 2.0.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 216,436 kB
  • sloc: cpp: 163,224; ansic: 51,368; python: 2,824; javascript: 2,637; lisp: 1,026; sh: 161; makefile: 17
file content (109 lines) | stat: -rw-r--r-- 3,426 bytes parent folder | download
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
// File: basisu_encoder_pybind11.cpp
// pybind11 native bindings for the compressor's pure C API basisu_wasm_api.h
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <stdint.h>

// include the basisu compression plain C API
#include "../encoder/basisu_wasm_api.h"   

namespace py = pybind11;

// Convert wasm_bool_t (uint32_t) ? Python bool
static inline bool to_bool(uint32_t v) { return v != 0; }

PYBIND11_MODULE(basisu_python, m) {
    m.doc() = "Native Basis Universal encoder (pybind11 binding over basisu_wasm_api)";

    //
    // Initialization / Version
    //
    m.def("init", &bu_init, "Initialize the BasisU codec library");
    m.def("get_version", &bu_get_version, "Return BASISU_LIB_VERSION");

    //
    // Memory allocation helpers
    //
    m.def("alloc", &bu_alloc,
          "Allocate memory inside native heap and return pointer as uint64");
    m.def("free", &bu_free,
          "Free previously allocated pointer");

    //
    // Compression params handles
    //
    m.def("new_params", &bu_new_comp_params,
          "Create a new comp_params struct inside native heap");
    m.def("delete_params",
          [](uint64_t h) { return to_bool(bu_delete_comp_params(h)); },
          "Destroy a comp_params struct");

    m.def("params_clear",
          [](uint64_t h) { return to_bool(bu_comp_params_clear(h)); },
          "Clear comp_params struct");

    //
    // Image upload API
    //
    m.def("set_image_rgba32",
          [](uint64_t params, uint32_t index,
             uint64_t img_ptr, uint32_t w, uint32_t h, uint32_t pitch) {
              return to_bool(bu_comp_params_set_image_rgba32(
                  params, index, img_ptr, w, h, pitch));
          },
          "Set 8-bit RGBA32 image into parameters");

    m.def("set_image_float_rgba",
          [](uint64_t params, uint32_t index,
             uint64_t img_ptr, uint32_t w, uint32_t h, uint32_t pitch) {
              return to_bool(bu_comp_params_set_image_float_rgba(
                  params, index, img_ptr, w, h, pitch));
          },
          "Set float32 RGBA image into parameters");

    //
    // Compression
    //
	m.def("compress",
      [](uint64_t params,
         int tex_format,
         int quality,
         int effort,
         uint64_t flags,
         float rdo_quality)
      {
          return to_bool(bu_compress_texture(
              params, tex_format, quality, effort, flags, rdo_quality));
      },
      py::arg("params"),
      py::arg("tex_format"),
      py::arg("quality"),
      py::arg("effort"),
      py::arg("flags"),
      py::arg("rdo_quality") = 0.0f
	);		  

    //
    // Output blob access
    //
    m.def("get_comp_data_size",
          &bu_comp_params_get_comp_data_size,
          "Return size (bytes) of compressed output");
    m.def("get_comp_data_ofs",
          &bu_comp_params_get_comp_data_ofs,
          "Return pointer (uint64) to compressed output buffer");
		  
	// Memory read/write
	m.def("read_memory",
	    [](uint64_t ptr, uint32_t size) {
	        return py::bytes((const char*)ptr, size);
	    },
	    "Read `size` bytes starting at native memory address `ptr`");
	
	m.def("write_memory",
	    [](uint64_t dest_ptr, py::buffer src) {
	        py::buffer_info info = src.request();
	        memcpy((void*)dest_ptr, info.ptr, info.size * info.itemsize);
	    },
	    "Write bytes/buffer-like object into native memory at address `ptr`");
}