| 1 |
#include "pycolmap/sensor/bitmap.h" |
| 2 |
|
| 3 |
#include "pycolmap/helpers.h" |
| 4 |
#include "pycolmap/pybind11_extension.h" |
| 5 |
|
| 6 |
#include <filesystem> |
| 7 |
|
| 8 |
#include <pybind11/eigen.h> |
| 9 |
#include <pybind11/pybind11.h> |
| 10 |
|
| 11 |
using namespace colmap; |
| 12 |
using namespace pybind11::literals; |
| 13 |
namespace py = pybind11; |
| 14 |
|
| 15 |
#ifdef _MSC_VER // If compiling with MSVC |
| 16 |
#include <stddef.h> |
| 17 |
typedef ptrdiff_t ssize_t; |
| 18 |
#endif |
| 19 |
|
| 20 |
py::array_t<uint8_t> ArrayFromBitmap(const Bitmap& self) { |
| 21 |
std::vector<ssize_t> shape = {static_cast<ssize_t>(self.Height()), |
| 22 |
static_cast<ssize_t>(self.Width())}; |
| 23 |
const auto channels = static_cast<ssize_t>(self.Channels()); |
| 24 |
if (channels != 1) { |
| 25 |
if (channels != 3) { |
| 26 |
throw std::runtime_error( |
| 27 |
"Can only convert grayscale or 3-channel RGB image to " |
| 28 |
"array"); |
| 29 |
} |
| 30 |
shape.push_back(channels); |
| 31 |
} |
| 32 |
py::array_t<uint8_t> output(shape); |
| 33 |
py::buffer_info output_into = output.request(); |
| 34 |
uint8_t* output_row_ptr = reinterpret_cast<uint8_t*>(output.request().ptr); |
| 35 |
std::memcpy(output_row_ptr, self.RowMajorData().data(), self.NumBytes()); |
| 36 |
return output; |
| 37 |
} |
| 38 |
|
| 39 |
Bitmap BitmapFromArray(py::array_t<uint8_t, py::array::c_style> array, |
| 40 |
bool linear_colorspace) { |
| 41 |
int channels = 1; |
| 42 |
if (array.ndim() == 3) { |
| 43 |
channels = array.shape(2); |
| 44 |
} else if (array.ndim() != 2) { |
| 45 |
throw std::runtime_error("Input array must have 2 or 3 dimensions!"); |
| 46 |
} |
| 47 |
|
| 48 |
const int width = array.shape(1); |
| 49 |
const int height = array.shape(0); |
| 50 |
if (width == 0 || height == 0) { |
| 51 |
throw std::runtime_error( |
| 52 |
"Input array must have positive width and height!"); |
| 53 |
} |
| 54 |
|
| 55 |
if (channels != 1 && channels != 3) { |
| 56 |
throw std::runtime_error("Input array must have 1 or 3 channels!"); |
| 57 |
} |
| 58 |
|
| 59 |
const bool as_rgb = channels != 1; |
| 60 |
Bitmap output(width, |
| 61 |
height, |
| 62 |
/*as_rgb=*/as_rgb, |
| 63 |
/*linear_colorspace=*/linear_colorspace); |
| 64 |
|
| 65 |
const uint8_t* input_row_ptr = static_cast<uint8_t*>(array.request().ptr); |
| 66 |
std::memcpy(output.RowMajorData().data(), input_row_ptr, output.NumBytes()); |
| 67 |
return output; |
| 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, |
| 128 |
"Rescale image to the new dimensions.") |
| 129 |
.def("thumbnail", |
| 130 |
&Bitmap::Thumbnail, |
| 131 |
"max_image_size"_a, |
| 132 |
"filter"_a = BitmapRescaleFilter::kBilinear, |
| 133 |
"Downscale the image so neither dimension exceeds max_image_size, " |
| 134 |
"preserving the aspect ratio. Images already within the bound are " |
| 135 |
"left unchanged. Returns the applied scale factor (1 if no " |
| 136 |
"rescaling was necessary).") |
| 137 |
.def("rot90", |
| 138 |
&Bitmap::Rot90, |
| 139 |
"Rotate image by k * 90 degrees counter-clockwise.") |
| 140 |
.def_property_readonly("width", &Bitmap::Width, "Width of the image.") |
| 141 |
.def_property_readonly("height", &Bitmap::Height, "Height of the image.") |
| 142 |
.def_property_readonly( |
| 143 |
"channels", &Bitmap::Channels, "Number of channels of the image.") |
| 144 |
.def_property_readonly( |
| 145 |
"is_rgb", &Bitmap::IsRGB, "Whether the image is colorscale.") |
| 146 |
.def_property_readonly( |
| 147 |
"is_grey", &Bitmap::IsGrey, "Whether the image is greyscale.") |
| 148 |
.def_property_readonly( |
| 149 |
"is_empty", &Bitmap::IsEmpty, "Whether the image is empty.") |
| 150 |
.def_property_readonly( |
| 151 |
"bits_per_pixel", |
| 152 |
&Bitmap::BitsPerPixel, |
| 153 |
"Number of bits per pixel (8 for grey, 24 for RGB).") |
| 154 |
.def_property_readonly( |
| 155 |
"pitch", &Bitmap::Pitch, "Scan line size in bytes (stride).") |
| 156 |
.def("clone", &Bitmap::Clone, "Clone the image to a new bitmap.") |
| 157 |
.def("clone_as_grey", |
| 158 |
&Bitmap::CloneAsGrey, |
| 159 |
"Clone the image as grayscale.") |
| 160 |
.def("clone_as_rgb", &Bitmap::CloneAsRGB, "Clone the image as RGB.") |
| 161 |
.def( |
| 162 |
"set_jpeg_quality", |
| 163 |
&Bitmap::SetJpegQuality, |
| 164 |
"quality"_a, |
| 165 |
"Set compression quality when writing to JPEG in the range [1, 100]. " |
| 166 |
"Lower values reduce quality and file size. By default, bitmaps are " |
| 167 |
"written in superb (100) quality, if not otherwise specified.") |
| 168 |
.def("exif_orientation", |
| 169 |
&Bitmap::ExifOrientation, |
| 170 |
"Extract EXIF orientation. Returns None if not available.") |
| 171 |
.def("exif_camera_model", |
| 172 |
&Bitmap::ExifCameraModel, |
| 173 |
"Extract EXIF camera model. Returns None if not available.") |
| 174 |
.def("exif_focal_length", |
| 175 |
&Bitmap::ExifFocalLength, |
| 176 |
"Extract EXIF focal length. Returns None if not available.") |
| 177 |
.def("exif_latitude", |
| 178 |
&Bitmap::ExifLatitude, |
| 179 |
"Extract EXIF latitude. Returns None if not available.") |
| 180 |
.def("exif_longitude", |
| 181 |
&Bitmap::ExifLongitude, |
| 182 |
"Extract EXIF longitude. Returns None if not available.") |
| 183 |
.def("exif_altitude", |
| 184 |
&Bitmap::ExifAltitude, |
| 185 |
"Extract EXIF altitude. Returns None if not available.") |
| 186 |
.def("__repr__", &CreateRepresentation<Bitmap>); |
| 187 |
} |