Skip to content
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
26 changes: 25 additions & 1 deletion lib/include/elements/support/pixmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ namespace cycfi { namespace elements
using std::runtime_error::runtime_error;
};

enum class pixel_format {
invalid = -1,
rgba32,
};

class pixmap
{
public:
Expand All @@ -47,6 +52,13 @@ namespace cycfi { namespace elements
friend class canvas;
friend class pixmap_context;

template <pixel_format fmt>
friend typename std::enable_if<fmt == pixel_format::rgba32, pixmap>::type
make_pixmap(std::uint8_t const *data, extent size, float scale);

explicit pixmap(std::uint8_t const *data, pixel_format fmt, extent size,
float scale);

cairo_surface_t* _surface;
};

Expand Down Expand Up @@ -101,6 +113,18 @@ namespace cycfi { namespace elements
}
return *this;
}
}}

//--------------------------------------------------------------------------
// Inlines
//--------------------------------------------------------------------------

template <pixel_format fmt>
inline typename std::enable_if<fmt == pixel_format::rgba32, pixmap>::type
make_pixmap(std::uint8_t const *data, extent size, float scale=1)
{
return pixmap(reinterpret_cast<std::uint8_t const *>(data), fmt, size, scale);
}

}}

#endif
50 changes: 50 additions & 0 deletions lib/src/support/pixmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <elements/support/detail/stb_image.h>
#include <infra/assert.hpp>
#include <infra/filesystem.hpp>
#include <bit>
#include <string>
#include <fstream>

Expand Down Expand Up @@ -108,6 +109,55 @@ namespace cycfi { namespace elements
cairo_surface_mark_dirty(_surface);
}

pixmap::pixmap(uint8_t const *data, pixel_format fmt, extent size, float scale)
: _surface(nullptr)
{
if (fmt == pixel_format::invalid)
throw std::runtime_error{"Error: Cannot initialize format: INVALID"};

if (fmt != pixel_format::rgba32)
throw std::runtime_error{"Error: Format not supported"};

_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size.x, size.y);
if (!_surface)
throw std::runtime_error{"Failed to create pixmap."};

uint8_t *dest_data = cairo_image_surface_get_data(_surface);
size_t src_stride = size.x * 4;
size_t dest_stride = cairo_image_surface_get_stride(_surface);

for (int y = 0; y != size.y; ++y)
{
uint8_t const *src = data + (y * src_stride);
uint8_t *dest = dest_data + (y * dest_stride);
for (int x = 0; x != size.x; ++x)
{
// RGBA32 to ARGB32
if constexpr (std::endian::native == std::endian::big)
{
dest[0] = src[3]; // alpha
dest[1] = src[0]; // red
dest[2] = src[1]; // green
dest[3] = src[2]; // blue
}
else if constexpr (std::endian::native == std::endian::little)
{
dest[0] = src[1]; // blue
dest[1] = src[2]; // green
dest[2] = src[3]; // red
dest[3] = src[0]; // alpha
}

src += 4;
dest += 4;
}
}

// Set scale and flag the surface as dirty
cairo_surface_set_device_scale(_surface, 1 / scale, 1 / scale);
cairo_surface_mark_dirty(_surface);
}

pixmap::~pixmap()
{
if (_surface)
Expand Down