| 68 | } |
| 69 | |
| 70 | void BindBitmap(pybind11::module& m) { |
| 71 | using BitmapRescaleFilter = Bitmap::RescaleFilter; |
| 72 | py::enum_<BitmapRescaleFilter> PyRescaleFilter(m, "BitmapRescaleFilter"); |
| 73 | PyRescaleFilter.value("BILINEAR", BitmapRescaleFilter::kBilinear) |
| 74 | .value("BOX", BitmapRescaleFilter::kBox); |
| 75 | AddStringToEnumConstructor(PyRescaleFilter); |
| 76 | |
| 77 | py::classh<Bitmap>(m, "Bitmap") |
| 78 | .def(py::init<>()) |
| 79 | .def(py::init<>( |
| 80 | [](int width, int height, bool as_rgb, bool linear_colorspace) { |
| 81 | return Bitmap(width, |
| 82 | height, |
| 83 | /*as_rgb=*/as_rgb, |
| 84 | /*linear_colorspace=*/linear_colorspace); |
| 85 | }), |
| 86 | "width"_a, |
| 87 | "height"_a, |
| 88 | "as_rgb"_a, |
| 89 | "linear_colorspace"_a = false) |
| 90 | .def("to_array", &ArrayFromBitmap) |
| 91 | .def_static("from_array", |
| 92 | &BitmapFromArray, |
| 93 | "array"_a, |
| 94 | "linear_colorspace"_a = false, |
| 95 | "Create bitmap as a copy of array. Returns RGB bitmap, " |
| 96 | "if array has shape (H, W, 3), or grayscale bitmap, if " |
| 97 | "array has shape (H, W[, 1]).") |
| 98 | .def("write", |
| 99 | &Bitmap::Write, |
| 100 | "path"_a, |
| 101 | "delinearize_colorspace"_a = true, |
| 102 | "Write bitmap to file at given path. Defaults to converting to " |
| 103 | "sRGB colorspace for file storage.") |
| 104 | .def_static( |
| 105 | "read", |
| 106 | [](const std::filesystem::path& path, |
| 107 | bool as_rgb, |
| 108 | bool linearize_colorspace) -> py::typing::Optional<Bitmap> { |
| 109 | Bitmap bitmap; |
| 110 | if (!bitmap.Read(path, |
| 111 | /*as_rgb=*/as_rgb, |
| 112 | /*linearize_colorspace=*/linearize_colorspace)) { |
| 113 | return py::none(); |
| 114 | } |
| 115 | return py::cast(bitmap); |
| 116 | }, |
| 117 | "path"_a, |
| 118 | "as_rgb"_a, |
| 119 | "linearize_colorspace"_a = false, |
| 120 | "Read bitmap at given path and convert to grey- or colorscale. " |
| 121 | "Defaults to keeping the original colorspace (potentially " |
| 122 | "non-linear) for image processing.") |
| 123 | .def("rescale", |
| 124 | &Bitmap::Rescale, |
| 125 | "new_width"_a, |
| 126 | "new_height"_a, |
| 127 | "filter"_a = BitmapRescaleFilter::kBilinear, |
no test coverage detected