Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sophus2: image proto + some API updates #848

Open
wants to merge 1 commit into
base: pango2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
with:
path: ${{runner.workspace}}/Pangolin/Sophus
repository: 'strasdat/Sophus'
ref: 5cb40f05e84ab5b8cc05ff1e1ae080bc9b61bed2
ref: a4070695b4bca492647a7966e4cb50f90a44ec95

- name: Checkout Pangolin
uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion components/pango_context/src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ struct ContextImpl : public Context {
const auto maybe_gl_pixel_type = glTypeInfo(pixel_type);
const GlFormatInfo gl_pixel_type = SOPHUS_UNWRAP(maybe_gl_pixel_type);

IntensityImage<> image(ImageSize(imsize[0], imsize[1]), pixel_type);
auto image = IntensityImage<>::fromFormat(
ImageSize(imsize[0], imsize[1]), pixel_type);

glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_FRONT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct GlDrawnImage : public DrawnImage {
if (color_transform) {
u_color_transform = color_transform->cast<float>();
} else if (
image->pixelFormat().num_channels == 1 &&
image->pixelFormat().num_components == 1 &&
u_colormap_index.getValue() == Palette::none) {
u_color_transform =
(Eigen::Matrix4f() << 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct GlDrawnPrimitives : public DrawnPrimitives {

for (int i = 0; i < 4; ++i) {
vao.addVertexAttrib(
location_vertex + i, *vertices, i * data_type.bytesPerPixel());
location_vertex + i, *vertices, i * data_type.numBytesPerPixel());
}

PANGO_GL(glDrawArrays(GL_POINTS, 0, vertices->size() - 3));
Expand Down
4 changes: 2 additions & 2 deletions components/pango_image/src/image_io_jpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ IntensityImage<> LoadJpg(std::istream& is)
// resize storage if necessary
PixelFormat fmt =
PixelFormatFromString(cinfo.output_components == 3 ? "RGB24" : "GRAY8");
image = sophus::IntensityImage<>(
image = sophus::IntensityImage<>::fromFormat(
ImageSize(cinfo.output_width, cinfo.output_height), fmt);
JSAMPARRAY imageBuffer = (*cinfo.mem->alloc_sarray)(
(j_common_ptr)&cinfo, JPOOL_IMAGE,
Expand Down Expand Up @@ -289,7 +289,7 @@ void SaveJpg(const IntensityImage<>& img, std::ostream& os, float quality)
if (img.numChannels() != 1 && img.numChannels() != 3) {
throw std::runtime_error("Unsupported number of image channels.");
}
if (img.numBytesPerPixelChannel() != 1) {
if (img.pixelFormat().num_bytes_per_component != 1) {
throw std::runtime_error("Unsupported image depth.");
}

Expand Down
3 changes: 2 additions & 1 deletion components/pango_image/src/image_io_lz4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ IntensityImage<> LoadLz4(std::istream& in)
lz4_image_header header;
in.read((char*)&header, sizeof(header));

IntensityImage<> img(
auto img = IntensityImage<>::fromFormat(
ImageSize(header.w, header.h), PixelFormatFromString(header.fmt));

std::unique_ptr<char[]> input_buffer(new char[header.compressed_size]);

in.read(input_buffer.get(), header.compressed_size);
Expand Down
6 changes: 3 additions & 3 deletions components/pango_image/src/image_io_packed12bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct packed12bit_image_header {

void SavePacked12bit(const IntensityImage<>& image, std::ostream& out)
{
if (image.numBytesPerPixelChannel() != 2) {
if (image.pixelFormat().num_bytes_per_component != 2) {
throw std::runtime_error(
"packed12bit currently only supported with 16bit input image");
}
Expand Down Expand Up @@ -62,10 +62,10 @@ IntensityImage<> LoadPacked12bit(std::istream& in)
packed12bit_image_header header;
in.read((char*)&header, sizeof(header));

IntensityImage<> img(
auto img = IntensityImage<>::fromFormat(
sophus::ImageSize(header.w, header.h), PixelFormatFromString(header.fmt));

if (img.numBytesPerPixelChannel() != 2) {
if (img.pixelFormat().num_bytes_per_component != 2) {
throw std::runtime_error(
"packed12bit currently only supported with 16bit input image");
}
Expand Down
4 changes: 2 additions & 2 deletions components/pango_image/src/image_io_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ IntensityImage<> LoadPng(std::istream& source)

auto shape = sophus::ImageLayout::makeFromSizeAndPitch<uint8_t>(
sophus::ImageSize(w, h), pitch);
IntensityImage<> img(shape, PngFormat(png_ptr, info_ptr));
auto img = IntensityImage<>::fromFormat(shape, PngFormat(png_ptr, info_ptr));

png_bytepp rows = png_get_rows(png_ptr, info_ptr);
for (unsigned int r = 0; r < h; r++) {
Expand Down Expand Up @@ -191,7 +191,7 @@ void SavePng(
png_ptr, (png_voidp)&stream, pango_png_stream_write,
pango_png_stream_write_flush);

const int bit_depth = image.numBytesPerPixelChannel() * 8;
const int bit_depth = image.pixelFormat().num_bytes_per_component * 8;

int colour_type;
switch (image.numChannels()) {
Expand Down
2 changes: 1 addition & 1 deletion components/pango_image/src/image_io_ppm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ IntensityImage<> LoadPpm(std::istream& in)
in.ignore(1, '\n');

if (!in.fail() && w > 0 && h > 0) {
IntensityImage<> img(
auto img = IntensityImage<>::fromFormat(
sophus::ImageSize(w, h), PpmFormat(ppm_type, num_colors));

// Read in data
Expand Down
6 changes: 3 additions & 3 deletions components/pango_image/src/image_io_raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ IntensityImage<> LoadImageNonPlanar(
{
ImageLayout shape = ImageLayout::makeFromSizeAndPitch<uint8_t>(
ImageSize(raw_width, raw_height), raw_pitch);
IntensityImage<> img(shape, raw_fmt);
auto img = IntensityImage<>::fromFormat(shape, raw_fmt);

// Read from file, row at a time.
std::ifstream bFile(filename.c_str(), std::ios::in | std::ios::binary);
Expand All @@ -33,11 +33,11 @@ template <typename Tin, typename Tout>
IntensityImage<> ToNonPlanarT(const IntensityImageView& planar_image)
{
PixelFormat new_fmt = PixelFormat::fromTemplate<Tout>();
const size_t planes = new_fmt.num_channels;
const size_t planes = new_fmt.num_components;

PANGO_ENSURE(planar_image.height() % planes == 0);
PANGO_ENSURE(sizeof(Tin) * planes == sizeof(Tout));
PANGO_ENSURE(sizeof(Tout) == new_fmt.bytesPerPixel());
PANGO_ENSURE(sizeof(Tout) == new_fmt.numBytesPerPixel());

ImageView<Tin> in = planar_image.imageView<Tin>();
MutImage<Tout> out(
Expand Down
2 changes: 1 addition & 1 deletion components/pango_image/src/image_io_tga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ IntensityImage<> LoadTga(std::istream& in)
const int height = info[2] + (info[3] * 256);

if (in.good()) {
IntensityImage<> img(
auto img = IntensityImage<>::fromFormat(
sophus::ImageSize(width, height), TgaFormat(info[4], type[2], type[1]));

// read in image data
Expand Down
2 changes: 1 addition & 1 deletion components/pango_image/src/image_io_tiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ IntensityImage<> LoadTiff(const std::string& filename)
"image_io_tiff.cpp.");
}

IntensityImage<> image(
auto image = IntensityImage<>::fromFormat(
sophus::ImageSize(width, height), PixelFormatFromString(sfmt.c_str()));
const tsize_t scanlength_bytes = TIFFScanlineSize(tif);
if (scanlength_bytes != tsize_t(image.pitchBytes()))
Expand Down
7 changes: 4 additions & 3 deletions components/pango_image/src/image_io_zstd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ void SaveZstd(
ZSTD_isError(initResult), "ZSTD_initCStream() error : {}",
ZSTD_getErrorName(initResult));

const size_t row_size_bytes =
image.numChannels() * image.numBytesPerPixelChannel() * image.width();
const size_t row_size_bytes = image.numChannels() *
image.pixelFormat().num_bytes_per_component *
image.width();

for (int y = 0; y < image.height(); ++y) {
ZSTD_inBuffer input = {image.rawRowPtr(y), row_size_bytes, 0};
Expand Down Expand Up @@ -98,7 +99,7 @@ IntensityImage<> LoadZstd(std::istream& in)
zstd_image_header header;
in.read((char*)&header, sizeof(header));

IntensityImage<> img(
auto img = IntensityImage<>::fromFormat(
ImageSize(header.w, header.h), PixelFormatFromString(header.fmt));

const size_t input_buffer_size = ZSTD_DStreamInSize();
Expand Down
45 changes: 30 additions & 15 deletions components/pango_opengl/include/pangolin/gl/gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,67 +256,82 @@ inline void GlTexture::Download(sophus::IntensityImage<>& image) const

switch (internal_format) {
case GL_LUMINANCE8:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY8"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY8"));
Download(unsafe_ptr, GL_RED, GL_UNSIGNED_BYTE);
break;
case GL_LUMINANCE16:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY16LE"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY16LE"));
Download(unsafe_ptr, GL_RED, GL_UNSIGNED_SHORT);
break;
case GL_RGB8:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGB24"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGB24"));
Download(unsafe_ptr, GL_RGB, GL_UNSIGNED_BYTE);
break;
case GL_RGBA8:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGBA32"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGBA32"));
Download(unsafe_ptr, GL_RGBA, GL_UNSIGNED_BYTE);
break;
case GL_RED_INTEGER:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY32"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY32"));
Download(unsafe_ptr, GL_RED, GL_UNSIGNED_INT);
break;
case GL_LUMINANCE:
case GL_LUMINANCE32F_ARB:
case GL_R32F:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY32F"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY32F"));
Download(unsafe_ptr, GL_RED, GL_FLOAT);
break;
case GL_RGB16:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGB48"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGB48"));
Download(unsafe_ptr, GL_RGB, GL_UNSIGNED_SHORT);
break;
case GL_RGBA16:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGBA64"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGBA64"));
Download(unsafe_ptr, GL_RGBA, GL_UNSIGNED_SHORT);
break;
case GL_RGB16F:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGB48F"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGB48F"));
Download(unsafe_ptr, GL_RGB, GL_HALF_FLOAT);
break;
case GL_RGBA16F:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGBA64F"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGBA64F"));
Download(unsafe_ptr, GL_RGBA, GL_HALF_FLOAT);
break;
case GL_RGB:
case GL_RGB32F:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGB96F"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGB96F"));
Download(unsafe_ptr, GL_RGB, GL_FLOAT);
break;
case GL_RGBA:
case GL_RGBA32F:
image = sophus::IntensityImage<>(size, PixelFormatFromString("RGBA128F"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("RGBA128F"));
Download(unsafe_ptr, GL_RGBA, GL_FLOAT);
break;
case GL_DEPTH_COMPONENT16:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY16LE"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY16LE"));
Download(unsafe_ptr, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT);
break;
case GL_DEPTH_COMPONENT24:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY32"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY32"));
Download(unsafe_ptr, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
break;
case GL_DEPTH_COMPONENT32F:
image = sophus::IntensityImage<>(size, PixelFormatFromString("GRAY32F"));
image = sophus::IntensityImage<>::fromFormat(
size, PixelFormatFromString("GRAY32F"));
Download(unsafe_ptr, GL_DEPTH_COMPONENT, GL_FLOAT);
break;
default:
Expand Down
7 changes: 4 additions & 3 deletions components/pango_opengl/include/pangolin/gl/gl_type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ inline farm_ng::Expected<GlFormatInfo> glTypeInfo(
};

// Make sure we'll be in bounds...
const int nbytes = pixel_type.num_bytes_per_pixel_channel;
const int nbytes = pixel_type.num_bytes_per_component;
const bool bfixed = pixel_type.number_type == sophus::NumberType::fixed_point;

if (between(pixel_type.num_channels, 1, 4) && isOneOf(nbytes, {1, 2, 4})) {
const int cidx = pixel_type.num_channels - 1;
if (between(int(pixel_type.num_components), 1, 4) &&
isOneOf(nbytes, {1, 2, 4})) {
const int cidx = pixel_type.num_components - 1;
const int fidx = (nbytes == 4 && bfixed) ? 2 : nbytes - 1;
return GlFormatInfo(
{.gl_sized_format = format_table[fidx][cidx],
Expand Down
4 changes: 2 additions & 2 deletions components/pango_opengl/include/pangolin/gl/glpixformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct GlPixFormat {

GlPixFormat(const sophus::PixelFormat& fmt)
{
switch (fmt.num_channels) {
switch (fmt.num_components) {
case 1:
glformat = GL_RED;
break;
Expand All @@ -63,7 +63,7 @@ struct GlPixFormat {

const bool is_integral = fmt.number_type == sophus::NumberType::fixed_point;

switch (fmt.num_bytes_per_pixel_channel) {
switch (fmt.num_bytes_per_component) {
case 1:
gltype = GL_UNSIGNED_BYTE;
break;
Expand Down
2 changes: 1 addition & 1 deletion components/pango_opengl/src/gl_device_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct DeviceGlBuffer : public DeviceBuffer {

size_t elementSizeBytes() const
{
return data_type_ ? data_type_->bytesPerPixel() : 0;
return data_type_ ? data_type_->numBytesPerPixel() : 0;
}

GLenum buffer_type_ = 0;
Expand Down
4 changes: 2 additions & 2 deletions components/pango_opengl/src/gl_device_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ struct DeviceGlTexture : public DeviceTexture {
}

// Upload data
const size_t stride = u.image.pitchBytes() / data_type.bytesPerPixel();
const size_t stride = u.image.pitchBytes() / data_type.numBytesPerPixel();
PANGO_CHECK(
stride * data_type.bytesPerPixel() == u.image.pitchBytes(),
stride * data_type.numBytesPerPixel() == u.image.pitchBytes(),
"Image pitch is not a multiple of the pixel size (which OpenGL "
"requires).");
PANGO_GL(glPixelStorei(GL_UNPACK_ROW_LENGTH, stride));
Expand Down
4 changes: 2 additions & 2 deletions components/pango_opengl/src/gl_vao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ void GlVertexArrayObject::addVertexAttrib(

if (isGlIntegralDatatype(gl_fmt.gl_type)) {
PANGO_GL(glVertexAttribIPointer(
attrib_location, data_type.num_channels, gl_fmt.gl_type, stride_bytes,
attrib_location, data_type.num_components, gl_fmt.gl_type, stride_bytes,
(void*)offset_bytes));
} else {
PANGO_GL(glVertexAttribPointer(
attrib_location, data_type.num_channels, gl_fmt.gl_type, normalized,
attrib_location, data_type.num_components, gl_fmt.gl_type, normalized,
stride_bytes, (void*)offset_bytes));
}
PANGO_GL(glEnableVertexAttribArray(attrib_location));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct DeviceBuffer {
virtual void reserve(size_t) const = 0;

// size of allocated buffer in bytes. Will be at least `capacity() *
// dataType().bytesPerPixel()`.
// dataType().numBytesPerPixel()`.
virtual size_t sizeBytes() const = 0;

// Queue upload of data into position `dest_element` of device buffer. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ struct SharedDataPackage {
using C = Eigen::Matrix<float, kRows, kCols>;
data_type = {
.number_type = sophus::NumberType::floating_point,
.num_channels = int(matrix.rows()),
.num_bytes_per_pixel_channel = sizeof(float),
.num_components = uint8_t(matrix.rows()),
.num_bytes_per_component = sizeof(float),
};
num_elements = matrix.cols();
data = makeTypeErasedSharedPtr(std::forward<C>(matrix));
Expand Down
9 changes: 5 additions & 4 deletions components/pango_video/include/pangolin/video/stream_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace pangolin
class PANGOLIN_EXPORT StreamInfo
{
public:
inline StreamInfo() : layout_(0, 0, 0), offset_bytes_(0) {}
inline StreamInfo() : layout_(), offset_bytes_(0) {}

inline StreamInfo(
sophus::PixelFormat fmt, const sophus::ImageLayout shape,
Expand All @@ -52,7 +52,7 @@ class PANGOLIN_EXPORT StreamInfo

inline size_t rowBytes() const
{
return format().bytesPerPixel() * layout().width();
return format().numBytesPerPixel() * layout().width();
}

//! Return Image wrapper around raw base pointer
Expand All @@ -69,11 +69,12 @@ class PANGOLIN_EXPORT StreamInfo
inline sophus::IntensityImage<> copyToDynImage(const uint8_t* base_ptr) const
{
PANGO_DEBUG("Unneeded image copy happening...");
sophus::IntensityImage<> runtime(layout_, fmt_);
sophus::IntensityImage<> runtime =
sophus::IntensityImage<>::fromFormat(layout_, fmt_);
sophus::details::pitchedCopy(
const_cast<uint8_t*>(runtime.rawPtr()), runtime.layout().pitchBytes(),
base_ptr, layout_.pitchBytes(), layout_.imageSize(),
fmt_.bytesPerPixel());
fmt_.numBytesPerPixel());
return runtime;
}

Expand Down
Loading