Spin out wuffs_aux Alloc{Pixbuf,Workbuf}Result

This commit is contained in:
Nigel Tao committed 2021-04-02 23:57:18 +11:00
1 parent 163bc2626c
commit 314d265fe8
7 files changed
+287 -145

No files matched your search

+5 -4
View File
@@ -86,7 +86,7 @@ wuffs_base__color_u32_argb_premul g_background_colors[NUM_BACKGROUND_COLORS] = {
uint32_t g_width = 0;
uint32_t g_height = 0;
wuffs_aux::MemOwner g_pixbuf_mem_owner(nullptr, &free);
wuffs_base__slice_u8 g_pixbuf_mem_slice = {0};
wuffs_base__pixel_buffer g_pixbuf = {0};
uint32_t g_background_color_index = 0;
bool //
@@ -106,7 +106,7 @@ load_image(const char* filename) {
g_width = 0;
g_height = 0;
g_pixbuf_mem_owner.reset();
g_pixbuf_mem_slice = wuffs_base__empty_slice_u8();
g_pixbuf = wuffs_base__null_pixel_buffer();
wuffs_aux::DecodeImageCallbacks callbacks;
wuffs_aux::sync_io::FileInput input(file);
@@ -123,7 +123,7 @@ load_image(const char* filename) {
g_width = res.pixbuf.pixcfg.width();
g_height = res.pixbuf.pixcfg.height();
g_pixbuf_mem_owner = std::move(res.pixbuf_mem_owner);
g_pixbuf_mem_slice = res.pixbuf_mem_slice;
g_pixbuf = res.pixbuf;
if (res.error_message.empty()) {
printf("%s: ok (%" PRIu32 " x %" PRIu32 ")\n", adj_filename, g_width,
@@ -193,11 +193,12 @@ load(xcb_connection_t* c,
if (!load_image(filename)) {
return false;
}
wuffs_base__table_u8 tab = g_pixbuf.plane(0);
xcb_create_pixmap(c, s->root_depth, g_pixmap, w, g_width, g_height);
xcb_image_t* image = xcb_image_create_native(
c, g_width, g_height, XCB_IMAGE_FORMAT_Z_PIXMAP, s->root_depth, NULL,
g_pixbuf_mem_slice.len, g_pixbuf_mem_slice.ptr);
tab.height * tab.stride, tab.ptr);
xcb_image_put(c, g_pixmap, g, image, 0, 0, 0);
xcb_image_destroy(image);
return true;
+17 -7
View File
@@ -115,7 +115,7 @@ class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL);
}
AllocResult //
AllocPixbufResult //
AllocPixbuf(const wuffs_base__image_config& image_config,
bool allow_uninitialized_memory) override {
if (m_surface) {
@@ -126,7 +126,7 @@ class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
uint32_t w = image_config.pixcfg.width();
uint32_t h = image_config.pixcfg.height();
if ((w > 0xFFFFFF) || (h > 0xFFFFFF)) {
return AllocResult("Wuffs_Load_RW_Callbacks: image is too large");
return AllocPixbufResult("Wuffs_Load_RW_Callbacks: image is too large");
}
uint32_t sdl_pixelformat = SDL_PIXELFORMAT_BGRA32;
@@ -140,14 +140,24 @@ class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
m_surface = SDL_CreateRGBSurfaceWithFormat(
0, static_cast<int>(w), static_cast<int>(h), 32, sdl_pixelformat);
if (!m_surface) {
return AllocResult(
return AllocPixbufResult(
"Wuffs_Load_RW_Callbacks: SDL_CreateRGBSurface failed");
}
SDL_LockSurface(m_surface);
return AllocResult(
wuffs_aux::MemOwner(NULL, &free),
wuffs_base__make_slice_u8(static_cast<uint8_t*>(m_surface->pixels),
m_surface->h * m_surface->pitch));
wuffs_base__pixel_buffer pixbuf;
wuffs_base__status status = pixbuf.set_interleaved(
&image_config.pixcfg,
wuffs_base__make_table_u8(static_cast<uint8_t*>(m_surface->pixels),
m_surface->w * 4, m_surface->h,
m_surface->pitch),
wuffs_base__empty_slice_u8());
if (!status.is_ok()) {
SDL_UnlockSurface(m_surface);
SDL_FreeSurface(m_surface);
m_surface = NULL;
return AllocPixbufResult(status.message());
}
return AllocPixbufResult(wuffs_aux::MemOwner(NULL, &free), pixbuf);
}
SDL_Surface* m_surface;